如何基于矩形叠加裁剪Flutter相机捕获的图像?

ujv3wf0j  于 2023-06-07  发布在  Flutter
关注(0)|答案(1)|浏览(363)

[Flutter]如何根据相机功能顶部的叠加裁剪图像?
我将图像设置为:
int maximum = maxmaximum();
我想裁剪的基础上的矩形覆盖的图像。
目前我将模型分配给:
model.img = image.path;
但我想先裁剪图像,然后分配给裁剪后的图像路径,而不是图像。

yi0zb3m4

yi0zb3m41#

为此,您可以使用copyCrop函数。这里有一个例子

import 'dart:ui' as ui;
import 'package:image/image.dart' as img;

final double left = ...;
final double top = ...;
final double width = ...;
final double height = ...;

final bytes = File(image.path).readAsBytesSync();
final rawImage = img.decodeImage(bytes);

final pictureRecorder = ui.PictureRecorder();
final canvas = Canvas(pictureRecorder);
final paint = Paint();
canvas.drawImage(imgCodec, Offset.zero, paint);

final croppedImage = img.copyCrop(
  rawImage,
  left.toInt(),
  top.toInt(),
  width.toInt(),
  height.toInt(),
);

final croppedBytes = img.encodePng(croppedImage);

final croppedImageFile = await saveImageToDisk(croppedBytes);
model.img = croppedImageFile.path;

在上面的代码中,替换...与裁剪矩形的实际值。

相关问题