-
-
Notifications
You must be signed in to change notification settings - Fork 287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: #1177 - new camera image getter with cropping feature #1184
Merged
monsieurtanuki
merged 2 commits into
openfoodfacts:develop
from
monsieurtanuki:feature/#1177
Mar 6, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
packages/smooth_app/lib/pages/scan/abstract_camera_image_getter.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import 'dart:typed_data'; | ||
import 'package:camera/camera.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:google_ml_barcode_scanner/google_ml_barcode_scanner.dart'; | ||
|
||
/// Abstract getter of Camera Image, for barcode scan. | ||
/// | ||
/// Use CameraController with imageFormatGroup: ImageFormatGroup.yuv420 | ||
abstract class AbstractCameraImageGetter { | ||
AbstractCameraImageGetter(this.cameraImage, this.cameraDescription); | ||
|
||
final CameraImage cameraImage; | ||
final CameraDescription cameraDescription; | ||
|
||
InputImage getInputImage() { | ||
final InputImageRotation imageRotation = | ||
InputImageRotationMethods.fromRawValue( | ||
cameraDescription.sensorOrientation) ?? | ||
InputImageRotation.Rotation_0deg; | ||
|
||
final InputImageFormat inputImageFormat = | ||
InputImageFormatMethods.fromRawValue( | ||
int.parse(cameraImage.format.raw.toString()), | ||
)!; | ||
|
||
final List<InputImagePlaneMetadata> planeData = getPlaneMetaData(); | ||
|
||
final InputImageData inputImageData = InputImageData( | ||
size: getSize(), | ||
imageRotation: imageRotation, | ||
inputImageFormat: inputImageFormat, | ||
planeData: planeData, | ||
); | ||
|
||
return InputImage.fromBytes( | ||
bytes: getBytes(), | ||
inputImageData: inputImageData, | ||
); | ||
} | ||
|
||
@protected | ||
Size getSize(); | ||
|
||
@protected | ||
Uint8List getBytes(); | ||
|
||
@protected | ||
List<InputImagePlaneMetadata> getPlaneMetaData(); | ||
} |
127 changes: 127 additions & 0 deletions
127
packages/smooth_app/lib/pages/scan/camera_image_cropper.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import 'dart:typed_data'; | ||
import 'package:camera/camera.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:google_ml_barcode_scanner/google_ml_barcode_scanner.dart'; | ||
import 'package:smooth_app/pages/scan/abstract_camera_image_getter.dart'; | ||
import 'package:typed_data/typed_buffers.dart'; | ||
|
||
/// Camera Image Cropper, in order to limit the barcode scan computations. | ||
/// | ||
/// Use CameraController with imageFormatGroup: ImageFormatGroup.yuv420 | ||
/// [left01], [top01], [width01] and [height01] are values between 0 and 1 | ||
/// that delimit the cropping area. | ||
/// For instance: | ||
/// * left01: 0, top01: 0, width01: 1, height01: .2 delimit the top 20% banner | ||
/// * left01: .5, top01: .5, width01: .5, height01: ..5 the bottom right rect | ||
class CameraImageCropper extends AbstractCameraImageGetter { | ||
CameraImageCropper( | ||
final CameraImage cameraImage, | ||
final CameraDescription cameraDescription, { | ||
required this.left01, | ||
required this.top01, | ||
required this.width01, | ||
required this.height01, | ||
}) : super(cameraImage, cameraDescription) { | ||
_computeCropParameters(); | ||
} | ||
|
||
final double left01; | ||
final double top01; | ||
final double width01; | ||
final double height01; | ||
late int _left; | ||
late int _top; | ||
late int _width; | ||
late int _height; | ||
|
||
void _computeCropParameters() { | ||
assert(width01 > 0 && width01 <= 1); | ||
assert(height01 > 0 && height01 <= 1); | ||
assert(left01 >= 0 && left01 < 1); | ||
assert(top01 >= 0 && top01 < 1); | ||
assert(left01 + width01 <= 1); | ||
assert(top01 + height01 <= 1); | ||
|
||
final int fullWidth = cameraImage.width; | ||
final int fullHeight = cameraImage.height; | ||
final int orientation = cameraDescription.sensorOrientation; | ||
|
||
int _getEven(final double value) => 2 * (value ~/ 2); | ||
|
||
if (orientation == 0) { | ||
_width = _getEven(fullWidth * width01); | ||
_height = _getEven(fullHeight * height01); | ||
_left = _getEven(fullWidth * left01); | ||
_top = _getEven(fullHeight * top01); | ||
return; | ||
} | ||
if (orientation == 90) { | ||
_width = _getEven(fullWidth * height01); | ||
_height = _getEven(fullHeight * width01); | ||
_left = _getEven(fullWidth * top01); | ||
_top = _getEven(fullHeight * left01); | ||
return; | ||
} | ||
throw Exception('Orientation $orientation not dealt with for the moment'); | ||
} | ||
|
||
// cf. https://en.wikipedia.org/wiki/YUV#Y′UV420p_(and_Y′V12_or_YV12)_to_RGB888_conversion | ||
static const Map<int, int> _planeDividers = <int, int>{ | ||
0: 1, // Y | ||
1: 2, // U | ||
2: 2, // V | ||
}; | ||
|
||
@override | ||
Size getSize() => Size(_width.toDouble(), _height.toDouble()); | ||
|
||
@override | ||
Uint8List getBytes() { | ||
int size = 0; | ||
for (final int divider in _planeDividers.values) { | ||
size += (_width ~/ divider) * (_height ~/ divider); | ||
} | ||
final Uint8Buffer buffer = Uint8Buffer(size); | ||
final int imageWidth = cameraImage.width; | ||
int planeIndex = 0; | ||
int bufferOffset = 0; | ||
for (final Plane plane in cameraImage.planes) { | ||
final int divider = _planeDividers[planeIndex]!; | ||
final int fullWidth = imageWidth ~/ divider; | ||
final int cropLeft = _left ~/ divider; | ||
final int cropTop = _top ~/ divider; | ||
final int cropWidth = _width ~/ divider; | ||
final int cropHeight = _height ~/ divider; | ||
|
||
for (int i = 0; i < cropHeight; i++) { | ||
//buffer.replaceRange(bufferOffset, bufferOffset + cropWidth, plane.bytes.getRange(15, 16)); | ||
for (int j = 0; j < cropWidth; j++) { | ||
buffer[bufferOffset++] = | ||
plane.bytes[fullWidth * (cropTop + i) + cropLeft + j]; | ||
} | ||
} | ||
planeIndex++; | ||
} | ||
|
||
return buffer.buffer.asUint8List(); | ||
} | ||
|
||
@override | ||
List<InputImagePlaneMetadata> getPlaneMetaData() { | ||
final List<InputImagePlaneMetadata> planeData = <InputImagePlaneMetadata>[]; | ||
for (final Plane plane in cameraImage.planes) { | ||
planeData.add( | ||
InputImagePlaneMetadata( | ||
bytesPerRow: (plane.bytesPerRow * _width) ~/ cameraImage.width, | ||
height: plane.height == null | ||
? null | ||
: (plane.height! * _height) ~/ cameraImage.height, | ||
width: plane.width == null | ||
? null | ||
: (plane.width! * _width) ~/ cameraImage.width, | ||
), | ||
); | ||
} | ||
return planeData; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/smooth_app/lib/pages/scan/camera_image_full_getter.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import 'dart:typed_data'; | ||
import 'package:camera/camera.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:google_ml_barcode_scanner/google_ml_barcode_scanner.dart'; | ||
import 'package:smooth_app/pages/scan/abstract_camera_image_getter.dart'; | ||
|
||
/// Camera Image helper where we get the full image. | ||
/// | ||
/// Use CameraController with imageFormatGroup: ImageFormatGroup.yuv420 | ||
class CameraImageFullGetter extends AbstractCameraImageGetter { | ||
CameraImageFullGetter( | ||
final CameraImage cameraImage, | ||
final CameraDescription cameraDescription, | ||
) : super(cameraImage, cameraDescription); | ||
|
||
@override | ||
Size getSize() => Size( | ||
cameraImage.width.toDouble(), | ||
cameraImage.height.toDouble(), | ||
); | ||
|
||
@override | ||
Uint8List getBytes() { | ||
final WriteBuffer allBytes = WriteBuffer(); | ||
for (final Plane plane in cameraImage.planes) { | ||
allBytes.putUint8List(plane.bytes); | ||
} | ||
return allBytes.done().buffer.asUint8List(); | ||
} | ||
|
||
@override | ||
List<InputImagePlaneMetadata> getPlaneMetaData() { | ||
final List<InputImagePlaneMetadata> planeData = <InputImagePlaneMetadata>[]; | ||
for (final Plane plane in cameraImage.planes) { | ||
planeData.add( | ||
InputImagePlaneMetadata( | ||
bytesPerRow: plane.bytesPerRow, | ||
height: plane.height, | ||
width: plane.width, | ||
), | ||
); | ||
} | ||
return planeData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://api.flutter.dev/flutter/meta/protected-constant.html
Looks to me like it's only usefull for libraries on pub.dev not for something private, but I could be wrong
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I read the link you've provided, I think you're right.
If I test, I see I'm right: