HarmonyOS 执行PixelMap进行保存

调用代码执行PixelMap进行保存,出现系统闪退

export async function savePixelMapV2(context: Context, pixelMap: PixelMap): Promise<string> {
  if (pixelMap === null) {
    console.error('传入的pm为空');
    return '';
  }
  const imagePackerApi: image.ImagePacker = image.createImagePacker();
  const path: string = context.cacheDir + '/' + getTimeStr() + '.jpg';
  const packOpts: image.PackingOption = { format: 'image/jpeg', quality: 100 };
  let file = fs.openSync(path, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
  return await imagePackerApi.packToFile(pixelMap, file.fd, packOpts).then(() => {
    //直接打包进文件
    fs.closeSync(file.fd);
    return " "
  }).catch((error: Error) => {
    console.error('Failed to pack the image. And the error is: ' + error);
    return " "
  })
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

获取相册图片进行裁剪,完成之后,对图片进行保存到本地闪退。

HarmonyOS
2024-12-27 16:10:26
732浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

demo如下:

export async  function savePixelMapV2(context: Context, pixelMap: PixelMap,width: number, height: number):Promise<string> {
  if (pixelMap === null) {
    console.error('传入的pm为空');
    return '';
  }
  const imagePackerApi: image.ImagePacker = image.createImagePacker();
  const path: string = context.cacheDir + '/' + getTimeStr() + '.jpg';
  const packOpts: image.PackingOption = { format: 'image/jpeg', quality: 100 };
  let file = fs.openSync(path, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
  console.log('path:'+path+"--file:"+file.fd)

  //该三个参数测试固定为16:9,建议封装调用
  const cropWidth = width;
  const cropHeight = Math.floor(width * (CommonConstants.CROP_RATE_9_16));
  const cropPosition = new RegionItem(0, Math.floor((height - cropHeight) / CommonConstants.AVERAGE_WEIGHT_WIDTH));

  //裁切后打包
  return  pixelMap.crop({
    size: {
      width: cropWidth,
      height: cropHeight
    },
    x: cropPosition.x,
    y: cropPosition.y
  }).then(() => {
    console.info('Sucessed in cropping pixelmap.');
    imagePackerApi.packToFile(pixelMap, file.fd, packOpts)
      .then(() => {
        console.info('Succeeded in packToFile.');
        //直接打包进文件
        fs.closeSync(file.fd);
        console.debug("path_:" + path)
        return ''
      }).catch((error : BusinessError) => {
      console.error('Failed to packToFile.');
      return ''
    })
    return '';
  }).catch((err : BusinessError) => {
    console.error('Failed to crop pixelmap.');
    return ''
  });
}
  • 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.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
分享
微博
QQ
微信
回复
2024-12-27 19:22:03


相关问题
PixelMap怎么保存成图片文件
1295浏览 • 1回复 待解决
HarmonyOS如何将PixelMap保存到相册?
1512浏览 • 1回复 待解决
HarmonyOS image.PixelMap保存到 相册问题
957浏览 • 1回复 待解决
HarmonyOS 如何将PixelMap保存为沙箱
919浏览 • 1回复 待解决
HarmonyOS image的pixelmap如何进行深拷贝
659浏览 • 1回复 待解决
pixelMap格式图如何进行页面流转
286浏览 • 1回复 待解决
对指定url的图片进行下载保存
2067浏览 • 1回复 待解决
HarmonyOS pixelMap
756浏览 • 1回复 待解决
HarmonyOS PixelMap裁剪
955浏览 • 1回复 待解决
HarmonyOS SVG转换pixelMap
654浏览 • 1回复 待解决
HarmonyOS PixelMap问题
642浏览 • 1回复 待解决
恭喜您,今日已阅读两篇内容,特奖励+2声望, 快来领取吧。