From 4b7ad0492b921765d0480746f571d1993e0fd496 Mon Sep 17 00:00:00 2001 From: Andrea Antonello Date: Wed, 13 Nov 2024 18:36:10 +0100 Subject: [PATCH] fix proper cropping --- .../flutterlibs/camera/camera_advanced.dart | 74 ++++++++++--------- 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/lib/com/hydrologis/flutterlibs/camera/camera_advanced.dart b/lib/com/hydrologis/flutterlibs/camera/camera_advanced.dart index 15daf6a..164c29c 100644 --- a/lib/com/hydrologis/flutterlibs/camera/camera_advanced.dart +++ b/lib/com/hydrologis/flutterlibs/camera/camera_advanced.dart @@ -229,43 +229,49 @@ class _AdvancedCameraWidgetState extends State icon: Icon(MdiIcons.crop, size: iconSize), color: SmashColors.mainDecorations, onPressed: () { - if (imageFile != null) { - var frameProperties = widget.frameProperties; - if (frameProperties != null && - frameProperties.ratio != null) { - // crop the picture to the defined frame - // at the momento we only handle ratio cases - var imgFile = File(imageFile!.path); - final image = - IMG.decodeImage(imgFile.readAsBytesSync()); - - if (image != null) { - var imageWidht = image.width; - var imageHeight = image.height; - var ratio = frameProperties.ratio!; - var newWidth = imageHeight * ratio; - var newHeight = newWidth / ratio; - if (newHeight > imageHeight) { - newHeight = imageWidht / ratio; - newWidth = newHeight * ratio; - } - var left = (imageWidht - newWidth) / 2; - var top = (imageHeight - newHeight) / 2; - - // Crop the image (parameters: x, y, width, height) - final croppedImage = IMG.copyCrop(image, - x: left.toInt(), - y: top.toInt(), - width: newWidth.toInt(), - height: newHeight.toInt()); - - // Save the cropped image as a new file - File(imageFile!.path) - ..writeAsBytesSync(IMG.encodeJpg(croppedImage)); + var finalPath = imageFile!.path; + var frameProperties = widget.frameProperties; + if (frameProperties != null && + frameProperties.ratio != null) { + // crop the picture to the defined frame + // at the momento we only handle ratio cases + var imgFile = File(imageFile!.path); + final image = IMG.decodeImage(imgFile.readAsBytesSync()); + + if (image != null) { + var imageWidth = image.width; + var imageHeight = image.height; + var ratio = frameProperties.ratio!; + var newWidth = imageHeight * ratio; + var newHeight = newWidth / ratio; + if (newWidth > imageWidth) { + newHeight = imageWidth / ratio; + newWidth = newHeight * ratio; } + var left = (imageWidth - newWidth) / 2; + var top = (imageHeight - newHeight) / 2; + + // print("Old image size: $imageWidth x $imageHeight"); + // print( + // "Cropping image to: left: $left, top: $top, width: $newWidth, height: $newHeight"); + + // Crop the image (parameters: x, y, width, height) + final croppedImage = IMG.copyCrop(image, + x: left.toInt(), + y: top.toInt(), + width: newWidth.toInt(), + height: newHeight.toInt()); + + // Save the cropped image as a new file + var finalFile = HU.FileUtilities.getTmpFile("jpg"); + // print("Saving to cropped image: $finalFile"); + File(finalFile.path) + ..writeAsBytesSync(IMG.encodeJpg(croppedImage), + flush: true); + finalPath = finalFile.path; } } - Navigator.of(context).pop(imageFile!.path); + Navigator.of(context).pop(finalPath); }, ), ),