Browse Source

added read barcodes new functions

pull/5/head
Khoren Markosyan 3 years ago
parent
commit
16559e1da4
  1. 48
      lib/src/logic/barcode_reader.dart
  2. 71
      lib/src/logic/barcodes_reader.dart
  3. 10
      lib/src/utils/isolate_utils.dart
  4. 2
      zxscanner/lib/pages/scanner_page.dart

48
lib/src/logic/barcode_reader.dart

@ -1,17 +1,21 @@
part of 'zxing.dart';
/// Reads barcode from String image path
Future<CodeResult?> readImagePathString(
Future<CodeResult?> 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<CodeResult?> readImagePath(
Future<CodeResult?> readBarcodeImagePath(
XFile path, {
int format = Format.Any,
int cropWidth = 0,
@ -24,16 +28,16 @@ Future<CodeResult?> 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<CodeResult?> readImageUrl(
Future<CodeResult?> readBarcodeImageUrl(
String url, {
int format = Format.Any,
int cropWidth = 0,
@ -47,23 +51,23 @@ Future<CodeResult?> 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,

71
lib/src/logic/barcodes_reader.dart

@ -1,8 +1,73 @@
part of 'zxing.dart';
// Reads barcodes from Uint8List image bytes
List<CodeResult> readBarcodes(Uint8List bytes, int format, int width,
int height, int cropWidth, int cropHeight) {
/// Reads barcodes from String image path
Future<List<CodeResult>> 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<List<CodeResult>> 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 <CodeResult>[];
}
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<List<CodeResult>> 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 <CodeResult>[];
}
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<CodeResult> 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<CodeResult> results = <CodeResult>[];

10
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) {

2
zxscanner/lib/pages/scanner_page.dart

@ -49,7 +49,7 @@ class _ScannerPageState extends State<ScannerPage> {
}
readCodeFromImage(XFile file) async {
final CodeResult? result = await readImagePath(file);
final CodeResult? result = await readBarcodeImagePath(file);
if (result != null && result.isValidBool) {
addCode(result);
} else {

Loading…
Cancel
Save