Browse Source

added EncodeParams, Params renamed to DecodeParams

pull/76/head
Khoren Markosyan 2 years ago
parent
commit
11a99a654b
  1. 2
      example/lib/main.dart
  2. 29
      lib/flutter_zxing.dart
  3. 10
      lib/src/logic/barcode_reader.dart
  4. 10
      lib/src/logic/barcodes_reader.dart
  5. 4
      lib/src/logic/camera_stream.dart
  6. 41
      lib/src/models/params.dart
  7. 2
      lib/src/ui/reader_widget.dart
  8. 4
      lib/src/ui/writer_widget.dart
  9. 2
      lib/src/utils/isolate_utils.dart
  10. 38
      lib/zxing_mobile.dart
  11. 28
      lib/zxing_web.dart

2
example/lib/main.dart

@ -203,7 +203,7 @@ class ScanFromGalleryWidget extends StatelessWidget {
if (file != null) { if (file != null) {
final Code? result = await zx.readBarcodeImagePath( final Code? result = await zx.readBarcodeImagePath(
file, file,
params: Params(tryInverted: true), params: DecodeParams(tryInverted: true),
); );
if (result != null && result.isValid) { if (result != null && result.isValid) {
onScan?.call(result); onScan?.call(result);

29
lib/flutter_zxing.dart

@ -20,13 +20,10 @@ abstract class Zxing {
void setLogEnabled(bool enabled) {} void setLogEnabled(bool enabled) {}
String barcodeFormatName(int format) => ''; String barcodeFormatName(int format) => '';
Encode encodeBarcode( /// Creates barcode from the given contents
String contents, { Encode encodeBarcode({
int format = Format.qrCode, required String contents,
int width = 300, required EncodeParams params,
int height = 300,
int margin = 0,
int eccLevel = 0,
}); });
/// Starts reading barcode from the camera /// Starts reading barcode from the camera
@ -38,25 +35,25 @@ abstract class Zxing {
/// Reads barcode from the camera /// Reads barcode from the camera
Future<Code> processCameraImage( Future<Code> processCameraImage(
CameraImage image, { CameraImage image, {
Params? params, DecodeParams? params,
}); });
/// Reads barcode from String image path /// Reads barcode from String image path
Future<Code?> readBarcodeImagePathString( Future<Code?> readBarcodeImagePathString(
String path, { String path, {
Params? params, DecodeParams? params,
}); });
/// Reads barcode from XFile image path /// Reads barcode from XFile image path
Future<Code?> readBarcodeImagePath( Future<Code?> readBarcodeImagePath(
XFile path, { XFile path, {
Params? params, DecodeParams? params,
}); });
/// Reads barcode from image url /// Reads barcode from image url
Future<Code?> readBarcodeImageUrl( Future<Code?> readBarcodeImageUrl(
String url, { String url, {
Params? params, DecodeParams? params,
}); });
// Reads barcode from Uint8List image bytes // Reads barcode from Uint8List image bytes
@ -64,25 +61,25 @@ abstract class Zxing {
Uint8List bytes, { Uint8List bytes, {
required int width, required int width,
required int height, required int height,
Params? params, DecodeParams? params,
}); });
/// Reads barcodes from String image path /// Reads barcodes from String image path
Future<List<Code>> readBarcodesImagePathString( Future<List<Code>> readBarcodesImagePathString(
String path, { String path, {
Params? params, DecodeParams? params,
}); });
/// Reads barcodes from XFile image path /// Reads barcodes from XFile image path
Future<List<Code>> readBarcodesImagePath( Future<List<Code>> readBarcodesImagePath(
XFile path, { XFile path, {
Params? params, DecodeParams? params,
}); });
/// Reads barcodes from image url /// Reads barcodes from image url
Future<List<Code>> readBarcodesImageUrl( Future<List<Code>> readBarcodesImageUrl(
String url, { String url, {
Params? params, DecodeParams? params,
}); });
/// Reads barcodes from Uint8List image bytes /// Reads barcodes from Uint8List image bytes
@ -90,6 +87,6 @@ abstract class Zxing {
Uint8List bytes, { Uint8List bytes, {
required int width, required int width,
required int height, required int height,
Params? params, DecodeParams? params,
}); });
} }

10
lib/src/logic/barcode_reader.dart

@ -3,7 +3,7 @@ part of 'zxing.dart';
/// Reads barcode from String image path /// Reads barcode from String image path
Future<Code?> zxingReadBarcodeImagePathString( Future<Code?> zxingReadBarcodeImagePathString(
String path, { String path, {
Params? params, DecodeParams? params,
}) => }) =>
zxingReadBarcodeImagePath( zxingReadBarcodeImagePath(
XFile(path), XFile(path),
@ -13,7 +13,7 @@ Future<Code?> zxingReadBarcodeImagePathString(
/// Reads barcode from XFile image path /// Reads barcode from XFile image path
Future<Code?> zxingReadBarcodeImagePath( Future<Code?> zxingReadBarcodeImagePath(
XFile path, { XFile path, {
Params? params, DecodeParams? params,
}) async { }) async {
final Uint8List imageBytes = await path.readAsBytes(); final Uint8List imageBytes = await path.readAsBytes();
final imglib.Image? image = imglib.decodeImage(imageBytes); final imglib.Image? image = imglib.decodeImage(imageBytes);
@ -31,7 +31,7 @@ Future<Code?> zxingReadBarcodeImagePath(
/// Reads barcode from image url /// Reads barcode from image url
Future<Code?> zxingReadBarcodeImageUrl( Future<Code?> zxingReadBarcodeImageUrl(
String url, { String url, {
Params? params, DecodeParams? params,
}) async { }) async {
final Uint8List imageBytes = final Uint8List imageBytes =
(await NetworkAssetBundle(Uri.parse(url)).load(url)).buffer.asUint8List(); (await NetworkAssetBundle(Uri.parse(url)).load(url)).buffer.asUint8List();
@ -52,7 +52,7 @@ Code zxingReadBarcode(
Uint8List bytes, { Uint8List bytes, {
required int width, required int width,
required int height, required int height,
Params? params, DecodeParams? params,
}) { }) {
Code result = _readBarcode(bytes, width, height, params); Code result = _readBarcode(bytes, width, height, params);
if (!result.isValid && params != null && params.tryInverted == true) { if (!result.isValid && params != null && params.tryInverted == true) {
@ -67,7 +67,7 @@ Code _readBarcode(
Uint8List bytes, Uint8List bytes,
int width, int width,
int height, int height,
Params? params, DecodeParams? params,
) => ) =>
bindings bindings
.readBarcode( .readBarcode(

10
lib/src/logic/barcodes_reader.dart

@ -3,7 +3,7 @@ part of 'zxing.dart';
/// Reads barcodes from String image path /// Reads barcodes from String image path
Future<List<Code>> zxingReadBarcodesImagePathString( Future<List<Code>> zxingReadBarcodesImagePathString(
String path, { String path, {
Params? params, DecodeParams? params,
}) => }) =>
zxingReadBarcodesImagePath( zxingReadBarcodesImagePath(
XFile(path), XFile(path),
@ -13,7 +13,7 @@ Future<List<Code>> zxingReadBarcodesImagePathString(
/// Reads barcodes from XFile image path /// Reads barcodes from XFile image path
Future<List<Code>> zxingReadBarcodesImagePath( Future<List<Code>> zxingReadBarcodesImagePath(
XFile path, { XFile path, {
Params? params, DecodeParams? params,
}) async { }) async {
final Uint8List imageBytes = await path.readAsBytes(); final Uint8List imageBytes = await path.readAsBytes();
final imglib.Image? image = imglib.decodeImage(imageBytes); final imglib.Image? image = imglib.decodeImage(imageBytes);
@ -31,7 +31,7 @@ Future<List<Code>> zxingReadBarcodesImagePath(
/// Reads barcodes from image url /// Reads barcodes from image url
Future<List<Code>> zxingReadBarcodesImageUrl( Future<List<Code>> zxingReadBarcodesImageUrl(
String url, { String url, {
Params? params, DecodeParams? params,
}) async { }) async {
final Uint8List imageBytes = final Uint8List imageBytes =
(await NetworkAssetBundle(Uri.parse(url)).load(url)).buffer.asUint8List(); (await NetworkAssetBundle(Uri.parse(url)).load(url)).buffer.asUint8List();
@ -52,7 +52,7 @@ List<Code> zxingReadBarcodes(
Uint8List bytes, { Uint8List bytes, {
required int width, required int width,
required int height, required int height,
Params? params, DecodeParams? params,
}) { }) {
List<Code> results = _readBarcodes(bytes, width, height, params); List<Code> results = _readBarcodes(bytes, width, height, params);
if (results.isEmpty && params != null && params.tryInverted == true) { if (results.isEmpty && params != null && params.tryInverted == true) {
@ -67,7 +67,7 @@ List<Code> _readBarcodes(
Uint8List bytes, Uint8List bytes,
int width, int width,
int height, int height,
Params? params, DecodeParams? params,
) { ) {
final CodeResults result = bindings.readBarcodes( final CodeResults result = bindings.readBarcodes(
bytes.allocatePointer(), bytes.allocatePointer(),

4
lib/src/logic/camera_stream.dart

@ -13,9 +13,9 @@ void zxingStopCameraProcessing() => isolateUtils?.stopReadingBarcode();
Future<Code> zxingProcessCameraImage( Future<Code> zxingProcessCameraImage(
CameraImage image, { CameraImage image, {
Params? params, DecodeParams? params,
}) async { }) async {
final IsolateData isolateData = IsolateData(image, params ?? Params()); final IsolateData isolateData = IsolateData(image, params ?? DecodeParams());
final Code result = await _inference(isolateData); final Code result = await _inference(isolateData);
return result; return result;
} }

41
lib/src/models/params.dart

@ -3,8 +3,8 @@ import 'dart:core';
import 'format.dart'; import 'format.dart';
// Represents the parameters for decoding a barcode // Represents the parameters for decoding a barcode
class Params { class DecodeParams {
Params({ DecodeParams({
this.format = Format.any, this.format = Format.any,
this.cropWidth = 0, this.cropWidth = 0,
this.cropHeight = 0, this.cropHeight = 0,
@ -13,10 +13,47 @@ class Params {
this.tryInverted = false, this.tryInverted = false,
}); });
// Specify a set of BarcodeFormats that should be searched for, the default is all supported formats.
int format; int format;
// The width of the area of the image to scan, in pixels. If 0, the entire image width is used.
int cropWidth; int cropWidth;
// The height of the area of the image to scan, in pixels. If 0, the entire image height is used.
int cropHeight; int cropHeight;
// Spend more time to try to find a barcode; optimize for accuracy, not speed.
bool tryHarder; bool tryHarder;
// Try to detect rotated code
bool tryRotate; bool tryRotate;
// Try to detect inverted code
bool tryInverted; 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;
}

2
lib/src/ui/reader_widget.dart

@ -201,7 +201,7 @@ class _ReaderWidgetState extends State<ReaderWidget>
widget.showScannerOverlay ? widget.cropPercent : 0; widget.showScannerOverlay ? widget.cropPercent : 0;
final int cropSize = final int cropSize =
(min(image.width, image.height) * cropPercent).round(); (min(image.width, image.height) * cropPercent).round();
final Params params = Params( final DecodeParams params = DecodeParams(
format: widget.codeFormat, format: widget.codeFormat,
cropWidth: cropSize, cropWidth: cropSize,
cropHeight: cropSize, cropHeight: cropSize,

4
lib/src/ui/writer_widget.dart

@ -222,12 +222,14 @@ class _WriterWidgetState extends State<WriterWidget>
final int margin = int.parse(_marginController.value.text); final int margin = int.parse(_marginController.value.text);
final int ecc = int.parse(_eccController.value.text); final int ecc = int.parse(_eccController.value.text);
final Encode result = zx.encodeBarcode( final Encode result = zx.encodeBarcode(
text, contents: text,
params: EncodeParams(
format: _codeFormat, format: _codeFormat,
width: width, width: width,
height: height, height: height,
margin: margin, margin: margin,
eccLevel: ecc, eccLevel: ecc,
),
); );
String? error; String? error;
if (result.isValid && result.data != null) { if (result.isValid && result.data != null) {

2
lib/src/utils/isolate_utils.dart

@ -16,7 +16,7 @@ class IsolateData {
this.params, this.params,
); );
CameraImage cameraImage; CameraImage cameraImage;
Params params; DecodeParams params;
SendPort? responsePort; SendPort? responsePort;
} }

38
lib/zxing_mobile.dart

@ -26,21 +26,17 @@ class ZxingMobile implements Zxing {
String barcodeFormatName(int format) => zxingBarcodeFormatName(format); String barcodeFormatName(int format) => zxingBarcodeFormatName(format);
@override @override
Encode encodeBarcode( Encode encodeBarcode({
String contents, { required String contents,
int format = Format.qrCode, required EncodeParams params,
int width = 300,
int height = 300,
int margin = 0,
int eccLevel = 0,
}) => }) =>
zxingEncodeBarcode( zxingEncodeBarcode(
contents, contents,
format: format, format: params.format,
width: width, width: params.width,
height: height, height: params.height,
margin: margin, margin: params.margin,
eccLevel: eccLevel, eccLevel: params.eccLevel,
); );
@override @override
@ -52,7 +48,7 @@ class ZxingMobile implements Zxing {
@override @override
Future<Code> processCameraImage( Future<Code> processCameraImage(
CameraImage image, { CameraImage image, {
Params? params, DecodeParams? params,
}) => }) =>
zxingProcessCameraImage( zxingProcessCameraImage(
image, image,
@ -62,7 +58,7 @@ class ZxingMobile implements Zxing {
@override @override
Future<Code?> readBarcodeImagePathString( Future<Code?> readBarcodeImagePathString(
String path, { String path, {
Params? params, DecodeParams? params,
}) => }) =>
zxingReadBarcodeImagePathString( zxingReadBarcodeImagePathString(
path, path,
@ -72,7 +68,7 @@ class ZxingMobile implements Zxing {
@override @override
Future<Code?> readBarcodeImagePath( Future<Code?> readBarcodeImagePath(
XFile path, { XFile path, {
Params? params, DecodeParams? params,
}) => }) =>
zxingReadBarcodeImagePath( zxingReadBarcodeImagePath(
path, path,
@ -82,7 +78,7 @@ class ZxingMobile implements Zxing {
@override @override
Future<Code?> readBarcodeImageUrl( Future<Code?> readBarcodeImageUrl(
String url, { String url, {
Params? params, DecodeParams? params,
}) => }) =>
zxingReadBarcodeImageUrl( zxingReadBarcodeImageUrl(
url, url,
@ -94,7 +90,7 @@ class ZxingMobile implements Zxing {
Uint8List bytes, { Uint8List bytes, {
required int width, required int width,
required int height, required int height,
Params? params, DecodeParams? params,
}) => }) =>
zxingReadBarcode( zxingReadBarcode(
bytes, bytes,
@ -106,7 +102,7 @@ class ZxingMobile implements Zxing {
@override @override
Future<List<Code>> readBarcodesImagePathString( Future<List<Code>> readBarcodesImagePathString(
String path, { String path, {
Params? params, DecodeParams? params,
}) => }) =>
zxingReadBarcodesImagePathString( zxingReadBarcodesImagePathString(
path, path,
@ -116,7 +112,7 @@ class ZxingMobile implements Zxing {
@override @override
Future<List<Code>> readBarcodesImagePath( Future<List<Code>> readBarcodesImagePath(
XFile path, { XFile path, {
Params? params, DecodeParams? params,
}) => }) =>
zxingReadBarcodesImagePath( zxingReadBarcodesImagePath(
path, path,
@ -126,7 +122,7 @@ class ZxingMobile implements Zxing {
@override @override
Future<List<Code>> readBarcodesImageUrl( Future<List<Code>> readBarcodesImageUrl(
String url, { String url, {
Params? params, DecodeParams? params,
}) => }) =>
zxingReadBarcodesImageUrl( zxingReadBarcodesImageUrl(
url, url,
@ -138,7 +134,7 @@ class ZxingMobile implements Zxing {
Uint8List bytes, { Uint8List bytes, {
required int width, required int width,
required int height, required int height,
Params? params, DecodeParams? params,
}) => }) =>
zxingReadBarcodes( zxingReadBarcodes(
bytes, bytes,

28
lib/zxing_web.dart

@ -19,13 +19,9 @@ class ZxingWeb implements Zxing {
String barcodeFormatName(int format) => 'Unsupported'; String barcodeFormatName(int format) => 'Unsupported';
@override @override
Encode encodeBarcode( Encode encodeBarcode({
String contents, { required String contents,
int format = Format.qrCode, required EncodeParams params,
int width = 300,
int height = 300,
int margin = 0,
int eccLevel = 0,
}) => }) =>
throw UnimplementedError(); throw UnimplementedError();
@ -38,28 +34,28 @@ class ZxingWeb implements Zxing {
@override @override
Future<Code> processCameraImage( Future<Code> processCameraImage(
CameraImage image, { CameraImage image, {
Params? params, DecodeParams? params,
}) => }) =>
throw UnimplementedError(); throw UnimplementedError();
@override @override
Future<Code?> readBarcodeImagePathString( Future<Code?> readBarcodeImagePathString(
String path, { String path, {
Params? params, DecodeParams? params,
}) => }) =>
throw UnimplementedError(); throw UnimplementedError();
@override @override
Future<Code?> readBarcodeImagePath( Future<Code?> readBarcodeImagePath(
XFile path, { XFile path, {
Params? params, DecodeParams? params,
}) => }) =>
throw UnimplementedError(); throw UnimplementedError();
@override @override
Future<Code?> readBarcodeImageUrl( Future<Code?> readBarcodeImageUrl(
String url, { String url, {
Params? params, DecodeParams? params,
}) => }) =>
throw UnimplementedError(); throw UnimplementedError();
@ -68,28 +64,28 @@ class ZxingWeb implements Zxing {
Uint8List bytes, { Uint8List bytes, {
required int width, required int width,
required int height, required int height,
Params? params, DecodeParams? params,
}) => }) =>
throw UnimplementedError(); throw UnimplementedError();
@override @override
Future<List<Code>> readBarcodesImagePathString( Future<List<Code>> readBarcodesImagePathString(
String path, { String path, {
Params? params, DecodeParams? params,
}) => }) =>
throw UnimplementedError(); throw UnimplementedError();
@override @override
Future<List<Code>> readBarcodesImagePath( Future<List<Code>> readBarcodesImagePath(
XFile path, { XFile path, {
Params? params, DecodeParams? params,
}) => }) =>
throw UnimplementedError(); throw UnimplementedError();
@override @override
Future<List<Code>> readBarcodesImageUrl( Future<List<Code>> readBarcodesImageUrl(
String url, { String url, {
Params? params, DecodeParams? params,
}) => }) =>
throw UnimplementedError(); throw UnimplementedError();
@ -98,7 +94,7 @@ class ZxingWeb implements Zxing {
Uint8List bytes, { Uint8List bytes, {
required int width, required int width,
required int height, required int height,
Params? params, DecodeParams? params,
}) => }) =>
throw UnimplementedError(); throw UnimplementedError();
} }

Loading…
Cancel
Save