一张图片如何压缩长宽和大小到指定值

一张图片如何压缩长宽和大小到指定值。

HarmonyOS
2024-09-10 11:11:56
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

请参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/image-transformation-0000001861886505-V5#ZH-CN_TOPIC_0000001881257777__开发步骤

async dealWithPic(targetWidth:number,targetHeight:number,targetPixelSize:number){ 
  let path=getContext().filesDir+'/pic.png' 
  // path为已获得的沙箱路径 
  const imageSource: image.ImageSource = image.createImageSource(path); 
  let decodingOptions:image.DecodingOptions={ 
    editable:true 
  } 
  //创建pixelMap 
  this.pixelMap = await imageSource.createPixelMap(decodingOptions); 
  console.log("6666661" + this.pixelMap.getPixelBytesNumber()); 
  //指定压缩宽、高、大小 
  // this.packingDetail(500, 500, 100) 
 
  if (this.pixelMap) { 
    console.info(" before scaling pixelmap." + this.pixelMap.getPixelBytesNumber()); 
    let imageInfo = await this.pixelMap.getImageInfo(); 
    let a = this.pixelMap 
    //计算压缩比 
    let scaleX: number = targetWidth / imageInfo.size.width; 
    let scaleY: number = targetHeight / imageInfo.size.height; 
    this.pixelMap.scale(scaleX, scaleY, (err: BusinessError) => { 
      if (err) { 
        console.error(" Failed to scale pixelmap."); 
        return; 
      } else { 
        console.log(" in scaling pixelmap." + a.getPixelBytesNumber()); 
      } 
    }); 
  } 
  let imagePackerApi = image.createImagePacker(); 
  let packOpts: image.PackingOption = { format: "image/jpeg", quality: 100 }; 
  imagePackerApi.packing(this.pixelMap, packOpts).then((data: ArrayBuffer) => { 
    console.info(" in 100 quality data" + data.byteLength) 
    this.compressPictures(data, targetPixelSize, 99); 
  }); 
}

压缩到指定大小:调用packing方法实现第一次压缩,调用递归算法,当压缩后的data ,小于等于压缩目标图片大小时将data写入本地沙箱并结束递归。

compressPictures(data?: ArrayBuffer, size?: number, quality?: number): void { 
  if (data && size && quality) { 
  const currentSize = data.byteLength / 1024 
  if (currentSize <= size || quality === 0) { 
  console.log("finally quallity is:" + ++quality) 
  let file = fileIo.openSync('', fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 
  fileIo.write(file.fd, data).then(async (writeLen) => { 
  console.info("write data to file succeed2 and size is:" + writeLen); 
  fileIo.closeSync(file); 
}).catch((err: BusinessError) => { 
  console.info("write data to file failed1 with error message: " + err.message + ", error code: " + err.code); 
}); 
return; 
} 
} 
if (quality) { 
  let packOpts: image.PackingOption = { format: "image/jpeg", quality: quality } 
  let imagePackerApi = image.createImagePacker(); 
  imagePackerApi.packing(this.pixelMap, packOpts) 
    .then((ret: ArrayBuffer) => { 
      //递归算法,直到压缩到指定大小的data 
      if (quality) { 
        console.info("PACKING66666"+quality) 
        return this.compressPictures(ret, size, quality - 1) 
      } 
    }).catch((err: BusinessError) => { 
    console.log(`ok`) 
    return false 
  }) 
}
分享
微博
QQ
微信
回复
2024-09-10 17:12:51
相关问题
如何吸取一张图片的色
297浏览 • 1回复 待解决
HarmonyOS图片压缩不到指定大小
474浏览 • 1回复 待解决
如何图片PixelMap压缩指定大小
1736浏览 • 1回复 待解决
如何一张图片转化为PixelMapElement
9850浏览 • 1回复 待解决
图片压缩指定宽高和限制大小
818浏览 • 1回复 待解决
如何保存一张PNG图片到相册中
1737浏览 • 1回复 待解决
如何将PixelMap压缩指定大小
898浏览 • 1回复 待解决