From 58ebd29725a820ea1af7759b3ae9dde717c97c13 Mon Sep 17 00:00:00 2001 From: Khoren Markosyan Date: Thu, 7 Jul 2022 15:57:25 +0400 Subject: [PATCH] fixed Chinese support for ios --- ios/Classes/src/common.cpp | 30 ++++++++++++------- ios/Classes/src/common.h | 5 ++-- ios/Classes/src/native_zxing.cpp | 49 +++++++++++++------------------- ios/Classes/src/native_zxing.h | 8 +++--- 4 files changed, 47 insertions(+), 45 deletions(-) diff --git a/ios/Classes/src/common.cpp b/ios/Classes/src/common.cpp index e57f5ab..4fffec9 100644 --- a/ios/Classes/src/common.cpp +++ b/ios/Classes/src/common.cpp @@ -5,6 +5,13 @@ using namespace std; +bool isLogEnabled; + +void setLoggingEnabled(bool enabled) +{ + isLogEnabled = enabled; +} + long long int get_now() { return chrono::duration_cast( @@ -14,18 +21,21 @@ long long int get_now() void platform_log(const char *fmt, ...) { - va_list args; - va_start(args, fmt); + if (isLogEnabled) + { + va_list args; + va_start(args, fmt); #ifdef __ANDROID__ - __android_log_vprint(ANDROID_LOG_VERBOSE, "ndk", fmt, args); + __android_log_vprint(ANDROID_LOG_VERBOSE, "ndk", fmt, args); #elif defined(IS_WIN32) - char *buf = new char[4096]; - std::fill_n(buf, 4096, '\0'); - _vsprintf_p(buf, 4096, fmt, args); - OutputDebugStringA(buf); - delete[] buf; + char *buf = new char[4096]; + std::fill_n(buf, 4096, '\0'); + _vsprintf_p(buf, 4096, fmt, args); + OutputDebugStringA(buf); + delete[] buf; #else - vprintf(fmt, args); + vprintf(fmt, args); #endif - va_end(args); + va_end(args); + } } \ No newline at end of file diff --git a/ios/Classes/src/common.h b/ios/Classes/src/common.h index 543ee12..a4cb7b8 100644 --- a/ios/Classes/src/common.h +++ b/ios/Classes/src/common.h @@ -19,7 +19,8 @@ #define FUNCTION_ATTRIBUTE __declspec(dllexport) #endif - long long int get_now(); -void platform_log(const char *fmt, ...); \ No newline at end of file +void platform_log(const char *fmt, ...); + +void setLoggingEnabled(bool enabled); \ No newline at end of file diff --git a/ios/Classes/src/native_zxing.cpp b/ios/Classes/src/native_zxing.cpp index 858904a..c0edc65 100644 --- a/ios/Classes/src/native_zxing.cpp +++ b/ios/Classes/src/native_zxing.cpp @@ -5,16 +5,18 @@ #include "BitMatrix.h" #include "native_zxing.h" +#include +#include +#include + using namespace ZXing; extern "C" { - bool logEnabled = false; - FUNCTION_ATTRIBUTE void setLogEnabled(int enabled) { - logEnabled = enabled; + setLoggingEnabled(enabled); } FUNCTION_ATTRIBUTE @@ -48,18 +50,16 @@ extern "C" code.format = Format(static_cast(result.format())); - const wchar_t *resultText = result.text().c_str(); - size_t size = (wcslen(resultText) + 1) * sizeof(wchar_t); - code.text = new char[size]; - std::wcstombs(code.text, resultText, size); - platform_log("zxingRead: %ls\n", resultText); + std::wstring_convert> converter; + std::string text = converter.to_bytes(result.text()); + code.text = new char[text.length() + 1]; + strcpy(code.text, text.c_str()); + + platform_log("Result: %s\n", code.text); } int evalInMillis = static_cast(get_now() - start); - if (logEnabled) - { - platform_log("zxingRead: %d ms\n", evalInMillis); - } + platform_log("Read Barcode in: %d ms\n", evalInMillis); return code; } @@ -92,22 +92,19 @@ extern "C" code.format = Format(static_cast(result.format())); - const wchar_t *resultText = result.text().c_str(); - size_t size = (wcslen(resultText) + 1) * sizeof(wchar_t); - code.text = new char[size]; - std::wcstombs(code.text, resultText, size); + std::wstring_convert> converter; + std::string text = converter.to_bytes(result.text()); + code.text = new char[text.length() + 1]; + strcpy(code.text, text.c_str()); codes[i] = code; i++; - platform_log("zxingRead: %s\n", code.text); + platform_log("Result: %s\n", code.text); } } int evalInMillis = static_cast(get_now() - start); - if (logEnabled) - { - platform_log("zxingRead: %d ms\n", evalInMillis); - } + platform_log("Read Barcode in: %d ms\n", evalInMillis); return {i, codes}; } @@ -127,19 +124,13 @@ extern "C" } catch (const std::exception &e) { - if (logEnabled) - { - platform_log("Can't encode text: %s\nError: %s\n", contents, e.what()); - } + platform_log("Can't encode text: %s\nError: %s\n", contents, e.what()); result.error = new char[strlen(e.what()) + 1]; strcpy(result.error, e.what()); } int evalInMillis = static_cast(get_now() - start); - if (logEnabled) - { - platform_log("zxingEncode: %d ms\n", evalInMillis); - } + platform_log("Encode Barcode in: %d ms\n", evalInMillis); return result; } } diff --git a/ios/Classes/src/native_zxing.h b/ios/Classes/src/native_zxing.h index d39f001..19f12c4 100644 --- a/ios/Classes/src/native_zxing.h +++ b/ios/Classes/src/native_zxing.h @@ -38,7 +38,7 @@ extern "C" struct CodeResults { int count; - struct CodeResult* results; + struct CodeResult *results; }; struct EncodeResult @@ -53,8 +53,8 @@ extern "C" /** * @brief Enables or disables the logging of the library. - * - * @param enabled + * + * @param enabled */ void setLogEnabled(int enabled); @@ -88,7 +88,7 @@ extern "C" * @param cropHeight Crop height. * @param logEnabled Log enabled. * @return Barcode results. - */ + */ struct CodeResults readBarcodes(char *bytes, int format, int width, int height, int cropWidth, int cropHeight); /**