From c53da1e55c30eeb16c1351a288ecca481a31ce45 Mon Sep 17 00:00:00 2001 From: Khoren Markosyan Date: Sun, 11 Sep 2022 00:38:50 +0400 Subject: [PATCH] fixed code creation issue on ios --- ios/Classes/src/native_zxing.cpp | 11 ++++---- ios/Classes/src/native_zxing.h | 2 +- lib/generated_bindings.dart | 2 +- lib/src/ui/writer_widget.dart | 48 ++++++++++++++++++++++++++------ lib/src/utils/extentions.dart | 10 +------ 5 files changed, 48 insertions(+), 25 deletions(-) diff --git a/ios/Classes/src/native_zxing.cpp b/ios/Classes/src/native_zxing.cpp index aa62fb9..c6cbaa7 100644 --- a/ios/Classes/src/native_zxing.cpp +++ b/ios/Classes/src/native_zxing.cpp @@ -10,6 +10,7 @@ #include using namespace ZXing; +using namespace std; extern "C" { @@ -97,12 +98,12 @@ extern "C" try { auto writer = MultiFormatWriter(BarcodeFormat(format)).setMargin(margin).setEccLevel(eccLevel).setEncoding(CharacterSet::UTF8); - auto bitMatrix = writer.encode(TextUtfEncoding::FromUtf8(std::string(contents)), width, height); - result.data = ToMatrix(bitMatrix).data(); + auto bitMatrix = writer.encode(TextUtfEncoding::FromUtf8(string(contents)), width, height); + result.data = ToMatrix(bitMatrix).data(); result.length = bitMatrix.width() * bitMatrix.height(); result.isValid = true; } - catch (const std::exception &e) + catch (const exception &e) { platform_log("Can't encode text: %s\nError: %s\n", contents, e.what()); result.error = new char[strlen(e.what()) + 1]; @@ -121,8 +122,8 @@ extern "C" code->format = Format(static_cast(result.format())); - std::wstring_convert> converter; - std::string text = converter.to_bytes(result.text()); + wstring_convert> converter; + string text = converter.to_bytes(result.text()); code->text = new char[text.length() + 1]; strcpy(code->text, text.c_str()); diff --git a/ios/Classes/src/native_zxing.h b/ios/Classes/src/native_zxing.h index d49b43b..84b26f1 100644 --- a/ios/Classes/src/native_zxing.h +++ b/ios/Classes/src/native_zxing.h @@ -77,7 +77,7 @@ extern "C" int isValid; ///< Whether the barcode was successfully encoded char *text; ///< The encoded text enum Format format; ///< The format of the barcode - const unsigned int *data; ///< The encoded data + const signed char *data; ///< The encoded data int length; ///< The length of the encoded data char *error; ///< The error message }; diff --git a/lib/generated_bindings.dart b/lib/generated_bindings.dart index fe2943c..0854b30 100644 --- a/lib/generated_bindings.dart +++ b/lib/generated_bindings.dart @@ -295,7 +295,7 @@ class EncodeResult extends ffi.Struct { external int format; /// < The encoded data - external ffi.Pointer data; + external ffi.Pointer data; /// < The length of the encoded data @ffi.Int() diff --git a/lib/src/ui/writer_widget.dart b/lib/src/ui/writer_widget.dart index a6f7fa5..73df9fe 100644 --- a/lib/src/ui/writer_widget.dart +++ b/lib/src/ui/writer_widget.dart @@ -8,12 +8,24 @@ import '../../flutter_zxing.dart'; class WriterWidget extends StatefulWidget { const WriterWidget({ super.key, + this.text = '', + this.format = Format.QRCode, + this.width = 120, + this.height = 120, + this.margin = 0, + this.eccLevel = 0, this.onSuccess, this.onError, }); - final Function(EncodeResult, Uint8List?)? onSuccess; - final Function(String)? onError; + final String text; + final int format; + final int width; + final int height; + final int margin; + final int eccLevel; + final Function(EncodeResult result, Uint8List? bytes)? onSuccess; + final Function(String error)? onError; @override State createState() => _WriterWidgetState(); @@ -23,13 +35,10 @@ class _WriterWidgetState extends State with TickerProviderStateMixin { final GlobalKey _formKey = GlobalKey(); final TextEditingController _textController = TextEditingController(); - final TextEditingController _widthController = - TextEditingController(text: '300'); - final TextEditingController _heightController = - TextEditingController(text: '300'); - final TextEditingController _marginController = - TextEditingController(text: '10'); - final TextEditingController _eccController = TextEditingController(text: '0'); + final TextEditingController _widthController = TextEditingController(); + final TextEditingController _heightController = TextEditingController(); + final TextEditingController _marginController = TextEditingController(); + final TextEditingController _eccController = TextEditingController(); bool isAndroid() => Theme.of(context).platform == TargetPlatform.android; @@ -37,6 +46,27 @@ class _WriterWidgetState extends State final List _supportedFormats = CodeFormat.writerFormats; int _codeFormat = Format.QRCode; + @override + void initState() { + _textController.text = widget.text; + _widthController.text = widget.width.toString(); + _heightController.text = widget.height.toString(); + _marginController.text = widget.margin.toString(); + _eccController.text = widget.eccLevel.toString(); + _codeFormat = widget.format; + super.initState(); + } + + @override + void dispose() { + _textController.dispose(); + _widthController.dispose(); + _heightController.dispose(); + _marginController.dispose(); + _eccController.dispose(); + super.dispose(); + } + @override Widget build(BuildContext context) { return SingleChildScrollView( diff --git a/lib/src/utils/extentions.dart b/lib/src/utils/extentions.dart index 6f1422d..70b1596 100644 --- a/lib/src/utils/extentions.dart +++ b/lib/src/utils/extentions.dart @@ -2,7 +2,6 @@ import 'dart:ffi'; import 'dart:typed_data'; import 'package:ffi/ffi.dart'; -import 'package:flutter/foundation.dart'; import '../../flutter_zxing.dart'; @@ -29,14 +28,7 @@ extension EncodeExt on EncodeResult { String? get textString => text == nullptr ? null : text.cast().toDartString(); String get formatString => barcodeFormatName(format); - Uint32List get bytes { - final Pointer ptr = data.cast(); - final Uint32List bytes = ptr.asTypedList(length); - // TODO: Crashes when trying to use 'bytes'. Only on iOS device. Need help to fix. - debugPrint(bytes.toString()); - return bytes; - } - + Uint32List get bytes => Uint32List.fromList(data.cast().asTypedList(length)); String get errorMessage => error.cast().toDartString(); }