From 70a11641811f6d1d18cd62468a8f9271739403b2 Mon Sep 17 00:00:00 2001 From: Khoren Markosyan Date: Sat, 9 Jul 2022 22:04:01 +0400 Subject: [PATCH] added code points support --- .vscode/settings.json | 3 +- ios/Classes/src/native_zxing.cpp | 49 ++++++++++++++++---------------- ios/Classes/src/native_zxing.h | 15 ++++++++++ lib/generated_bindings.dart | 28 ++++++++++++++++++ lib/src/ui/reader_widget.dart | 2 +- lib/src/utils/extentions.dart | 1 + zxscanner/pubspec.lock | 2 +- 7 files changed, 73 insertions(+), 27 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 3898a9c..1ae53cc 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,6 +4,7 @@ "__config": "cpp", "__nullptr": "cpp", "codecvt": "cpp", - "cmath": "cpp" + "cmath": "cpp", + "array": "cpp" } } \ No newline at end of file diff --git a/ios/Classes/src/native_zxing.cpp b/ios/Classes/src/native_zxing.cpp index dc2f8ec..0cf522a 100644 --- a/ios/Classes/src/native_zxing.cpp +++ b/ios/Classes/src/native_zxing.cpp @@ -34,28 +34,18 @@ extern "C" auto *data = new uint8_t[length]; memcpy(data, bytes, length); - BarcodeFormats formats = BarcodeFormat(format); - DecodeHints hints = DecodeHints().setTryHarder(false).setTryRotate(true).setFormats(formats); ImageView image{data, width, height, ImageFormat::Lum}; if (cropWidth > 0 && cropHeight > 0 && cropWidth < width && cropHeight < height) { image = image.cropped(width / 2 - cropWidth / 2, height / 2 - cropHeight / 2, cropWidth, cropHeight); } + DecodeHints hints = DecodeHints().setTryHarder(false).setTryRotate(true).setFormats(BarcodeFormat(format)); Result result = ReadBarcode(image, hints); struct CodeResult code = {false, nullptr}; if (result.isValid()) { - code.isValid = result.isValid(); - - code.format = Format(static_cast(result.format())); - - 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); + resultToCodeResult(&code, result); } int evalInMillis = static_cast(get_now() - start); @@ -72,13 +62,12 @@ extern "C" auto *data = new uint8_t[length]; memcpy(data, bytes, length); - BarcodeFormats formats = BarcodeFormat(format); - DecodeHints hints = DecodeHints().setTryHarder(false).setTryRotate(true).setFormats(formats); ImageView image{data, width, height, ImageFormat::Lum}; if (cropWidth > 0 && cropHeight > 0 && cropWidth < width && cropHeight < height) { image = image.cropped(width / 2 - cropWidth / 2, height / 2 - cropHeight / 2, cropWidth, cropHeight); } + DecodeHints hints = DecodeHints().setTryHarder(false).setTryRotate(true).setFormats(BarcodeFormat(format)); Results results = ReadBarcodes(image, hints); auto *codes = new struct CodeResult[results.size()]; @@ -88,18 +77,9 @@ extern "C" struct CodeResult code = {false, nullptr}; if (result.isValid()) { - code.isValid = result.isValid(); - - code.format = Format(static_cast(result.format())); - - 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()); - + resultToCodeResult(&code, result); codes[i] = code; i++; - platform_log("Result: %s\n", code.text); } } @@ -133,4 +113,25 @@ extern "C" platform_log("Encode Barcode in: %d ms\n", evalInMillis); return result; } + + FUNCTION_ATTRIBUTE + void resultToCodeResult(struct CodeResult *code, Result result) + { + code->isValid = result.isValid(); + + code->format = Format(static_cast(result.format())); + + 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()); + + auto p = result.position(); + auto tl = p.topLeft(); + auto tr = p.topRight(); + auto bl = p.bottomLeft(); + auto br = p.bottomRight(); + code->pos = new Pos{tl.x, tl.y, tr.x, tr.y, bl.x, bl.y, br.x, br.y}; + platform_log("Result: %s\n", code->text); + } } diff --git a/ios/Classes/src/native_zxing.h b/ios/Classes/src/native_zxing.h index 19f12c4..f834ca5 100644 --- a/ios/Classes/src/native_zxing.h +++ b/ios/Classes/src/native_zxing.h @@ -28,11 +28,24 @@ extern "C" Any = OneDCodes | TwoDCodes, }; + struct Pos + { + int topLeftX; + int topLeftY; + int topRightX; + int topRightY; + int bottomLeftX; + int bottomLeftY; + int bottomRightX; + int bottomRightY; + }; + struct CodeResult { int isValid; char *text; enum Format format; + struct Pos *pos; }; struct CodeResults @@ -104,6 +117,8 @@ extern "C" */ struct EncodeResult encodeBarcode(char *contents, int width, int height, int format, int margin, int eccLevel); + void resultToCodeResult(struct CodeResult *code, ZXing::Result result); + #ifdef __cplusplus } #endif \ No newline at end of file diff --git a/lib/generated_bindings.dart b/lib/generated_bindings.dart index b872449..e9deca7 100644 --- a/lib/generated_bindings.dart +++ b/lib/generated_bindings.dart @@ -206,6 +206,32 @@ abstract class Format { static const int Any = 65535; } +class Pos extends ffi.Struct { + @ffi.Int() + external int topLeftX; + + @ffi.Int() + external int topLeftY; + + @ffi.Int() + external int topRightX; + + @ffi.Int() + external int topRightY; + + @ffi.Int() + external int bottomLeftX; + + @ffi.Int() + external int bottomLeftY; + + @ffi.Int() + external int bottomRightX; + + @ffi.Int() + external int bottomRightY; +} + class CodeResult extends ffi.Struct { @ffi.Int() external int isValid; @@ -214,6 +240,8 @@ class CodeResult extends ffi.Struct { @ffi.Int32() external int format; + + external ffi.Pointer pos; } class CodeResults extends ffi.Struct { diff --git a/lib/src/ui/reader_widget.dart b/lib/src/ui/reader_widget.dart index 63943f0..1e1e096 100644 --- a/lib/src/ui/reader_widget.dart +++ b/lib/src/ui/reader_widget.dart @@ -158,7 +158,7 @@ class _ReaderWidgetState extends State final CodeResult result = await processCameraImage( image, format: widget.codeFormat, - cropPercent: widget.cropPercent, + cropPercent: widget.showCroppingRect ? widget.cropPercent : 0, ); if (result.isValidBool) { widget.onScan(result); diff --git a/lib/src/utils/extentions.dart b/lib/src/utils/extentions.dart index d840544..6f1422d 100644 --- a/lib/src/utils/extentions.dart +++ b/lib/src/utils/extentions.dart @@ -21,6 +21,7 @@ extension CodeExt on CodeResult { String? get textString => text == nullptr ? null : text.cast().toDartString(); String get formatString => barcodeFormatName(format); + Pos get position => pos.ref; } extension EncodeExt on EncodeResult { diff --git a/zxscanner/pubspec.lock b/zxscanner/pubspec.lock index 7a665eb..73a894d 100644 --- a/zxscanner/pubspec.lock +++ b/zxscanner/pubspec.lock @@ -314,7 +314,7 @@ packages: path: ".." relative: true source: path - version: "0.5.0" + version: "0.6.0" font_awesome_flutter: dependency: "direct main" description: