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);
  }
相关推荐
啃火龙果的兔子4 分钟前
前端单元测试覆盖率工具有哪些,分别有什么优缺点
前端·单元测试
「、皓子~32 分钟前
后台管理系统的诞生 - 利用AI 1天完成整个后台管理系统的微服务后端+前端
前端·人工智能·微服务·小程序·go·ai编程·ai写作
就改了34 分钟前
Ajax——在OA系统提升性能的局部刷新
前端·javascript·ajax
凌冰_36 分钟前
Ajax 入门
前端·javascript·ajax
京东零售技术1 小时前
京东小程序JS API仓颉改造实践
前端
老A技术联盟1 小时前
从小白入门,基于Cursor开发一个前端小程序之Cursor 编程实践与案例分析
前端·小程序
风铃喵游1 小时前
构建引擎: 打造小程序编译器
前端·小程序·架构
sunbyte1 小时前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | ThemeClock(主题时钟)
前端·javascript·css·vue.js·前端框架·tailwindcss
小飞悟1 小时前
🎯 什么是模块化?CommonJS 和 ES6 Modules 到底有什么区别?小白也能看懂
前端·javascript·设计
浏览器API调用工程师_Taylor1 小时前
AOP魔法:一招实现登录弹窗的全局拦截与动态处理
前端·javascript·vue.js