使用 HarmonyOS 拍照API拍的照片无法压缩,使用系统相机拍的照片可以压缩

​操作步骤:

1、调用API进行拍照。​

let pickerResult: cameraPicker.PickerResult = await cameraPicker.pick(getContext(this), 
  [cameraPicker.PickerMediaType.PHOTO], pickerProfile);

2、调用压缩API进行压缩。

static packing = (uri: string, bufferSize: number): Promise<ArrayBuffer | null> => { 
  const context = getContext(this); 
  return new Promise(async (resolve, reject) => { 
    try { 
      let file = fs.openSync(uri); 
      console.info('file fd: ' + file.fd); 
      const imageSourceApi: image.ImageSource = image.createImageSource(file.fd); 
      let packOpts: image.PackingOption = { format: "image/jpeg", quality: 30, bufferSize } 
      const imagePackerApi: image.ImagePacker = image.createImagePacker(); 
      imagePackerApi.packing(imageSourceApi, packOpts) 
        .then((data: ArrayBuffer) => { 
          resolve(data) 
          console.info('packing succeeded.'); 
          fs.closeSync(file); 
        }).catch((error: BusinessError) => { 
        resolve(null) 
        console.error('packing failed.'); 
      }) 
        .finally(() => { 
          fs.closeSync(file); 
        }); 
    } catch (err) { 
      console.error('getAssets failed with err: ' + err); 
    } 
  }); 
}

结果:压缩报错。

HarmonyOS
2024-11-12 11:17:31
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

试一下下面的方案。

const photoViewPicker = new picker.PhotoViewPicker(); 
photoViewPicker.select().then(async (photoSelectResult: picker.PhotoSelectResult) => { 
  const uris = photoSelectResult.photoUris; 
  console.info('photoViewPicker.select to file succeed and uris are:' + uris); 
  let file = fs.openSync(uris[0], fs.OpenMode.READ_ONLY); 
  console.info('file fd: ' + file.fd); 
 
  const imageSource: image.ImageSource = image.createImageSource(file.fd); 
  imageSource.getImageInfo(0) 
    .then((imageInfo: image.ImageInfo) => { 
      let a = imageInfo 
      console.log("11111111111111111111111111111"+" " + a.toString()) 
      console.info('Succeeded in obtaining the image information.'); 
    }).catch((error: BusinessError) => { 
    console.error('Failed to obtain the image information.'); 
  }) 
 
  let decodingOptions: image.DecodingOptions = { 
    editable: true, 
    desiredPixelFormat: 3, 
  } 
  this.pixmap= await imageSource.createPixelMap(decodingOptions) 
  let img:ImageBitmap = new ImageBitmap(this.pixmap) 
  const imagePackerApi = image.createImagePacker(); 
  let packOpts: image.PackingOption = { format: "image/jpeg", quality: 60 }; 
 
 
  imagePackerApi.packing(this.pixmap, packOpts).then((data: ArrayBuffer) => { 
    // data 为打包获取到的文件流,写入文件保存即可得到一张图片 
 
    console.log('packing succeeded.'); 
    const path = getContext(this).filesDir + "imagePng.jpeg"; 
    let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 
    fs.write(file.fd, data).then((writeLen) => { 
      console.info("write data to file succeed and size is:" + writeLen); 
      fs.closeSync(file); 
    }).catch((err: BusinessError) => { 
      console.info("write data to file failed with error message: " + err.message + ", error code: " + err.code); 
    });

如果还是不行,建议您这边升级一下版本。

分享
微博
QQ
微信
回复
2024-11-12 17:02:18
相关问题
HarmonyOS 关于照片压缩咨询
156浏览 • 1回复 待解决
HarmonyOS 相机拍照完,照片读取失败
124浏览 • 1回复 待解决
使用图片压缩API参数影响
740浏览 • 1回复 待解决
HarmonyOS视频压缩模块无法压缩视频
749浏览 • 1回复 待解决
HarmonyOS 相册/拍照 压缩 上传demo
196浏览 • 1回复 待解决
网络请求:gzip压缩使用
603浏览 • 1回复 待解决