Browse Source

added read barcodes new functions

pull/5/head
Khoren Markosyan 2 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'; part of 'zxing.dart';
/// Reads barcode from String image path /// Reads barcode from String image path
Future<CodeResult?> readImagePathString( Future<CodeResult?> readBarcodeImagePathString(
String path, { String path, {
int format = Format.Any, int format = Format.Any,
int cropWidth = 0, int cropWidth = 0,
int cropHeight = 0, int cropHeight = 0,
}) => }) =>
readImagePath(XFile(path), readBarcodeImagePath(
format: format, cropWidth: cropWidth, cropHeight: cropHeight); XFile(path),
format: format,
cropWidth: cropWidth,
cropHeight: cropHeight,
);
/// Reads barcode from XFile image path /// Reads barcode from XFile image path
Future<CodeResult?> readImagePath( Future<CodeResult?> readBarcodeImagePath(
XFile path, { XFile path, {
int format = Format.Any, int format = Format.Any,
int cropWidth = 0, int cropWidth = 0,
@ -24,16 +28,16 @@ Future<CodeResult?> readImagePath(
} }
return readBarcode( return readBarcode(
image.getBytes(format: imglib.Format.luminance), image.getBytes(format: imglib.Format.luminance),
format, width: image.width,
image.width, height: image.height,
image.height, format: format,
cropWidth, cropWidth: cropWidth,
cropHeight, cropHeight: cropHeight,
); );
} }
/// Reads barcode from image url /// Reads barcode from image url
Future<CodeResult?> readImageUrl( Future<CodeResult?> readBarcodeImageUrl(
String url, { String url, {
int format = Format.Any, int format = Format.Any,
int cropWidth = 0, int cropWidth = 0,
@ -47,23 +51,23 @@ Future<CodeResult?> readImageUrl(
} }
return readBarcode( return readBarcode(
image.getBytes(format: imglib.Format.luminance), image.getBytes(format: imglib.Format.luminance),
format, width: image.width,
image.width, height: image.height,
image.height, format: format,
cropWidth, cropWidth: cropWidth,
cropHeight, cropHeight: cropHeight,
); );
} }
// Reads barcode from Uint8List image bytes // Reads barcode from Uint8List image bytes
CodeResult readBarcode( CodeResult readBarcode(
Uint8List bytes, Uint8List bytes, {
int format, required int width,
int width, required int height,
int height, int format = Format.Any,
int cropWidth, int cropWidth = 0,
int cropHeight, int cropHeight = 0,
) => }) =>
bindings.readBarcode( bindings.readBarcode(
bytes.allocatePointer(), bytes.allocatePointer(),
format, format,

71
lib/src/logic/barcodes_reader.dart

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

10
lib/src/utils/isolate_utils.dart

@ -64,8 +64,14 @@ class IsolateUtils {
final int cropSize = final int cropSize =
(min(image.width, image.height) * cropPercent).round(); (min(image.width, image.height) * cropPercent).round();
final CodeResult result = readBarcode(bytes, isolateData.format, final CodeResult result = readBarcode(
image.width, image.height, cropSize, cropSize); bytes,
width: image.width,
height: image.height,
format: isolateData.format,
cropWidth: cropSize,
cropHeight: cropSize,
);
isolateData.responsePort?.send(result); isolateData.responsePort?.send(result);
} catch (e) { } catch (e) {

2
zxscanner/lib/pages/scanner_page.dart

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

Loading…
Cancel
Save