Browse Source

migrating to image 4.x

pull/84/head
Khoren Markosyan 2 years ago
parent
commit
834a6d5f83
  1. 4
      example/pubspec.lock
  2. 23
      lib/src/logic/barcode_reader.dart
  3. 4
      lib/src/logic/barcodes_reader.dart
  4. 2
      lib/src/ui/reader_widget.dart
  5. 6
      lib/src/ui/writer_widget.dart
  6. 56
      lib/src/utils/image_converter.dart
  7. 2
      pubspec.yaml

4
example/pubspec.lock

@ -187,10 +187,10 @@ packages:
dependency: transitive
description:
name: image
sha256: "8e9d133755c3e84c73288363e6343157c383a0c6c56fc51afcc5d4d7180306d6"
sha256: "3686865febd85c57a632d87a0fb1f0a8a9ef602fdb68d909c3e9aa29ec70fd24"
url: "https://pub.dev"
source: hosted
version: "3.3.0"
version: "4.0.13"
image_picker:
dependency: transitive
description:

23
lib/src/logic/barcode_reader.dart

@ -16,13 +16,23 @@ Future<Code> zxingReadBarcodeImagePath(
DecodeParams? params,
}) async {
final Uint8List imageBytes = await path.readAsBytes();
imglib.Image? image = imglib.decodeImage(imageBytes);
final imglib.Image? image = imglib.decodeImage(imageBytes);
if (image == null) {
return Code();
}
image = resizeToMaxSize(image, params?.maxSize);
// image = resizeToMaxSize(image, params?.maxSize);
// imglib.Image imgRgba8 = image.convert(
// format: imglib.Format.uint8,
// numChannels: 4,
// ); // Make sure it's an RGBA 32-bit image like v3
// imgRgba8 =
// imglib.grayscale(imgRgba8); // map the pixels to grayscale (luminance)
// final Uint8List bytes = imgRgba8.getBytes();
return zxingReadBarcode(
image.getBytes(format: imglib.Format.luminance),
image.toUint8List(), //.getBytes(format: imglib.Format.luminance),
width: image.width,
height: image.height,
params: params,
@ -42,7 +52,7 @@ Future<Code> zxingReadBarcodeImageUrl(
}
image = resizeToMaxSize(image, params?.maxSize);
return zxingReadBarcode(
image.getBytes(format: imglib.Format.luminance),
image.toUint8List(), //.getBytes(format: imglib.Format.luminance),
width: image.width,
height: image.height,
params: params,
@ -63,8 +73,8 @@ Code _readBarcode(
int width,
int height,
DecodeParams? params,
) =>
bindings
) {
return bindings
.readBarcode(
bytes.allocatePointer(),
params?.format ?? Format.any,
@ -77,3 +87,4 @@ Code _readBarcode(
params?.tryInverted ?? false ? 1 : 0,
)
.toCode();
}

4
lib/src/logic/barcodes_reader.dart

@ -22,7 +22,7 @@ Future<Codes> zxingReadBarcodesImagePath(
}
image = resizeToMaxSize(image, params?.maxSize);
return zxingReadBarcodes(
image.getBytes(format: imglib.Format.luminance),
image.toUint8List(), //.getBytes(format: imglib.Format.luminance),
width: image.width,
height: image.height,
params: params,
@ -42,7 +42,7 @@ Future<Codes> zxingReadBarcodesImageUrl(
}
image = resizeToMaxSize(image, params?.maxSize);
return zxingReadBarcodes(
image.getBytes(format: imglib.Format.luminance),
image.toUint8List(), //.getBytes(format: imglib.Format.luminance),
width: image.width,
height: image.height,
params: params,

2
lib/src/ui/reader_widget.dart

@ -223,7 +223,7 @@ class _ReaderWidgetState extends State<ReaderWidget>
widget.resolution,
enableAudio: false,
imageFormatGroup:
isAndroid() ? ImageFormatGroup.yuv420 : ImageFormatGroup.bgra8888,
isAndroid() ? ImageFormatGroup.yuv420 : ImageFormatGroup.yuv420,
);
controller = cameraController;
cameraController.addListener(rebuildOnMount);

6
lib/src/ui/writer_widget.dart

@ -278,9 +278,9 @@ class _WriterWidgetState extends State<WriterWidget>
if (result.isValid && result.data != null) {
try {
final imglib.Image img = imglib.Image.fromBytes(
width,
height,
result.data!,
width: width,
height: height,
bytes: result.data!.buffer,
);
final Uint8List encodedBytes = Uint8List.fromList(
imglib.encodeJpg(img),

56
lib/src/utils/image_converter.dart

@ -16,7 +16,7 @@ Future<Uint8List> convertImage(CameraImage image) async {
} else if (image.format.group == ImageFormatGroup.bgra8888) {
img = convertBGRA8888(image);
}
return img.getBytes(format: imglib.Format.luminance);
return img.toUint8List();
} catch (e) {
debugPrint('>>>>>>>>>>>> ERROR: $e');
}
@ -25,39 +25,43 @@ Future<Uint8List> convertImage(CameraImage image) async {
imglib.Image convertBGRA8888(CameraImage image) {
return imglib.Image.fromBytes(
image.width,
image.height,
image.planes[0].bytes,
format: imglib.Format.bgra,
width: image.width,
height: image.height,
bytes: image.planes[0].bytes.buffer,
// format: imglib.Format.bgra,
// format: imglib.Format.int8,
// order: imglib.ChannelOrder.bgra,
);
}
// ignore: unused_element
imglib.Image convertYUV420(CameraImage image) {
final imglib.Image img =
imglib.Image(image.width, image.height); // Create Image buffer
// imglib.Image convertYUV420(CameraImage image) {
// final imglib.Image img = imglib.Image(
// width: image.width,
// height: image.height,
// ); // Create Image buffer
final Plane plane = image.planes[0];
const int shift = 0xFF << 24;
// final Plane plane = image.planes[0];
// const int shift = 0xFF << 24;
// Fill image buffer with plane[0] from YUV420_888
for (int x = 0; x < image.width; x++) {
for (int planeOffset = 0;
planeOffset < image.height * image.width;
planeOffset += image.width) {
final int pixelColor = plane.bytes[planeOffset + x];
// color: 0x FF FF FF FF
// A B G R
// Calculate pixel color
final int newVal =
shift | (pixelColor << 16) | (pixelColor << 8) | pixelColor;
// // Fill image buffer with plane[0] from YUV420_888
// for (int x = 0; x < image.width; x++) {
// for (int planeOffset = 0;
// planeOffset < image.height * image.width;
// planeOffset += image.width) {
// final int pixelColor = plane.bytes[planeOffset + x];
// // color: 0x FF FF FF FF
// // A B G R
// // Calculate pixel color
// final int newVal =
// shift | (pixelColor << 16) | (pixelColor << 8) | pixelColor;
img.data[planeOffset + x] = newVal;
}
}
// // img.data?.buffer[planeOffset + x] = newVal;
// }
// }
return img;
}
// return img;
// }
Uint8List invertImage(Uint8List bytes) {
final Uint8List invertedBytes = Uint8List.fromList(bytes);

2
pubspec.yaml

@ -12,7 +12,7 @@ dependencies:
ffi: ^2.0.0
flutter:
sdk: flutter
image: ^3.0.0
image: ^4.0.0
image_picker: ^0.8.0
dev_dependencies:

Loading…
Cancel
Save