HarmonyOS 图片自定义分辨率问题

HarmonyOS
2025-01-10 09:08:29
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

可以将截图后的照片通过压缩至特定大小(分辨率)后再返回储存数据。

可以先创建pixelMap获取imageInfo,计算和目标宽高的压缩比,调用pixelMap的scale方法压缩,然后在编码压缩后的pixelmap,具体参考以下代码:

// path为已获得的沙箱路径
const imageSource: image.ImageSource = image.createImageSource(path2);
let decodingOptions: image.DecodingOptions = {
  editable: true,
  desiredPixelFormat: 3,
}
this.pixelmap=await imageSource.createPixelMap(decodingOptions);
let imageInfo = await this.pixelmap.getImageInfo();
//计算压缩比
let scaleX : number = 500/imageInfo.size.width;
let scaleY : number = 500/imageInfo.size.height;
this.pixelmap.scale(scaleX,scaleY,(err : BusinessError) => {
  if (err) {
    console.error("Failed to scale pixelmap."); return;
  } else {
    console.info("Succeeded in scaling pixelmap.");
  }
});

const imagePackerApi = image.createImagePacker();
let packOpts: image.PackingOption = { format: "image/jpeg", quality: 90 };
imagePackerApi.packing( this.pixelmap, packOpts).then((data:ArrayBuffer) => {
  console.info("Succeeded in packing the image.");
}).catch((error: BusinessError) => {
  console.error("Failed to pack the image. And the error is: " + error);
})
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
分享
微博
QQ
微信
回复
2025-01-10 10:41:37
相关问题
如何在图片显示的分辨率
2509浏览 • 1回复 待解决
如何设置图片显示的分辨率
1122浏览 • 1回复 待解决
应用图标分辨率规范问题
1692浏览 • 1回复 待解决
HarmonyOS 手机分辨率怎么获取?
945浏览 • 1回复 待解决
HarmonyOS 如何获取屏幕分辨率
1323浏览 • 1回复 待解决
获取鸿蒙的分辨率高度不对
6935浏览 • 1回复 待解决
HarmonyOS 如何获取视频时长和分辨率
585浏览 • 1回复 待解决
OpenGL无法正常渲染某些分辨率YUV数据
1093浏览 • 0回复 待解决