Browse Source

fixed Chinese support

pull/3/head
Khoren Markosyan 2 years ago
parent
commit
dbacf19363
  1. 2
      example/pubspec.lock
  2. 23
      ios/Classes/src/native_zxing.cpp
  3. 8
      lib/flutter_zxing.dart
  4. 50
      lib/generated_bindings.dart
  5. 2
      lib/reader_widget.dart
  6. 4
      zxscanner/pubspec.lock
  7. 2
      zxscanner/pubspec.yaml

2
example/pubspec.lock

@ -141,7 +141,7 @@ packages:
path: ".." path: ".."
relative: true relative: true
source: path source: path
version: "0.2.0" version: "0.3.0"
font_awesome_flutter: font_awesome_flutter:
dependency: "direct main" dependency: "direct main"
description: description:

23
ios/Classes/src/native_zxing.cpp

@ -24,7 +24,7 @@ extern "C"
auto *data = new uint8_t[length]; auto *data = new uint8_t[length];
memcpy(data, bytes, length); memcpy(data, bytes, length);
BarcodeFormats formats = BarcodeFormat(format); // BarcodeFormat::Any; BarcodeFormats formats = BarcodeFormat(format);
DecodeHints hints = DecodeHints().setTryHarder(false).setTryRotate(true).setFormats(formats); DecodeHints hints = DecodeHints().setTryHarder(false).setTryRotate(true).setFormats(formats);
ImageView image{data, width, height, ImageFormat::Lum}; ImageView image{data, width, height, ImageFormat::Lum};
if (cropWidth > 0 && cropHeight > 0 && cropWidth < width && cropHeight < height) if (cropWidth > 0 && cropHeight > 0 && cropWidth < width && cropHeight < height)
@ -37,10 +37,13 @@ extern "C"
if (result.isValid()) if (result.isValid())
{ {
code.isValid = result.isValid(); code.isValid = result.isValid();
code.text = new char[result.text().length() + 1];
std::string text = std::string(result.text().begin(), result.text().end());
strcpy(code.text, text.c_str());
code.format = Format(static_cast<int>(result.format())); code.format = Format(static_cast<int>(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);
} }
int evalInMillis = static_cast<int>(get_now() - start); int evalInMillis = static_cast<int>(get_now() - start);
@ -60,7 +63,7 @@ extern "C"
auto *data = new uint8_t[length]; auto *data = new uint8_t[length];
memcpy(data, bytes, length); memcpy(data, bytes, length);
BarcodeFormats formats = BarcodeFormat(format); // BarcodeFormat::Any; BarcodeFormats formats = BarcodeFormat(format);
DecodeHints hints = DecodeHints().setTryHarder(false).setTryRotate(true).setFormats(formats); DecodeHints hints = DecodeHints().setTryHarder(false).setTryRotate(true).setFormats(formats);
ImageView image{data, width, height, ImageFormat::Lum}; ImageView image{data, width, height, ImageFormat::Lum};
if (cropWidth > 0 && cropHeight > 0 && cropWidth < width && cropHeight < height) if (cropWidth > 0 && cropHeight > 0 && cropWidth < width && cropHeight < height)
@ -77,10 +80,14 @@ extern "C"
if (result.isValid()) if (result.isValid())
{ {
code.isValid = result.isValid(); code.isValid = result.isValid();
code.text = new char[result.text().length() + 1];
std::string text = std::string(result.text().begin(), result.text().end());
strcpy(code.text, text.c_str());
code.format = Format(static_cast<int>(result.format())); code.format = Format(static_cast<int>(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);
codes[i] = code; codes[i] = code;
i++; i++;
} }

8
lib/flutter_zxing.dart

@ -124,7 +124,7 @@ class FlutterZxing {
static EncodeResult encodeBarcode(String contents, int width, int height, static EncodeResult encodeBarcode(String contents, int width, int height,
int format, int margin, int eccLevel) { int format, int margin, int eccLevel) {
var result = bindings.encodeBarcode(contents.toNativeUtf8().cast<Int8>(), var result = bindings.encodeBarcode(contents.toNativeUtf8().cast<Char>(),
width, height, format, margin, eccLevel, _logEnabled); width, height, format, margin, eccLevel, _logEnabled);
return result; return result;
} }
@ -166,11 +166,11 @@ DynamicLibrary dylib = _openDynamicLibrary();
extension Uint8ListBlobConversion on Uint8List { extension Uint8ListBlobConversion on Uint8List {
/// Allocates a pointer filled with the Uint8List data. /// Allocates a pointer filled with the Uint8List data.
Pointer<Int8> allocatePointer() { Pointer<Char> allocatePointer() {
final blob = calloc<Int8>(length); final blob = calloc<Int8>(length);
final blobBytes = blob.asTypedList(length); final blobBytes = blob.asTypedList(length);
blobBytes.setAll(0, this); blobBytes.setAll(0, this);
return blob; return blob.cast<Char>();
} }
} }
@ -186,7 +186,7 @@ extension EncodeExt on EncodeResult {
String? get textString => String? get textString =>
text == nullptr ? null : text.cast<Utf8>().toDartString(); text == nullptr ? null : text.cast<Utf8>().toDartString();
String get formatString => FlutterZxing.formatName(format); String get formatString => FlutterZxing.formatName(format);
Uint32List get bytes => data.asTypedList(length); Uint32List get bytes => data.cast<Uint32>().asTypedList(length);
String get errorMessage => error.cast<Utf8>().toDartString(); String get errorMessage => error.cast<Utf8>().toDartString();
} }

50
lib/generated_bindings.dart

@ -22,14 +22,14 @@ class GeneratedBindings {
/// Returns the version of the zxing-cpp library. /// Returns the version of the zxing-cpp library.
/// ///
/// @return The version of the zxing-cpp library. /// @return The version of the zxing-cpp library.
ffi.Pointer<ffi.Int8> version() { ffi.Pointer<ffi.Char> version() {
return _version(); return _version();
} }
late final _versionPtr = late final _versionPtr =
_lookup<ffi.NativeFunction<ffi.Pointer<ffi.Int8> Function()>>('version'); _lookup<ffi.NativeFunction<ffi.Pointer<ffi.Char> Function()>>('version');
late final _version = late final _version =
_versionPtr.asFunction<ffi.Pointer<ffi.Int8> Function()>(); _versionPtr.asFunction<ffi.Pointer<ffi.Char> Function()>();
/// @brief Reads barcode from image. /// @brief Reads barcode from image.
/// @param bytes Image bytes. /// @param bytes Image bytes.
@ -41,7 +41,7 @@ class GeneratedBindings {
/// @param logEnabled Log enabled. /// @param logEnabled Log enabled.
/// @return Barcode result. /// @return Barcode result.
CodeResult readBarcode( CodeResult readBarcode(
ffi.Pointer<ffi.Int8> bytes, ffi.Pointer<ffi.Char> bytes,
int format, int format,
int width, int width,
int height, int height,
@ -62,11 +62,11 @@ class GeneratedBindings {
late final _readBarcodePtr = _lookup< late final _readBarcodePtr = _lookup<
ffi.NativeFunction< ffi.NativeFunction<
CodeResult Function(ffi.Pointer<ffi.Int8>, ffi.Int32, ffi.Int32, CodeResult Function(ffi.Pointer<ffi.Char>, ffi.Int, ffi.Int, ffi.Int,
ffi.Int32, ffi.Int32, ffi.Int32, ffi.Int32)>>('readBarcode'); ffi.Int, ffi.Int, ffi.Int)>>('readBarcode');
late final _readBarcode = _readBarcodePtr.asFunction< late final _readBarcode = _readBarcodePtr.asFunction<
CodeResult Function( CodeResult Function(
ffi.Pointer<ffi.Int8>, int, int, int, int, int, int)>(); ffi.Pointer<ffi.Char>, int, int, int, int, int, int)>();
/// @brief Reads barcodes from image. /// @brief Reads barcodes from image.
/// @param bytes Image bytes. /// @param bytes Image bytes.
@ -78,7 +78,7 @@ class GeneratedBindings {
/// @param logEnabled Log enabled. /// @param logEnabled Log enabled.
/// @return Barcode results. /// @return Barcode results.
ffi.Pointer<CodeResult> readBarcodes( ffi.Pointer<CodeResult> readBarcodes(
ffi.Pointer<ffi.Int8> bytes, ffi.Pointer<ffi.Char> bytes,
int format, int format,
int width, int width,
int height, int height,
@ -99,17 +99,11 @@ class GeneratedBindings {
late final _readBarcodesPtr = _lookup< late final _readBarcodesPtr = _lookup<
ffi.NativeFunction< ffi.NativeFunction<
ffi.Pointer<CodeResult> Function( ffi.Pointer<CodeResult> Function(ffi.Pointer<ffi.Char>, ffi.Int,
ffi.Pointer<ffi.Int8>, ffi.Int, ffi.Int, ffi.Int, ffi.Int, ffi.Int)>>('readBarcodes');
ffi.Int32,
ffi.Int32,
ffi.Int32,
ffi.Int32,
ffi.Int32,
ffi.Int32)>>('readBarcodes');
late final _readBarcodes = _readBarcodesPtr.asFunction< late final _readBarcodes = _readBarcodesPtr.asFunction<
ffi.Pointer<CodeResult> Function( ffi.Pointer<CodeResult> Function(
ffi.Pointer<ffi.Int8>, int, int, int, int, int, int)>(); ffi.Pointer<ffi.Char>, int, int, int, int, int, int)>();
/// @brief Encode a string into a barcode /// @brief Encode a string into a barcode
/// @param contents The string to encode /// @param contents The string to encode
@ -121,7 +115,7 @@ class GeneratedBindings {
/// @param eccLevel The error correction level of the barcode. Used for Aztec, PDF417, and QRCode only, [0-8]. /// @param eccLevel The error correction level of the barcode. Used for Aztec, PDF417, and QRCode only, [0-8].
/// @return The barcode data /// @return The barcode data
EncodeResult encodeBarcode( EncodeResult encodeBarcode(
ffi.Pointer<ffi.Int8> contents, ffi.Pointer<ffi.Char> contents,
int width, int width,
int height, int height,
int format, int format,
@ -142,11 +136,11 @@ class GeneratedBindings {
late final _encodeBarcodePtr = _lookup< late final _encodeBarcodePtr = _lookup<
ffi.NativeFunction< ffi.NativeFunction<
EncodeResult Function(ffi.Pointer<ffi.Int8>, ffi.Int32, ffi.Int32, EncodeResult Function(ffi.Pointer<ffi.Char>, ffi.Int, ffi.Int,
ffi.Int32, ffi.Int32, ffi.Int32, ffi.Int32)>>('encodeBarcode'); ffi.Int, ffi.Int, ffi.Int, ffi.Int)>>('encodeBarcode');
late final _encodeBarcode = _encodeBarcodePtr.asFunction< late final _encodeBarcode = _encodeBarcodePtr.asFunction<
EncodeResult Function( EncodeResult Function(
ffi.Pointer<ffi.Int8>, int, int, int, int, int, int)>(); ffi.Pointer<ffi.Char>, int, int, int, int, int, int)>();
} }
abstract class Format { abstract class Format {
@ -206,28 +200,28 @@ abstract class Format {
} }
class CodeResult extends ffi.Struct { class CodeResult extends ffi.Struct {
@ffi.Int32() @ffi.Int()
external int isValid; external int isValid;
external ffi.Pointer<ffi.Int8> text; external ffi.Pointer<ffi.Char> text;
@ffi.Int32() @ffi.Int32()
external int format; external int format;
} }
class EncodeResult extends ffi.Struct { class EncodeResult extends ffi.Struct {
@ffi.Int32() @ffi.Int()
external int isValid; external int isValid;
external ffi.Pointer<ffi.Int8> text; external ffi.Pointer<ffi.Char> text;
@ffi.Int32() @ffi.Int32()
external int format; external int format;
external ffi.Pointer<ffi.Uint32> data; external ffi.Pointer<ffi.UnsignedInt> data;
@ffi.Int32() @ffi.Int()
external int length; external int length;
external ffi.Pointer<ffi.Int8> error; external ffi.Pointer<ffi.Char> error;
} }

2
lib/reader_widget.dart

@ -89,6 +89,8 @@ class _ReaderWidgetState extends State<ReaderWidget>
} }
if (mounted) { if (mounted) {
if (message == AppLifecycleState.paused.toString()) { if (message == AppLifecycleState.paused.toString()) {
await cameraController.stopImageStream();
await cameraController.dispose();
_cameraOn = false; _cameraOn = false;
setState(() {}); setState(() {});
} }

4
zxscanner/pubspec.lock

@ -300,7 +300,7 @@ packages:
path: ".." path: ".."
relative: true relative: true
source: path source: path
version: "0.2.0" version: "0.3.0"
font_awesome_flutter: font_awesome_flutter:
dependency: "direct main" dependency: "direct main"
description: description:
@ -524,7 +524,7 @@ packages:
name: path_provider name: path_provider
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.10" version: "2.0.11"
path_provider_android: path_provider_android:
dependency: transitive dependency: transitive
description: description:

2
zxscanner/pubspec.yaml

@ -26,7 +26,7 @@ dependencies:
image_picker: ^0.8.5 image_picker: ^0.8.5
intl: ^0.17.0 intl: ^0.17.0
mobx: ^2.0.7 mobx: ^2.0.7
path_provider: ^2.0.10 path_provider: ^2.0.11
share_plus: ^4.0.7 share_plus: ^4.0.7
shared_preferences: ^2.0.15 shared_preferences: ^2.0.15
url_launcher: ^6.1.2 url_launcher: ^6.1.2

Loading…
Cancel
Save