Browse Source

added code points support

pull/9/head
Khoren Markosyan 3 years ago
parent
commit
70a1164181
  1. 3
      .vscode/settings.json
  2. 49
      ios/Classes/src/native_zxing.cpp
  3. 15
      ios/Classes/src/native_zxing.h
  4. 28
      lib/generated_bindings.dart
  5. 2
      lib/src/ui/reader_widget.dart
  6. 1
      lib/src/utils/extentions.dart
  7. 2
      zxscanner/pubspec.lock

3
.vscode/settings.json vendored

@ -4,6 +4,7 @@
"__config": "cpp",
"__nullptr": "cpp",
"codecvt": "cpp",
"cmath": "cpp"
"cmath": "cpp",
"array": "cpp"
}
}

49
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<int>(result.format()));
std::wstring_convert<std::codecvt_utf8<wchar_t>> 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<int>(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<int>(result.format()));
std::wstring_convert<std::codecvt_utf8<wchar_t>> 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<int>(result.format()));
std::wstring_convert<std::codecvt_utf8<wchar_t>> 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);
}
}

15
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

28
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> pos;
}
class CodeResults extends ffi.Struct {

2
lib/src/ui/reader_widget.dart

@ -158,7 +158,7 @@ class _ReaderWidgetState extends State<ReaderWidget>
final CodeResult result = await processCameraImage(
image,
format: widget.codeFormat,
cropPercent: widget.cropPercent,
cropPercent: widget.showCroppingRect ? widget.cropPercent : 0,
);
if (result.isValidBool) {
widget.onScan(result);

1
lib/src/utils/extentions.dart

@ -21,6 +21,7 @@ extension CodeExt on CodeResult {
String? get textString =>
text == nullptr ? null : text.cast<Utf8>().toDartString();
String get formatString => barcodeFormatName(format);
Pos get position => pos.ref;
}
extension EncodeExt on EncodeResult {

2
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:

Loading…
Cancel
Save