Explorar o código

[mob][photos] Simplify face generation code

laurenspriem hai 1 ano
pai
achega
4cff6b1299
Modificáronse 1 ficheiros con 39 adicións e 5 borrados
  1. 39 5
      mobile/lib/utils/image_ml_util.dart

+ 39 - 5
mobile/lib/utils/image_ml_util.dart

@@ -525,6 +525,40 @@ Future<Image> cropImage(
   return newImage;
   return newImage;
 }
 }
 
 
+Future<Image> cropImageWithCanvasSimple(
+  Image image, {
+  required double x,
+  required double y,
+  required double width,
+  required double height,
+}) async {
+  final recorder = PictureRecorder();
+  final canvas = Canvas(
+    recorder,
+    Rect.fromPoints(
+      const Offset(0, 0),
+      Offset(width, height),
+    ),
+  );
+
+  canvas.drawImageRect(
+    image,
+    Rect.fromPoints(
+      Offset(x, y),
+      Offset(x + width, y + height),
+    ),
+    Rect.fromPoints(
+      const Offset(0, 0),
+      Offset(width, height),
+    ),
+    Paint()..filterQuality = FilterQuality.medium,
+  );
+
+  final picture = recorder.endRecording();
+  return picture.toImage(width.toInt(), height.toInt());
+}
+
+@Deprecated('Old image processing method, use `cropImage` instead!')
 /// Crops an [image] based on the specified [x], [y], [width] and [height].
 /// Crops an [image] based on the specified [x], [y], [width] and [height].
 /// Optionally, the cropped image can be resized to comply with a [maxSize] and/or [minSize].
 /// Optionally, the cropped image can be resized to comply with a [maxSize] and/or [minSize].
 /// Optionally, the cropped image can be rotated from the center by [rotation] radians.
 /// Optionally, the cropped image can be rotated from the center by [rotation] radians.
@@ -1295,10 +1329,10 @@ Future<List<Uint8List>> generateFaceThumbnailsUsingCanvas(
       final double heightAbs = faceBox.height * img.height;
       final double heightAbs = faceBox.height * img.height;
 
 
       // Prevent the face from going out of image bounds
       // Prevent the face from going out of image bounds
-      final int xCrop = (xMinAbs - widthAbs / 2).round().clamp(0, img.width);
-      final int yCrop = (yMinAbs - heightAbs / 2).round().clamp(0, img.height);
-      final int widthCrop = min((widthAbs * 2).round(), img.width - xCrop);
-      final int heightCrop = min((heightAbs * 2).round(), img.height - yCrop);
+      final num xCrop = (xMinAbs - widthAbs / 2).clamp(0, img.width);
+      final num yCrop = (yMinAbs - heightAbs / 2).clamp(0, img.height);
+      final num widthCrop = min((widthAbs * 2), img.width - xCrop);
+      final num heightCrop = min((heightAbs * 2), img.height - yCrop);
 
 
       futureFaceThumbnails.add(
       futureFaceThumbnails.add(
         cropAndEncodeCanvas(
         cropAndEncodeCanvas(
@@ -1328,7 +1362,7 @@ Future<Uint8List> cropAndEncodeCanvas(
   required double width,
   required double width,
   required double height,
   required double height,
 }) async {
 }) async {
-  final croppedImage = await cropImageWithCanvas(
+  final croppedImage = await cropImageWithCanvasSimple(
     image,
     image,
     x: x,
     x: x,
     y: y,
     y: y,