Browse Source

fixed code creation issue on ios

pull/30/head
Khoren Markosyan 2 years ago
parent
commit
c53da1e55c
  1. 11
      ios/Classes/src/native_zxing.cpp
  2. 2
      ios/Classes/src/native_zxing.h
  3. 2
      lib/generated_bindings.dart
  4. 48
      lib/src/ui/writer_widget.dart
  5. 10
      lib/src/utils/extentions.dart

11
ios/Classes/src/native_zxing.cpp

@ -10,6 +10,7 @@
#include <stdarg.h> #include <stdarg.h>
using namespace ZXing; using namespace ZXing;
using namespace std;
extern "C" extern "C"
{ {
@ -97,12 +98,12 @@ extern "C"
try try
{ {
auto writer = MultiFormatWriter(BarcodeFormat(format)).setMargin(margin).setEccLevel(eccLevel).setEncoding(CharacterSet::UTF8); auto writer = MultiFormatWriter(BarcodeFormat(format)).setMargin(margin).setEccLevel(eccLevel).setEncoding(CharacterSet::UTF8);
auto bitMatrix = writer.encode(TextUtfEncoding::FromUtf8(std::string(contents)), width, height); auto bitMatrix = writer.encode(TextUtfEncoding::FromUtf8(string(contents)), width, height);
result.data = ToMatrix<uint32_t>(bitMatrix).data(); result.data = ToMatrix<int8_t>(bitMatrix).data();
result.length = bitMatrix.width() * bitMatrix.height(); result.length = bitMatrix.width() * bitMatrix.height();
result.isValid = true; 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()); platform_log("Can't encode text: %s\nError: %s\n", contents, e.what());
result.error = new char[strlen(e.what()) + 1]; result.error = new char[strlen(e.what()) + 1];
@ -121,8 +122,8 @@ extern "C"
code->format = Format(static_cast<int>(result.format())); code->format = Format(static_cast<int>(result.format()));
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter; wstring_convert<codecvt_utf8<wchar_t>> converter;
std::string text = converter.to_bytes(result.text()); string text = converter.to_bytes(result.text());
code->text = new char[text.length() + 1]; code->text = new char[text.length() + 1];
strcpy(code->text, text.c_str()); strcpy(code->text, text.c_str());

2
ios/Classes/src/native_zxing.h

@ -77,7 +77,7 @@ extern "C"
int isValid; ///< Whether the barcode was successfully encoded int isValid; ///< Whether the barcode was successfully encoded
char *text; ///< The encoded text char *text; ///< The encoded text
enum Format format; ///< The format of the barcode 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 int length; ///< The length of the encoded data
char *error; ///< The error message char *error; ///< The error message
}; };

2
lib/generated_bindings.dart

@ -295,7 +295,7 @@ class EncodeResult extends ffi.Struct {
external int format; external int format;
/// < The encoded data /// < The encoded data
external ffi.Pointer<ffi.UnsignedInt> data; external ffi.Pointer<ffi.SignedChar> data;
/// < The length of the encoded data /// < The length of the encoded data
@ffi.Int() @ffi.Int()

48
lib/src/ui/writer_widget.dart

@ -8,12 +8,24 @@ import '../../flutter_zxing.dart';
class WriterWidget extends StatefulWidget { class WriterWidget extends StatefulWidget {
const WriterWidget({ const WriterWidget({
super.key, super.key,
this.text = '',
this.format = Format.QRCode,
this.width = 120,
this.height = 120,
this.margin = 0,
this.eccLevel = 0,
this.onSuccess, this.onSuccess,
this.onError, this.onError,
}); });
final Function(EncodeResult, Uint8List?)? onSuccess; final String text;
final Function(String)? onError; 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 @override
State<WriterWidget> createState() => _WriterWidgetState(); State<WriterWidget> createState() => _WriterWidgetState();
@ -23,13 +35,10 @@ class _WriterWidgetState extends State<WriterWidget>
with TickerProviderStateMixin { with TickerProviderStateMixin {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>(); final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final TextEditingController _textController = TextEditingController(); final TextEditingController _textController = TextEditingController();
final TextEditingController _widthController = final TextEditingController _widthController = TextEditingController();
TextEditingController(text: '300'); final TextEditingController _heightController = TextEditingController();
final TextEditingController _heightController = final TextEditingController _marginController = TextEditingController();
TextEditingController(text: '300'); final TextEditingController _eccController = TextEditingController();
final TextEditingController _marginController =
TextEditingController(text: '10');
final TextEditingController _eccController = TextEditingController(text: '0');
bool isAndroid() => Theme.of(context).platform == TargetPlatform.android; bool isAndroid() => Theme.of(context).platform == TargetPlatform.android;
@ -37,6 +46,27 @@ class _WriterWidgetState extends State<WriterWidget>
final List<int> _supportedFormats = CodeFormat.writerFormats; final List<int> _supportedFormats = CodeFormat.writerFormats;
int _codeFormat = Format.QRCode; 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return SingleChildScrollView( return SingleChildScrollView(

10
lib/src/utils/extentions.dart

@ -2,7 +2,6 @@ import 'dart:ffi';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:ffi/ffi.dart'; import 'package:ffi/ffi.dart';
import 'package:flutter/foundation.dart';
import '../../flutter_zxing.dart'; import '../../flutter_zxing.dart';
@ -29,14 +28,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 => barcodeFormatName(format); String get formatString => barcodeFormatName(format);
Uint32List get bytes { Uint32List get bytes => Uint32List.fromList(data.cast<Int8>().asTypedList(length));
final Pointer<Uint32> ptr = data.cast<Uint32>();
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;
}
String get errorMessage => error.cast<Utf8>().toDartString(); String get errorMessage => error.cast<Utf8>().toDartString();
} }

Loading…
Cancel
Save