Browse Source

Merge pull request #84 from khoren93/image_4

Image v4 migration
pull/89/head
Khoren Markosyan 2 years ago committed by GitHub
parent
commit
d215db6ec1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      example/pubspec.lock
  2. 10
      lib/src/logic/barcode_reader.dart
  3. 4
      lib/src/logic/barcodes_reader.dart
  4. 2
      lib/src/ui/reader_widget.dart
  5. 7
      lib/src/ui/writer_widget.dart
  6. 66
      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:

10
lib/src/logic/barcode_reader.dart

@ -17,12 +17,13 @@ Future<Code> zxingReadBarcodeImagePath(
}) async {
final Uint8List imageBytes = await path.readAsBytes();
imglib.Image? image = imglib.decodeImage(imageBytes);
if (image == null) {
return Code();
}
image = resizeToMaxSize(image, params?.maxSize);
return zxingReadBarcode(
image.getBytes(format: imglib.Format.luminance),
grayscaleBytes(image),
width: image.width,
height: image.height,
params: params,
@ -42,7 +43,7 @@ Future<Code> zxingReadBarcodeImageUrl(
}
image = resizeToMaxSize(image, params?.maxSize);
return zxingReadBarcode(
image.getBytes(format: imglib.Format.luminance),
grayscaleBytes(image),
width: image.width,
height: image.height,
params: params,
@ -63,8 +64,8 @@ Code _readBarcode(
int width,
int height,
DecodeParams? params,
) =>
bindings
) {
return bindings
.readBarcode(
bytes.allocatePointer(),
params?.format ?? Format.any,
@ -77,3 +78,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),
grayscaleBytes(image),
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),
grayscaleBytes(image),
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);

7
lib/src/ui/writer_widget.dart

@ -278,9 +278,10 @@ 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,
numChannels: 4,
);
final Uint8List encodedBytes = Uint8List.fromList(
imglib.encodeJpg(img),

66
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);
@ -78,3 +82,13 @@ imglib.Image resizeToMaxSize(imglib.Image image, int? maxSize) {
}
return image;
}
// get the bytes of the image in grayscale format (luminance) like v3
Uint8List grayscaleBytes(imglib.Image image) {
final imglib.Image imgRgba8 = image.convert(
format: imglib.Format.uint8,
numChannels: 1,
); // Make sure it's an RGBA 32-bit image like v3
imglib.grayscale(imgRgba8); // map the pixels to grayscale (luminance)
return imgRgba8.getBytes();
}

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