Flutter OHOS flutter_image_crop(图片裁剪)

Flutter 的图片裁剪插件

该插件附带一个Crop小部件。该小部件仅渲染图像、叠加层和用于裁剪图像的句柄。因此,它可以与其他小部件组合以构建自定义图像裁剪体验。

使用

  1. 创建一个小部件来加载和编辑图像:
ini 复制代码
final cropKey = GlobalKey<CropState>();
less 复制代码
Widget _buildCropImage() {
  return Container(
  color: Colors.black,
  padding: const EdgeInsets.all(20.0),
  child: Crop(
key: cropKey,
image: Image.file(imageFile),
aspectRatio: 4.0 / 3.0,
  ),
  );
}
  1. 访问裁剪值:
  • 比例是裁剪时按比例缩放图像宽度和高度的一个因素。1.0不需要比例。
  • 区域是一个矩形,表示图像上要裁剪的分数位置。
ini 复制代码
final crop = cropKey.currentState;
// or
// final crop = Crop.of(context);
final scale = crop.scale;
final area = crop.area;

if (area == null) {
// cannot crop, widget is not setup
// ...
}
  1. 访问和处理图像。作为请求访问照片权限的便捷功能。

    final permissionsGranted = await ImageCrop.requestPermissions();

  2. 读取图像选项,例如:宽度和高度。这是一种高效的实现,无需解码或将实际图像加载到内存中。

css 复制代码
	final options = await getImageOptions(file: file);
debugPrint('image width: ${options.width}, height: ${options.height}');
  1. 如果要将大图像加载到内存中,则有一个采样函数依赖于本机平台按比例缩小图像,然后再将其加载到内存中。例如,重新采样图像以1024x4096尽可能接近尺寸。如果它是正方形,preferredSize则可以使用它来指定宽度和高度。在 UI 中显示图像时最好利用此功能。
yaml 复制代码
	final sampleFile = await ImageCrop.sampleImage(
file: originalFile,
preferredWidth: 1024,
preferredHeight: 4096,
);
  1. 一旦Crop小部件准备就绪,就会提供对图像的裁剪和缩放的原生支持。为了生成更高质量的裁剪图像,请依靠具有首选最大宽度和高度的采样图像。放大采样图像的分辨率。裁剪后,图像的分辨率更高。示例如下:
less 复制代码
	final sampledFile = await ImageCrop.sampleImage(
file: originalFile,
preferredWidth: (1024 / crop.scale).round(),
preferredHeight: (4096 / crop.scale).round(),
);

final croppedFile = await ImageCrop.cropImage(
file: sampledFile,
area: crop.area,
);

鸿蒙OS关键代码

###裁剪方法

ini 复制代码
 private async cropImage(result: MethodResult, path: string, scale: number, left: number, top: number, right: number, bottom: number) {
let imageSource = image.createImageSource(path);
let pixelMapData = await imageSource.createPixelMap();
let options = await this.decodeImageOptions(imageSource);
if (options.isFlippedDimensions()) {
  await pixelMapData.rotate(options.getDegrees());
}

let width = options.getWidth() * (right - left) * scale;
let height = options.getHeight() * (bottom - top) * scale;
let region: image.Region = {
  x: options.getWidth() * left, y: options.getHeight() * top, size: {
height: height, width: width
  }
};
await pixelMapData.crop(region);
let dstFile = this.createTemporaryImageFilePath();
await this.savaPixelMap(pixelMapData, dstFile);
await imageSource.release();
result.success(dstFile);
  }

缩略图采样

typescript 复制代码
  private async sampleImage(result: MethodResult, path: string, maximumWidth: number, maximumHeight: number) {
let imageSource = image.createImageSource(path);
let options = await this.decodeImageOptions(imageSource);
// 缩略图采样大小,当前只能取1。
let inSampleSize = 1;//this.calculateInSampleSize(options.getWidth(), options.getHeight(), maximumWidth, maximumHeight);
let pixelMapData = await imageSource.createPixelMap({sampleSize: inSampleSize});
if (options.getWidth() > maximumWidth && options.getHeight() > maximumHeight) {
  let ratio = Math.max(maximumWidth / options.getWidth(), maximumHeight / options.getHeight());
  await pixelMapData.scale(ratio, ratio);
}
let dstFile = this.createTemporaryImageFilePath();
await this.savaPixelMap(pixelMapData, dstFile);
await imageSource.release()
// await this.copyExif(path, dstFile); //此方法OHOS接口暂时支持有问题 等新版本在验证
result.success(dstFile);
  }

获取图片属性

csharp 复制代码
  private async getImageOptions(path: string, result: MethodResult) {
let source = image.createImageSource(path);
let options = await this.decodeImageOptions(source);
let properties = new HashMap<string, number>();
properties.set("width", options.getWidth());
properties.set("height", options.getHeight());
await source.release();
result.success(properties);
  }
相关推荐
yinke小琪7 分钟前
JavaScript DOM节点操作(增删改)常用方法
前端·javascript
枣把儿11 分钟前
Vercel 收购 NuxtLabs!Nuxt UI Pro 即将免费!
前端·vue.js·nuxt.js
望获linux12 分钟前
【Linux基础知识系列】第四十三篇 - 基础正则表达式与 grep/sed
linux·运维·服务器·开发语言·前端·操作系统·嵌入式软件
爱编程的喵14 分钟前
从XMLHttpRequest到Fetch:前端异步请求的演进之路
前端·javascript
喜欢吃豆17 分钟前
深入企业内部的MCP知识(三):FastMCP工具转换(Tool Transformation)全解析:从适配到增强的工具进化指南
java·前端·人工智能·大模型·github·mcp
豆苗学前端20 分钟前
手把手实现支持百万级数据量、高可用和可扩展性的穿梭框组件
前端·javascript·面试
不见_20 分钟前
不想再写周报了?来看看这个吧!
前端·命令行
yinke小琪22 分钟前
JavaScript 事件冒泡与事件捕获
前端·javascript
pany24 分钟前
写代码的节奏,正在被 AI 改写
前端·人工智能·aigc
liliangrong77726 分钟前
ES2025新特性详解
前端