Skip to content

Commit 4b7ad04

Browse files
committed
fix proper cropping
1 parent fb1b064 commit 4b7ad04

File tree

1 file changed

+40
-34
lines changed

1 file changed

+40
-34
lines changed

lib/com/hydrologis/flutterlibs/camera/camera_advanced.dart

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -229,43 +229,49 @@ class _AdvancedCameraWidgetState extends State<AdvancedCameraWidget>
229229
icon: Icon(MdiIcons.crop, size: iconSize),
230230
color: SmashColors.mainDecorations,
231231
onPressed: () {
232-
if (imageFile != null) {
233-
var frameProperties = widget.frameProperties;
234-
if (frameProperties != null &&
235-
frameProperties.ratio != null) {
236-
// crop the picture to the defined frame
237-
// at the momento we only handle ratio cases
238-
var imgFile = File(imageFile!.path);
239-
final image =
240-
IMG.decodeImage(imgFile.readAsBytesSync());
241-
242-
if (image != null) {
243-
var imageWidht = image.width;
244-
var imageHeight = image.height;
245-
var ratio = frameProperties.ratio!;
246-
var newWidth = imageHeight * ratio;
247-
var newHeight = newWidth / ratio;
248-
if (newHeight > imageHeight) {
249-
newHeight = imageWidht / ratio;
250-
newWidth = newHeight * ratio;
251-
}
252-
var left = (imageWidht - newWidth) / 2;
253-
var top = (imageHeight - newHeight) / 2;
254-
255-
// Crop the image (parameters: x, y, width, height)
256-
final croppedImage = IMG.copyCrop(image,
257-
x: left.toInt(),
258-
y: top.toInt(),
259-
width: newWidth.toInt(),
260-
height: newHeight.toInt());
261-
262-
// Save the cropped image as a new file
263-
File(imageFile!.path)
264-
..writeAsBytesSync(IMG.encodeJpg(croppedImage));
232+
var finalPath = imageFile!.path;
233+
var frameProperties = widget.frameProperties;
234+
if (frameProperties != null &&
235+
frameProperties.ratio != null) {
236+
// crop the picture to the defined frame
237+
// at the momento we only handle ratio cases
238+
var imgFile = File(imageFile!.path);
239+
final image = IMG.decodeImage(imgFile.readAsBytesSync());
240+
241+
if (image != null) {
242+
var imageWidth = image.width;
243+
var imageHeight = image.height;
244+
var ratio = frameProperties.ratio!;
245+
var newWidth = imageHeight * ratio;
246+
var newHeight = newWidth / ratio;
247+
if (newWidth > imageWidth) {
248+
newHeight = imageWidth / ratio;
249+
newWidth = newHeight * ratio;
265250
}
251+
var left = (imageWidth - newWidth) / 2;
252+
var top = (imageHeight - newHeight) / 2;
253+
254+
// print("Old image size: $imageWidth x $imageHeight");
255+
// print(
256+
// "Cropping image to: left: $left, top: $top, width: $newWidth, height: $newHeight");
257+
258+
// Crop the image (parameters: x, y, width, height)
259+
final croppedImage = IMG.copyCrop(image,
260+
x: left.toInt(),
261+
y: top.toInt(),
262+
width: newWidth.toInt(),
263+
height: newHeight.toInt());
264+
265+
// Save the cropped image as a new file
266+
var finalFile = HU.FileUtilities.getTmpFile("jpg");
267+
// print("Saving to cropped image: $finalFile");
268+
File(finalFile.path)
269+
..writeAsBytesSync(IMG.encodeJpg(croppedImage),
270+
flush: true);
271+
finalPath = finalFile.path;
266272
}
267273
}
268-
Navigator.of(context).pop(imageFile!.path);
274+
Navigator.of(context).pop(finalPath);
269275
},
270276
),
271277
),

0 commit comments

Comments
 (0)