使用 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
4天前
浏览
收藏 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
微信
回复
4天前
相关问题
使用图片压缩API参数影响
331浏览 • 1回复 待解决
HarmonyOS视频压缩模块无法压缩视频
314浏览 • 1回复 待解决
网络请求:gzip压缩使用
292浏览 • 1回复 待解决
网络请求使用gzip压缩数据
1900浏览 • 1回复 待解决
打开相机:直接使用相机拍照能力
1481浏览 • 1回复 待解决
关于如何使用相机拍照模块拍照问题
1676浏览 • 0回复 待解决
HarmonyOS 视频压缩没有相关api
281浏览 • 1回复 待解决
是否存在系统内置视频压缩方案
291浏览 • 1回复 待解决
HarmonyOS APP无法打开相机拍照
296浏览 • 1回复 待解决
HarmonyOS gzip压缩和解压缩接口
402浏览 • 1回复 待解决