diff --git a/lib/src/logic/barcode_reader.dart b/lib/src/logic/barcode_reader.dart index 6d467bd..3e25eca 100644 --- a/lib/src/logic/barcode_reader.dart +++ b/lib/src/logic/barcode_reader.dart @@ -1,17 +1,21 @@ part of 'zxing.dart'; /// Reads barcode from String image path -Future readImagePathString( +Future readBarcodeImagePathString( String path, { int format = Format.Any, int cropWidth = 0, int cropHeight = 0, }) => - readImagePath(XFile(path), - format: format, cropWidth: cropWidth, cropHeight: cropHeight); + readBarcodeImagePath( + XFile(path), + format: format, + cropWidth: cropWidth, + cropHeight: cropHeight, + ); /// Reads barcode from XFile image path -Future readImagePath( +Future readBarcodeImagePath( XFile path, { int format = Format.Any, int cropWidth = 0, @@ -24,16 +28,16 @@ Future readImagePath( } return readBarcode( image.getBytes(format: imglib.Format.luminance), - format, - image.width, - image.height, - cropWidth, - cropHeight, + width: image.width, + height: image.height, + format: format, + cropWidth: cropWidth, + cropHeight: cropHeight, ); } /// Reads barcode from image url -Future readImageUrl( +Future readBarcodeImageUrl( String url, { int format = Format.Any, int cropWidth = 0, @@ -47,23 +51,23 @@ Future readImageUrl( } return readBarcode( image.getBytes(format: imglib.Format.luminance), - format, - image.width, - image.height, - cropWidth, - cropHeight, + width: image.width, + height: image.height, + format: format, + cropWidth: cropWidth, + cropHeight: cropHeight, ); } // Reads barcode from Uint8List image bytes CodeResult readBarcode( - Uint8List bytes, - int format, - int width, - int height, - int cropWidth, - int cropHeight, -) => + Uint8List bytes, { + required int width, + required int height, + int format = Format.Any, + int cropWidth = 0, + int cropHeight = 0, +}) => bindings.readBarcode( bytes.allocatePointer(), format, diff --git a/lib/src/logic/barcodes_reader.dart b/lib/src/logic/barcodes_reader.dart index e71b65d..80a4000 100644 --- a/lib/src/logic/barcodes_reader.dart +++ b/lib/src/logic/barcodes_reader.dart @@ -1,8 +1,73 @@ part of 'zxing.dart'; -// Reads barcodes from Uint8List image bytes -List readBarcodes(Uint8List bytes, int format, int width, - int height, int cropWidth, int cropHeight) { +/// Reads barcodes from String image path +Future> readBarcodesImagePathString( + String path, { + int format = Format.Any, + int cropWidth = 0, + int cropHeight = 0, +}) => + readBarcodesImagePath( + XFile(path), + format: format, + cropWidth: cropWidth, + cropHeight: cropHeight, + ); + +/// Reads barcodes from XFile image path +Future> readBarcodesImagePath( + XFile path, { + int format = Format.Any, + int cropWidth = 0, + int cropHeight = 0, +}) async { + final Uint8List imageBytes = await path.readAsBytes(); + final imglib.Image? image = imglib.decodeImage(imageBytes); + if (image == null) { + return []; + } + return readBarcodes( + image.getBytes(format: imglib.Format.luminance), + width: image.width, + height: image.height, + format: format, + cropWidth: cropWidth, + cropHeight: cropHeight, + ); +} + +/// Reads barcodes from image url +Future> readBarcodesImageUrl( + String url, { + int format = Format.Any, + int cropWidth = 0, + int cropHeight = 0, +}) async { + final Uint8List imageBytes = + (await NetworkAssetBundle(Uri.parse(url)).load(url)).buffer.asUint8List(); + final imglib.Image? image = imglib.decodeImage(imageBytes); + if (image == null) { + return []; + } + return readBarcodes( + image.getBytes(format: imglib.Format.luminance), + width: image.width, + height: image.height, + format: format, + cropWidth: cropWidth, + cropHeight: cropHeight, + ); +} + +/// Reads barcodes from Uint8List image bytes +List readBarcodes( + Uint8List bytes, { + required int width, + required int height, + int format = Format.Any, + int cropWidth = 0, + int cropHeight = 0, +}) { final CodeResults result = bindings.readBarcodes( bytes.allocatePointer(), format, width, height, cropWidth, cropHeight); final List results = []; diff --git a/lib/src/utils/isolate_utils.dart b/lib/src/utils/isolate_utils.dart index 03b9ce5..43e0d33 100644 --- a/lib/src/utils/isolate_utils.dart +++ b/lib/src/utils/isolate_utils.dart @@ -64,8 +64,14 @@ class IsolateUtils { final int cropSize = (min(image.width, image.height) * cropPercent).round(); - final CodeResult result = readBarcode(bytes, isolateData.format, - image.width, image.height, cropSize, cropSize); + final CodeResult result = readBarcode( + bytes, + width: image.width, + height: image.height, + format: isolateData.format, + cropWidth: cropSize, + cropHeight: cropSize, + ); isolateData.responsePort?.send(result); } catch (e) { diff --git a/zxscanner/lib/pages/scanner_page.dart b/zxscanner/lib/pages/scanner_page.dart index 0f32963..7b06bff 100644 --- a/zxscanner/lib/pages/scanner_page.dart +++ b/zxscanner/lib/pages/scanner_page.dart @@ -49,7 +49,7 @@ class _ScannerPageState extends State { } readCodeFromImage(XFile file) async { - final CodeResult? result = await readImagePath(file); + final CodeResult? result = await readBarcodeImagePath(file); if (result != null && result.isValidBool) { addCode(result); } else {