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(); +}