diff --git a/example/pubspec.lock b/example/pubspec.lock index 4fae50a..38b8d2d 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..a0d488b 100644 --- a/lib/src/logic/barcode_reader.dart +++ b/lib/src/logic/barcode_reader.dart @@ -17,12 +17,13 @@ Future zxingReadBarcodeImagePath( }) async { final Uint8List imageBytes = await path.readAsBytes(); imglib.Image? image = imglib.decodeImage(imageBytes); + if (image == null) { return Code(); } image = resizeToMaxSize(image, params?.maxSize); return zxingReadBarcode( - image.getBytes(format: imglib.Format.luminance), + grayscaleBytes(image), width: image.width, height: image.height, params: params, @@ -42,7 +43,7 @@ Future zxingReadBarcodeImageUrl( } image = resizeToMaxSize(image, params?.maxSize); return zxingReadBarcode( - image.getBytes(format: imglib.Format.luminance), + grayscaleBytes(image), width: image.width, height: image.height, params: params, @@ -63,17 +64,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..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.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.getBytes(format: imglib.Format.luminance), + grayscaleBytes(image), 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 c85820f..32d5e67 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..ee57b57 100644 --- a/lib/src/ui/writer_widget.dart +++ b/lib/src/ui/writer_widget.dart @@ -278,9 +278,10 @@ 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, + 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 1a6e2bf..d0b4171 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); @@ -78,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(); +} diff --git a/pubspec.yaml b/pubspec.yaml index 7ec2e49..975bfb1 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: