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) {
final Code? result = await zx.readBarcodeImagePath(
file,
params: Params(tryInverted: true),
params: DecodeParams(tryInverted: true),
);
if (result != null && result.isValid) {
onScan?.call(result);

29
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<Code> processCameraImage(
CameraImage image, {
Params? params,
DecodeParams? params,
});
/// Reads barcode from String image path
Future<Code?> readBarcodeImagePathString(
String path, {
Params? params,
DecodeParams? params,
});
/// Reads barcode from XFile image path
Future<Code?> readBarcodeImagePath(
XFile path, {
Params? params,
DecodeParams? params,
});
/// Reads barcode from image url
Future<Code?> 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<List<Code>> readBarcodesImagePathString(
String path, {
Params? params,
DecodeParams? params,
});
/// Reads barcodes from XFile image path
Future<List<Code>> readBarcodesImagePath(
XFile path, {
Params? params,
DecodeParams? params,
});
/// Reads barcodes from image url
Future<List<Code>> 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,
});
}

10
lib/src/logic/barcode_reader.dart

@ -3,7 +3,7 @@ part of 'zxing.dart';
/// Reads barcode from String image path
Future<Code?> zxingReadBarcodeImagePathString(
String path, {
Params? params,
DecodeParams? params,
}) =>
zxingReadBarcodeImagePath(
XFile(path),
@ -13,7 +13,7 @@ Future<Code?> zxingReadBarcodeImagePathString(
/// Reads barcode from XFile image path
Future<Code?> 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<Code?> zxingReadBarcodeImagePath(
/// Reads barcode from image url
Future<Code?> 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(

10
lib/src/logic/barcodes_reader.dart

@ -3,7 +3,7 @@ part of 'zxing.dart';
/// Reads barcodes from String image path
Future<List<Code>> zxingReadBarcodesImagePathString(
String path, {
Params? params,
DecodeParams? params,
}) =>
zxingReadBarcodesImagePath(
XFile(path),
@ -13,7 +13,7 @@ Future<List<Code>> zxingReadBarcodesImagePathString(
/// Reads barcodes from XFile image path
Future<List<Code>> 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<List<Code>> zxingReadBarcodesImagePath(
/// Reads barcodes from image url
Future<List<Code>> 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<Code> zxingReadBarcodes(
Uint8List bytes, {
required int width,
required int height,
Params? params,
DecodeParams? params,
}) {
List<Code> results = _readBarcodes(bytes, width, height, params);
if (results.isEmpty && params != null && params.tryInverted == true) {
@ -67,7 +67,7 @@ List<Code> _readBarcodes(
Uint8List bytes,
int width,
int height,
Params? params,
DecodeParams? params,
) {
final CodeResults result = bindings.readBarcodes(
bytes.allocatePointer(),

4
lib/src/logic/camera_stream.dart

@ -13,9 +13,9 @@ void zxingStopCameraProcessing() => isolateUtils?.stopReadingBarcode();
Future<Code> 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;
}

41
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;
}

2
lib/src/ui/reader_widget.dart

@ -201,7 +201,7 @@ class _ReaderWidgetState extends State<ReaderWidget>
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,

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 ecc = int.parse(_eccController.value.text);
final Encode result = zx.encodeBarcode(
text,
contents: text,
params: EncodeParams(
format: _codeFormat,
width: width,
height: height,
margin: margin,
eccLevel: ecc,
),
);
String? error;
if (result.isValid && result.data != null) {

2
lib/src/utils/isolate_utils.dart

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

38
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<Code> processCameraImage(
CameraImage image, {
Params? params,
DecodeParams? params,
}) =>
zxingProcessCameraImage(
image,
@ -62,7 +58,7 @@ class ZxingMobile implements Zxing {
@override
Future<Code?> readBarcodeImagePathString(
String path, {
Params? params,
DecodeParams? params,
}) =>
zxingReadBarcodeImagePathString(
path,
@ -72,7 +68,7 @@ class ZxingMobile implements Zxing {
@override
Future<Code?> readBarcodeImagePath(
XFile path, {
Params? params,
DecodeParams? params,
}) =>
zxingReadBarcodeImagePath(
path,
@ -82,7 +78,7 @@ class ZxingMobile implements Zxing {
@override
Future<Code?> 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<List<Code>> readBarcodesImagePathString(
String path, {
Params? params,
DecodeParams? params,
}) =>
zxingReadBarcodesImagePathString(
path,
@ -116,7 +112,7 @@ class ZxingMobile implements Zxing {
@override
Future<List<Code>> readBarcodesImagePath(
XFile path, {
Params? params,
DecodeParams? params,
}) =>
zxingReadBarcodesImagePath(
path,
@ -126,7 +122,7 @@ class ZxingMobile implements Zxing {
@override
Future<List<Code>> 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,

28
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<Code> processCameraImage(
CameraImage image, {
Params? params,
DecodeParams? params,
}) =>
throw UnimplementedError();
@override
Future<Code?> readBarcodeImagePathString(
String path, {
Params? params,
DecodeParams? params,
}) =>
throw UnimplementedError();
@override
Future<Code?> readBarcodeImagePath(
XFile path, {
Params? params,
DecodeParams? params,
}) =>
throw UnimplementedError();
@override
Future<Code?> 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<List<Code>> readBarcodesImagePathString(
String path, {
Params? params,
DecodeParams? params,
}) =>
throw UnimplementedError();
@override
Future<List<Code>> readBarcodesImagePath(
XFile path, {
Params? params,
DecodeParams? params,
}) =>
throw UnimplementedError();
@override
Future<List<Code>> 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();
}

Loading…
Cancel
Save