Skip to content
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

Crop the image output size #58

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/src/controllers/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class CustomImageCropController {
final listeners = <CustomImageCropListener>[];

/// Crop the image
Future<MemoryImage?> onCropImage() =>
listeners.map((e) => e.onCropImage()).first;
Future<MemoryImage?> onCropImage({Size? outSize}) =>
listeners.map((e) => e.onCropImage(outSize: outSize)).first;

/// The data that handles the transformation of the cropped image.
CropImageData? get cropImageData => listeners.map((e) => e.data).first;
Expand Down Expand Up @@ -48,5 +48,5 @@ mixin CustomImageCropListener {
void setData(CropImageData transition);

/// Crop the image
Future<MemoryImage?> onCropImage();
Future<MemoryImage?> onCropImage({Size? outSize});
}
14 changes: 11 additions & 3 deletions lib/src/widgets/custom_image_crop_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ class _CustomImageCropState extends State<CustomImageCrop>
}

@override
Future<MemoryImage?> onCropImage() async {
Future<MemoryImage?> onCropImage({Size? outSize}) async {
if (_imageAsUIImage == null) {
return null;
}
Expand Down Expand Up @@ -598,6 +598,14 @@ class _CustomImageCropState extends State<CustomImageCrop>
bgPaint,
);
canvas.save();
if (outSize != null) {
Matrix4 matrix4Scale = Matrix4.diagonal3(vector_math.Vector3.all(1))
..scale(
outSize.width / onCropParams.cropSizeWidth,
outSize.height / onCropParams.cropSizeHeight,
);
canvas.transform(matrix4Scale.storage);
}
canvas.clipPath(clipPath);
canvas.transform(matrix4Image.storage);
canvas.drawImage(
Expand All @@ -614,8 +622,8 @@ class _CustomImageCropState extends State<CustomImageCrop>

ui.Picture picture = pictureRecorder.endRecording();
ui.Image image = await picture.toImage(
onCropParams.cropSizeWidth.floor(),
onCropParams.cropSizeHeight.floor(),
outSize?.width.floor() ?? onCropParams.cropSizeWidth.floor(),
outSize?.height.floor() ?? onCropParams.cropSizeHeight.floor(),
);

// Adding compute would be preferrable. Unfortunately we cannot pass an ui image to this.
Expand Down