From 834a6d5f83c4da3be965e9642b97ad3f370cfa3d Mon Sep 17 00:00:00 2001 From: Khoren Markosyan Date: Thu, 9 Feb 2023 17:59:52 +0400 Subject: [PATCH 1/2] migrating to image 4.x --- example/pubspec.lock | 4 +-- lib/src/logic/barcode_reader.dart | 47 +++++++++++++++---------- lib/src/logic/barcodes_reader.dart | 4 +-- lib/src/ui/reader_widget.dart | 2 +- lib/src/ui/writer_widget.dart | 6 ++-- lib/src/utils/image_converter.dart | 56 ++++++++++++++++-------------- pubspec.yaml | 2 +- 7 files changed, 68 insertions(+), 53 deletions(-) diff --git a/example/pubspec.lock b/example/pubspec.lock index b491d3e..317e6d9 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -187,10 +187,10 @@ packages: dependency: transitive description: name: image - sha256: "8e9d133755c3e84c73288363e6343157c383a0c6c56fc51afcc5d4d7180306d6" + sha256: "3686865febd85c57a632d87a0fb1f0a8a9ef602fdb68d909c3e9aa29ec70fd24" url: "https://pub.dev" source: hosted - version: "3.3.0" + version: "4.0.13" image_picker: dependency: transitive description: diff --git a/lib/src/logic/barcode_reader.dart b/lib/src/logic/barcode_reader.dart index ae2c8da..9d56a2a 100644 --- a/lib/src/logic/barcode_reader.dart +++ b/lib/src/logic/barcode_reader.dart @@ -16,13 +16,23 @@ Future zxingReadBarcodeImagePath( DecodeParams? params, }) async { final Uint8List imageBytes = await path.readAsBytes(); - imglib.Image? image = imglib.decodeImage(imageBytes); + final imglib.Image? image = imglib.decodeImage(imageBytes); + if (image == null) { return Code(); } - image = resizeToMaxSize(image, params?.maxSize); + // image = resizeToMaxSize(image, params?.maxSize); + + // imglib.Image imgRgba8 = image.convert( + // format: imglib.Format.uint8, + // numChannels: 4, + // ); // Make sure it's an RGBA 32-bit image like v3 + // imgRgba8 = + // imglib.grayscale(imgRgba8); // map the pixels to grayscale (luminance) + // final Uint8List bytes = imgRgba8.getBytes(); + return zxingReadBarcode( - image.getBytes(format: imglib.Format.luminance), + image.toUint8List(), //.getBytes(format: imglib.Format.luminance), width: image.width, height: image.height, params: params, @@ -42,7 +52,7 @@ Future zxingReadBarcodeImageUrl( } image = resizeToMaxSize(image, params?.maxSize); return zxingReadBarcode( - image.getBytes(format: imglib.Format.luminance), + image.toUint8List(), //.getBytes(format: imglib.Format.luminance), width: image.width, height: image.height, params: params, @@ -63,17 +73,18 @@ Code _readBarcode( int width, int height, DecodeParams? params, -) => - bindings - .readBarcode( - bytes.allocatePointer(), - params?.format ?? Format.any, - width, - height, - params?.cropWidth ?? 0, - params?.cropHeight ?? 0, - params?.tryHarder ?? false ? 1 : 0, - params?.tryRotate ?? true ? 1 : 0, - params?.tryInverted ?? false ? 1 : 0, - ) - .toCode(); +) { + return bindings + .readBarcode( + bytes.allocatePointer(), + params?.format ?? Format.any, + width, + height, + params?.cropWidth ?? 0, + params?.cropHeight ?? 0, + params?.tryHarder ?? false ? 1 : 0, + params?.tryRotate ?? true ? 1 : 0, + params?.tryInverted ?? false ? 1 : 0, + ) + .toCode(); +} diff --git a/lib/src/logic/barcodes_reader.dart b/lib/src/logic/barcodes_reader.dart index dd7a732..f46c7b6 100644 --- a/lib/src/logic/barcodes_reader.dart +++ b/lib/src/logic/barcodes_reader.dart @@ -22,7 +22,7 @@ Future zxingReadBarcodesImagePath( } image = resizeToMaxSize(image, params?.maxSize); return zxingReadBarcodes( - image.getBytes(format: imglib.Format.luminance), + image.toUint8List(), //.getBytes(format: imglib.Format.luminance), width: image.width, height: image.height, params: params, @@ -42,7 +42,7 @@ Future zxingReadBarcodesImageUrl( } image = resizeToMaxSize(image, params?.maxSize); return zxingReadBarcodes( - image.getBytes(format: imglib.Format.luminance), + image.toUint8List(), //.getBytes(format: imglib.Format.luminance), width: image.width, height: image.height, params: params, diff --git a/lib/src/ui/reader_widget.dart b/lib/src/ui/reader_widget.dart index 5a32a05..ed46b45 100644 --- a/lib/src/ui/reader_widget.dart +++ b/lib/src/ui/reader_widget.dart @@ -223,7 +223,7 @@ class _ReaderWidgetState extends State widget.resolution, enableAudio: false, imageFormatGroup: - isAndroid() ? ImageFormatGroup.yuv420 : ImageFormatGroup.bgra8888, + isAndroid() ? ImageFormatGroup.yuv420 : ImageFormatGroup.yuv420, ); controller = cameraController; cameraController.addListener(rebuildOnMount); diff --git a/lib/src/ui/writer_widget.dart b/lib/src/ui/writer_widget.dart index 11e3903..e369371 100644 --- a/lib/src/ui/writer_widget.dart +++ b/lib/src/ui/writer_widget.dart @@ -278,9 +278,9 @@ class _WriterWidgetState extends State if (result.isValid && result.data != null) { try { final imglib.Image img = imglib.Image.fromBytes( - width, - height, - result.data!, + width: width, + height: height, + bytes: result.data!.buffer, ); final Uint8List encodedBytes = Uint8List.fromList( imglib.encodeJpg(img), diff --git a/lib/src/utils/image_converter.dart b/lib/src/utils/image_converter.dart index 1a6e2bf..54cd351 100644 --- a/lib/src/utils/image_converter.dart +++ b/lib/src/utils/image_converter.dart @@ -16,7 +16,7 @@ Future convertImage(CameraImage image) async { } else if (image.format.group == ImageFormatGroup.bgra8888) { img = convertBGRA8888(image); } - return img.getBytes(format: imglib.Format.luminance); + return img.toUint8List(); } catch (e) { debugPrint('>>>>>>>>>>>> ERROR: $e'); } @@ -25,39 +25,43 @@ Future convertImage(CameraImage image) async { imglib.Image convertBGRA8888(CameraImage image) { return imglib.Image.fromBytes( - image.width, - image.height, - image.planes[0].bytes, - format: imglib.Format.bgra, + width: image.width, + height: image.height, + bytes: image.planes[0].bytes.buffer, + // format: imglib.Format.bgra, + // format: imglib.Format.int8, + // order: imglib.ChannelOrder.bgra, ); } // ignore: unused_element -imglib.Image convertYUV420(CameraImage image) { - final imglib.Image img = - imglib.Image(image.width, image.height); // Create Image buffer +// imglib.Image convertYUV420(CameraImage image) { +// final imglib.Image img = imglib.Image( +// width: image.width, +// height: image.height, +// ); // Create Image buffer - final Plane plane = image.planes[0]; - const int shift = 0xFF << 24; +// final Plane plane = image.planes[0]; +// const int shift = 0xFF << 24; - // Fill image buffer with plane[0] from YUV420_888 - for (int x = 0; x < image.width; x++) { - for (int planeOffset = 0; - planeOffset < image.height * image.width; - planeOffset += image.width) { - final int pixelColor = plane.bytes[planeOffset + x]; - // color: 0x FF FF FF FF - // A B G R - // Calculate pixel color - final int newVal = - shift | (pixelColor << 16) | (pixelColor << 8) | pixelColor; +// // Fill image buffer with plane[0] from YUV420_888 +// for (int x = 0; x < image.width; x++) { +// for (int planeOffset = 0; +// planeOffset < image.height * image.width; +// planeOffset += image.width) { +// final int pixelColor = plane.bytes[planeOffset + x]; +// // color: 0x FF FF FF FF +// // A B G R +// // Calculate pixel color +// final int newVal = +// shift | (pixelColor << 16) | (pixelColor << 8) | pixelColor; - img.data[planeOffset + x] = newVal; - } - } +// // img.data?.buffer[planeOffset + x] = newVal; +// } +// } - return img; -} +// return img; +// } Uint8List invertImage(Uint8List bytes) { final Uint8List invertedBytes = Uint8List.fromList(bytes); diff --git a/pubspec.yaml b/pubspec.yaml index 299eb90..695ea8d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -12,7 +12,7 @@ dependencies: ffi: ^2.0.0 flutter: sdk: flutter - image: ^3.0.0 + image: ^4.0.0 image_picker: ^0.8.0 dev_dependencies: From cb4f63f921297c9a3cf7ac34da7b97cfe7fb2a30 Mon Sep 17 00:00:00 2001 From: Khoren Markosyan Date: Mon, 27 Feb 2023 16:07:22 +0400 Subject: [PATCH 2/2] correctly get bytes --- lib/src/logic/barcode_reader.dart | 17 ++++------------- lib/src/logic/barcodes_reader.dart | 4 ++-- lib/src/ui/writer_widget.dart | 1 + lib/src/utils/image_converter.dart | 10 ++++++++++ 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/src/logic/barcode_reader.dart b/lib/src/logic/barcode_reader.dart index 9d56a2a..a0d488b 100644 --- a/lib/src/logic/barcode_reader.dart +++ b/lib/src/logic/barcode_reader.dart @@ -16,23 +16,14 @@ Future zxingReadBarcodeImagePath( DecodeParams? params, }) async { final Uint8List imageBytes = await path.readAsBytes(); - final imglib.Image? image = imglib.decodeImage(imageBytes); + imglib.Image? image = imglib.decodeImage(imageBytes); if (image == null) { return Code(); } - // image = resizeToMaxSize(image, params?.maxSize); - - // imglib.Image imgRgba8 = image.convert( - // format: imglib.Format.uint8, - // numChannels: 4, - // ); // Make sure it's an RGBA 32-bit image like v3 - // imgRgba8 = - // imglib.grayscale(imgRgba8); // map the pixels to grayscale (luminance) - // final Uint8List bytes = imgRgba8.getBytes(); - + image = resizeToMaxSize(image, params?.maxSize); return zxingReadBarcode( - image.toUint8List(), //.getBytes(format: imglib.Format.luminance), + grayscaleBytes(image), width: image.width, height: image.height, params: params, @@ -52,7 +43,7 @@ Future zxingReadBarcodeImageUrl( } image = resizeToMaxSize(image, params?.maxSize); return zxingReadBarcode( - image.toUint8List(), //.getBytes(format: imglib.Format.luminance), + grayscaleBytes(image), width: image.width, height: image.height, params: params, diff --git a/lib/src/logic/barcodes_reader.dart b/lib/src/logic/barcodes_reader.dart index f46c7b6..d803397 100644 --- a/lib/src/logic/barcodes_reader.dart +++ b/lib/src/logic/barcodes_reader.dart @@ -22,7 +22,7 @@ Future zxingReadBarcodesImagePath( } image = resizeToMaxSize(image, params?.maxSize); return zxingReadBarcodes( - image.toUint8List(), //.getBytes(format: imglib.Format.luminance), + grayscaleBytes(image), width: image.width, height: image.height, params: params, @@ -42,7 +42,7 @@ Future zxingReadBarcodesImageUrl( } image = resizeToMaxSize(image, params?.maxSize); return zxingReadBarcodes( - image.toUint8List(), //.getBytes(format: imglib.Format.luminance), + grayscaleBytes(image), width: image.width, height: image.height, params: params, diff --git a/lib/src/ui/writer_widget.dart b/lib/src/ui/writer_widget.dart index e369371..ee57b57 100644 --- a/lib/src/ui/writer_widget.dart +++ b/lib/src/ui/writer_widget.dart @@ -281,6 +281,7 @@ class _WriterWidgetState extends State width: width, height: height, bytes: result.data!.buffer, + numChannels: 4, ); final Uint8List encodedBytes = Uint8List.fromList( imglib.encodeJpg(img), diff --git a/lib/src/utils/image_converter.dart b/lib/src/utils/image_converter.dart index 54cd351..d0b4171 100644 --- a/lib/src/utils/image_converter.dart +++ b/lib/src/utils/image_converter.dart @@ -82,3 +82,13 @@ imglib.Image resizeToMaxSize(imglib.Image image, int? maxSize) { } return image; } + +// get the bytes of the image in grayscale format (luminance) like v3 +Uint8List grayscaleBytes(imglib.Image image) { + final imglib.Image imgRgba8 = image.convert( + format: imglib.Format.uint8, + numChannels: 1, + ); // Make sure it's an RGBA 32-bit image like v3 + imglib.grayscale(imgRgba8); // map the pixels to grayscale (luminance) + return imgRgba8.getBytes(); +}