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. 34
      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 dependency: transitive
description: description:
name: image name: image
sha256: "8e9d133755c3e84c73288363e6343157c383a0c6c56fc51afcc5d4d7180306d6" sha256: "3686865febd85c57a632d87a0fb1f0a8a9ef602fdb68d909c3e9aa29ec70fd24"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "3.3.0" version: "4.0.13"
image_picker: image_picker:
dependency: transitive dependency: transitive
description: description:

34
lib/src/logic/barcode_reader.dart

@ -17,12 +17,13 @@ Future<Code> zxingReadBarcodeImagePath(
}) async { }) async {
final Uint8List imageBytes = await path.readAsBytes(); final Uint8List imageBytes = await path.readAsBytes();
imglib.Image? image = imglib.decodeImage(imageBytes); imglib.Image? image = imglib.decodeImage(imageBytes);
if (image == null) { if (image == null) {
return Code(); return Code();
} }
image = resizeToMaxSize(image, params?.maxSize); image = resizeToMaxSize(image, params?.maxSize);
return zxingReadBarcode( return zxingReadBarcode(
image.getBytes(format: imglib.Format.luminance), grayscaleBytes(image),
width: image.width, width: image.width,
height: image.height, height: image.height,
params: params, params: params,
@ -42,7 +43,7 @@ Future<Code> zxingReadBarcodeImageUrl(
} }
image = resizeToMaxSize(image, params?.maxSize); image = resizeToMaxSize(image, params?.maxSize);
return zxingReadBarcode( return zxingReadBarcode(
image.getBytes(format: imglib.Format.luminance), grayscaleBytes(image),
width: image.width, width: image.width,
height: image.height, height: image.height,
params: params, params: params,
@ -63,17 +64,18 @@ Code _readBarcode(
int width, int width,
int height, int height,
DecodeParams? params, DecodeParams? params,
) => ) {
bindings return bindings
.readBarcode( .readBarcode(
bytes.allocatePointer(), bytes.allocatePointer(),
params?.format ?? Format.any, params?.format ?? Format.any,
width, width,
height, height,
params?.cropWidth ?? 0, params?.cropWidth ?? 0,
params?.cropHeight ?? 0, params?.cropHeight ?? 0,
params?.tryHarder ?? false ? 1 : 0, params?.tryHarder ?? false ? 1 : 0,
params?.tryRotate ?? true ? 1 : 0, params?.tryRotate ?? true ? 1 : 0,
params?.tryInverted ?? false ? 1 : 0, params?.tryInverted ?? false ? 1 : 0,
) )
.toCode(); .toCode();
}

4
lib/src/logic/barcodes_reader.dart

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

2
lib/src/ui/reader_widget.dart

@ -223,7 +223,7 @@ class _ReaderWidgetState extends State<ReaderWidget>
widget.resolution, widget.resolution,
enableAudio: false, enableAudio: false,
imageFormatGroup: imageFormatGroup:
isAndroid() ? ImageFormatGroup.yuv420 : ImageFormatGroup.bgra8888, isAndroid() ? ImageFormatGroup.yuv420 : ImageFormatGroup.yuv420,
); );
controller = cameraController; controller = cameraController;
cameraController.addListener(rebuildOnMount); 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) { if (result.isValid && result.data != null) {
try { try {
final imglib.Image img = imglib.Image.fromBytes( final imglib.Image img = imglib.Image.fromBytes(
width, width: width,
height, height: height,
result.data!, bytes: result.data!.buffer,
numChannels: 4,
); );
final Uint8List encodedBytes = Uint8List.fromList( final Uint8List encodedBytes = Uint8List.fromList(
imglib.encodeJpg(img), 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) { } else if (image.format.group == ImageFormatGroup.bgra8888) {
img = convertBGRA8888(image); img = convertBGRA8888(image);
} }
return img.getBytes(format: imglib.Format.luminance); return img.toUint8List();
} catch (e) { } catch (e) {
debugPrint('>>>>>>>>>>>> ERROR: $e'); debugPrint('>>>>>>>>>>>> ERROR: $e');
} }
@ -25,39 +25,43 @@ Future<Uint8List> convertImage(CameraImage image) async {
imglib.Image convertBGRA8888(CameraImage image) { imglib.Image convertBGRA8888(CameraImage image) {
return imglib.Image.fromBytes( return imglib.Image.fromBytes(
image.width, width: image.width,
image.height, height: image.height,
image.planes[0].bytes, bytes: image.planes[0].bytes.buffer,
format: imglib.Format.bgra, // format: imglib.Format.bgra,
// format: imglib.Format.int8,
// order: imglib.ChannelOrder.bgra,
); );
} }
// ignore: unused_element // ignore: unused_element
imglib.Image convertYUV420(CameraImage image) { // imglib.Image convertYUV420(CameraImage image) {
final imglib.Image img = // final imglib.Image img = imglib.Image(
imglib.Image(image.width, image.height); // Create Image buffer // width: image.width,
// height: image.height,
// ); // Create Image buffer
final Plane plane = image.planes[0]; // final Plane plane = image.planes[0];
const int shift = 0xFF << 24; // const int shift = 0xFF << 24;
// Fill image buffer with plane[0] from YUV420_888 // // Fill image buffer with plane[0] from YUV420_888
for (int x = 0; x < image.width; x++) { // for (int x = 0; x < image.width; x++) {
for (int planeOffset = 0; // for (int planeOffset = 0;
planeOffset < image.height * image.width; // planeOffset < image.height * image.width;
planeOffset += image.width) { // planeOffset += image.width) {
final int pixelColor = plane.bytes[planeOffset + x]; // final int pixelColor = plane.bytes[planeOffset + x];
// color: 0x FF FF FF FF // // color: 0x FF FF FF FF
// A B G R // // A B G R
// Calculate pixel color // // Calculate pixel color
final int newVal = // final int newVal =
shift | (pixelColor << 16) | (pixelColor << 8) | pixelColor; // 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) { Uint8List invertImage(Uint8List bytes) {
final Uint8List invertedBytes = Uint8List.fromList(bytes); final Uint8List invertedBytes = Uint8List.fromList(bytes);
@ -78,3 +82,13 @@ imglib.Image resizeToMaxSize(imglib.Image image, int? maxSize) {
} }
return image; 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 ffi: ^2.0.0
flutter: flutter:
sdk: flutter sdk: flutter
image: ^3.0.0 image: ^4.0.0
image_picker: ^0.8.0 image_picker: ^0.8.0
dev_dependencies: dev_dependencies:

Loading…
Cancel
Save