From 11a99a654b93922022624dfa69722ba59acfb127 Mon Sep 17 00:00:00 2001 From: Khoren Markosyan Date: Fri, 6 Jan 2023 21:52:52 +0400 Subject: [PATCH] added EncodeParams, Params renamed to DecodeParams --- example/lib/main.dart | 2 +- lib/flutter_zxing.dart | 29 ++++++++++----------- lib/src/logic/barcode_reader.dart | 10 ++++---- lib/src/logic/barcodes_reader.dart | 10 ++++---- lib/src/logic/camera_stream.dart | 4 +-- lib/src/models/params.dart | 41 ++++++++++++++++++++++++++++-- lib/src/ui/reader_widget.dart | 2 +- lib/src/ui/writer_widget.dart | 14 +++++----- lib/src/utils/isolate_utils.dart | 2 +- lib/zxing_mobile.dart | 38 +++++++++++++-------------- lib/zxing_web.dart | 28 +++++++++----------- 11 files changed, 104 insertions(+), 76 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 1ce7135..deb2ce5 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -203,7 +203,7 @@ class ScanFromGalleryWidget extends StatelessWidget { if (file != null) { final Code? result = await zx.readBarcodeImagePath( file, - params: Params(tryInverted: true), + params: DecodeParams(tryInverted: true), ); if (result != null && result.isValid) { onScan?.call(result); diff --git a/lib/flutter_zxing.dart b/lib/flutter_zxing.dart index 8486b70..03de081 100644 --- a/lib/flutter_zxing.dart +++ b/lib/flutter_zxing.dart @@ -20,13 +20,10 @@ abstract class Zxing { void setLogEnabled(bool enabled) {} String barcodeFormatName(int format) => ''; - Encode encodeBarcode( - String contents, { - int format = Format.qrCode, - int width = 300, - int height = 300, - int margin = 0, - int eccLevel = 0, + /// Creates barcode from the given contents + Encode encodeBarcode({ + required String contents, + required EncodeParams params, }); /// Starts reading barcode from the camera @@ -38,25 +35,25 @@ abstract class Zxing { /// Reads barcode from the camera Future processCameraImage( CameraImage image, { - Params? params, + DecodeParams? params, }); /// Reads barcode from String image path Future readBarcodeImagePathString( String path, { - Params? params, + DecodeParams? params, }); /// Reads barcode from XFile image path Future readBarcodeImagePath( XFile path, { - Params? params, + DecodeParams? params, }); /// Reads barcode from image url Future readBarcodeImageUrl( String url, { - Params? params, + DecodeParams? params, }); // Reads barcode from Uint8List image bytes @@ -64,25 +61,25 @@ abstract class Zxing { Uint8List bytes, { required int width, required int height, - Params? params, + DecodeParams? params, }); /// Reads barcodes from String image path Future> readBarcodesImagePathString( String path, { - Params? params, + DecodeParams? params, }); /// Reads barcodes from XFile image path Future> readBarcodesImagePath( XFile path, { - Params? params, + DecodeParams? params, }); /// Reads barcodes from image url Future> readBarcodesImageUrl( String url, { - Params? params, + DecodeParams? params, }); /// Reads barcodes from Uint8List image bytes @@ -90,6 +87,6 @@ abstract class Zxing { Uint8List bytes, { required int width, required int height, - Params? params, + DecodeParams? params, }); } diff --git a/lib/src/logic/barcode_reader.dart b/lib/src/logic/barcode_reader.dart index 506b959..735e4fd 100644 --- a/lib/src/logic/barcode_reader.dart +++ b/lib/src/logic/barcode_reader.dart @@ -3,7 +3,7 @@ part of 'zxing.dart'; /// Reads barcode from String image path Future zxingReadBarcodeImagePathString( String path, { - Params? params, + DecodeParams? params, }) => zxingReadBarcodeImagePath( XFile(path), @@ -13,7 +13,7 @@ Future zxingReadBarcodeImagePathString( /// Reads barcode from XFile image path Future zxingReadBarcodeImagePath( XFile path, { - Params? params, + DecodeParams? params, }) async { final Uint8List imageBytes = await path.readAsBytes(); final imglib.Image? image = imglib.decodeImage(imageBytes); @@ -31,7 +31,7 @@ Future zxingReadBarcodeImagePath( /// Reads barcode from image url Future zxingReadBarcodeImageUrl( String url, { - Params? params, + DecodeParams? params, }) async { final Uint8List imageBytes = (await NetworkAssetBundle(Uri.parse(url)).load(url)).buffer.asUint8List(); @@ -52,7 +52,7 @@ Code zxingReadBarcode( Uint8List bytes, { required int width, required int height, - Params? params, + DecodeParams? params, }) { Code result = _readBarcode(bytes, width, height, params); if (!result.isValid && params != null && params.tryInverted == true) { @@ -67,7 +67,7 @@ Code _readBarcode( Uint8List bytes, int width, int height, - Params? params, + DecodeParams? params, ) => bindings .readBarcode( diff --git a/lib/src/logic/barcodes_reader.dart b/lib/src/logic/barcodes_reader.dart index cf4be03..05f499c 100644 --- a/lib/src/logic/barcodes_reader.dart +++ b/lib/src/logic/barcodes_reader.dart @@ -3,7 +3,7 @@ part of 'zxing.dart'; /// Reads barcodes from String image path Future> zxingReadBarcodesImagePathString( String path, { - Params? params, + DecodeParams? params, }) => zxingReadBarcodesImagePath( XFile(path), @@ -13,7 +13,7 @@ Future> zxingReadBarcodesImagePathString( /// Reads barcodes from XFile image path Future> zxingReadBarcodesImagePath( XFile path, { - Params? params, + DecodeParams? params, }) async { final Uint8List imageBytes = await path.readAsBytes(); final imglib.Image? image = imglib.decodeImage(imageBytes); @@ -31,7 +31,7 @@ Future> zxingReadBarcodesImagePath( /// Reads barcodes from image url Future> zxingReadBarcodesImageUrl( String url, { - Params? params, + DecodeParams? params, }) async { final Uint8List imageBytes = (await NetworkAssetBundle(Uri.parse(url)).load(url)).buffer.asUint8List(); @@ -52,7 +52,7 @@ List zxingReadBarcodes( Uint8List bytes, { required int width, required int height, - Params? params, + DecodeParams? params, }) { List results = _readBarcodes(bytes, width, height, params); if (results.isEmpty && params != null && params.tryInverted == true) { @@ -67,7 +67,7 @@ List _readBarcodes( Uint8List bytes, int width, int height, - Params? params, + DecodeParams? params, ) { final CodeResults result = bindings.readBarcodes( bytes.allocatePointer(), diff --git a/lib/src/logic/camera_stream.dart b/lib/src/logic/camera_stream.dart index 93c4303..5e4c3bb 100644 --- a/lib/src/logic/camera_stream.dart +++ b/lib/src/logic/camera_stream.dart @@ -13,9 +13,9 @@ void zxingStopCameraProcessing() => isolateUtils?.stopReadingBarcode(); Future zxingProcessCameraImage( CameraImage image, { - Params? params, + DecodeParams? params, }) async { - final IsolateData isolateData = IsolateData(image, params ?? Params()); + final IsolateData isolateData = IsolateData(image, params ?? DecodeParams()); final Code result = await _inference(isolateData); return result; } diff --git a/lib/src/models/params.dart b/lib/src/models/params.dart index bb8b590..fcbae88 100644 --- a/lib/src/models/params.dart +++ b/lib/src/models/params.dart @@ -3,8 +3,8 @@ import 'dart:core'; import 'format.dart'; // Represents the parameters for decoding a barcode -class Params { - Params({ +class DecodeParams { + DecodeParams({ this.format = Format.any, this.cropWidth = 0, this.cropHeight = 0, @@ -13,10 +13,47 @@ class Params { this.tryInverted = false, }); + // Specify a set of BarcodeFormats that should be searched for, the default is all supported formats. int format; + + // The width of the area of the image to scan, in pixels. If 0, the entire image width is used. int cropWidth; + + // The height of the area of the image to scan, in pixels. If 0, the entire image height is used. int cropHeight; + + // Spend more time to try to find a barcode; optimize for accuracy, not speed. bool tryHarder; + + // Try to detect rotated code bool tryRotate; + + // Try to detect inverted code bool tryInverted; } + +// Represents the parameters for encoding a barcode +class EncodeParams { + EncodeParams({ + this.format = Format.qrCode, + this.width = 120, + this.height = 120, + this.margin = 0, + this.eccLevel = 0, + }); + + // The barcode format to be generated. The default is QRCode. + int format; + + // The width of the barcode image, in pixels. + int width; + + // The height of the barcode image, in pixels. + int height; + + // Used for all formats, sets the minimum number of quiet zone pixels + int margin; + + // Used for Aztec, PDF417, and QRCode only, [0-8] + int eccLevel; +} diff --git a/lib/src/ui/reader_widget.dart b/lib/src/ui/reader_widget.dart index c6fd0f0..9234544 100644 --- a/lib/src/ui/reader_widget.dart +++ b/lib/src/ui/reader_widget.dart @@ -201,7 +201,7 @@ class _ReaderWidgetState extends State widget.showScannerOverlay ? widget.cropPercent : 0; final int cropSize = (min(image.width, image.height) * cropPercent).round(); - final Params params = Params( + final DecodeParams params = DecodeParams( format: widget.codeFormat, cropWidth: cropSize, cropHeight: cropSize, diff --git a/lib/src/ui/writer_widget.dart b/lib/src/ui/writer_widget.dart index d0aa196..38942d1 100644 --- a/lib/src/ui/writer_widget.dart +++ b/lib/src/ui/writer_widget.dart @@ -222,12 +222,14 @@ class _WriterWidgetState extends State final int margin = int.parse(_marginController.value.text); final int ecc = int.parse(_eccController.value.text); final Encode result = zx.encodeBarcode( - text, - format: _codeFormat, - width: width, - height: height, - margin: margin, - eccLevel: ecc, + contents: text, + params: EncodeParams( + format: _codeFormat, + width: width, + height: height, + margin: margin, + eccLevel: ecc, + ), ); String? error; if (result.isValid && result.data != null) { diff --git a/lib/src/utils/isolate_utils.dart b/lib/src/utils/isolate_utils.dart index f7ced1a..3063bad 100644 --- a/lib/src/utils/isolate_utils.dart +++ b/lib/src/utils/isolate_utils.dart @@ -16,7 +16,7 @@ class IsolateData { this.params, ); CameraImage cameraImage; - Params params; + DecodeParams params; SendPort? responsePort; } diff --git a/lib/zxing_mobile.dart b/lib/zxing_mobile.dart index 15c04d7..14771d1 100644 --- a/lib/zxing_mobile.dart +++ b/lib/zxing_mobile.dart @@ -26,21 +26,17 @@ class ZxingMobile implements Zxing { String barcodeFormatName(int format) => zxingBarcodeFormatName(format); @override - Encode encodeBarcode( - String contents, { - int format = Format.qrCode, - int width = 300, - int height = 300, - int margin = 0, - int eccLevel = 0, + Encode encodeBarcode({ + required String contents, + required EncodeParams params, }) => zxingEncodeBarcode( contents, - format: format, - width: width, - height: height, - margin: margin, - eccLevel: eccLevel, + format: params.format, + width: params.width, + height: params.height, + margin: params.margin, + eccLevel: params.eccLevel, ); @override @@ -52,7 +48,7 @@ class ZxingMobile implements Zxing { @override Future processCameraImage( CameraImage image, { - Params? params, + DecodeParams? params, }) => zxingProcessCameraImage( image, @@ -62,7 +58,7 @@ class ZxingMobile implements Zxing { @override Future readBarcodeImagePathString( String path, { - Params? params, + DecodeParams? params, }) => zxingReadBarcodeImagePathString( path, @@ -72,7 +68,7 @@ class ZxingMobile implements Zxing { @override Future readBarcodeImagePath( XFile path, { - Params? params, + DecodeParams? params, }) => zxingReadBarcodeImagePath( path, @@ -82,7 +78,7 @@ class ZxingMobile implements Zxing { @override Future readBarcodeImageUrl( String url, { - Params? params, + DecodeParams? params, }) => zxingReadBarcodeImageUrl( url, @@ -94,7 +90,7 @@ class ZxingMobile implements Zxing { Uint8List bytes, { required int width, required int height, - Params? params, + DecodeParams? params, }) => zxingReadBarcode( bytes, @@ -106,7 +102,7 @@ class ZxingMobile implements Zxing { @override Future> readBarcodesImagePathString( String path, { - Params? params, + DecodeParams? params, }) => zxingReadBarcodesImagePathString( path, @@ -116,7 +112,7 @@ class ZxingMobile implements Zxing { @override Future> readBarcodesImagePath( XFile path, { - Params? params, + DecodeParams? params, }) => zxingReadBarcodesImagePath( path, @@ -126,7 +122,7 @@ class ZxingMobile implements Zxing { @override Future> readBarcodesImageUrl( String url, { - Params? params, + DecodeParams? params, }) => zxingReadBarcodesImageUrl( url, @@ -138,7 +134,7 @@ class ZxingMobile implements Zxing { Uint8List bytes, { required int width, required int height, - Params? params, + DecodeParams? params, }) => zxingReadBarcodes( bytes, diff --git a/lib/zxing_web.dart b/lib/zxing_web.dart index 07b4468..aa79843 100644 --- a/lib/zxing_web.dart +++ b/lib/zxing_web.dart @@ -19,13 +19,9 @@ class ZxingWeb implements Zxing { String barcodeFormatName(int format) => 'Unsupported'; @override - Encode encodeBarcode( - String contents, { - int format = Format.qrCode, - int width = 300, - int height = 300, - int margin = 0, - int eccLevel = 0, + Encode encodeBarcode({ + required String contents, + required EncodeParams params, }) => throw UnimplementedError(); @@ -38,28 +34,28 @@ class ZxingWeb implements Zxing { @override Future processCameraImage( CameraImage image, { - Params? params, + DecodeParams? params, }) => throw UnimplementedError(); @override Future readBarcodeImagePathString( String path, { - Params? params, + DecodeParams? params, }) => throw UnimplementedError(); @override Future readBarcodeImagePath( XFile path, { - Params? params, + DecodeParams? params, }) => throw UnimplementedError(); @override Future readBarcodeImageUrl( String url, { - Params? params, + DecodeParams? params, }) => throw UnimplementedError(); @@ -68,28 +64,28 @@ class ZxingWeb implements Zxing { Uint8List bytes, { required int width, required int height, - Params? params, + DecodeParams? params, }) => throw UnimplementedError(); @override Future> readBarcodesImagePathString( String path, { - Params? params, + DecodeParams? params, }) => throw UnimplementedError(); @override Future> readBarcodesImagePath( XFile path, { - Params? params, + DecodeParams? params, }) => throw UnimplementedError(); @override Future> readBarcodesImageUrl( String url, { - Params? params, + DecodeParams? params, }) => throw UnimplementedError(); @@ -98,7 +94,7 @@ class ZxingWeb implements Zxing { Uint8List bytes, { required int width, required int height, - Params? params, + DecodeParams? params, }) => throw UnimplementedError(); }