HarmonyOS 图片压缩和图片转base64

​有个选择图片的需求,选择图片我用的PhotoViewPicker,返回的uri,拿到uri之后,我还需要做图片压缩和图片转base64

1、图片压缩

2、图片转base64​


HarmonyOS
2024-11-22 11:01:22
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit
async aboutToAppear(): Promise<void> { 
  const resourceManager = getContext(this).resourceManager 
  const imageArray = await resourceManager.getMediaContent($r('app.media.100')); 
  ​ 
  console.log("yuv_path is:" + path); 
  let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 
  ​ 
  fs.write(file.fd, imageArray.buffer).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); 
}); 
} 
2.创建pixelMap,并调用压缩方法 
 
  / // path为已获得的沙箱路径 
  const imageSource: image.ImageSource = image.createImageSource(path); 
​ 
//创建pixelMap 
this.pixelMap = await imageSource.createPixelMap(decodingOptions); 
​ 
console.log("6666661" + this.pixelMap.getPixelBytesNumber()); 
//指定压缩宽、高、大小 
this.packingDetail(500, 500, 100) 
3.根据压缩后目标图片宽高计算缩放比 并进行缩放,调用packing方法实现第一次压缩 
 
if (this.pixelMap) { 
  Logger.info("tag 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("tag Failed to scale pixelmap."); 
      return; 
    } else { 
      console.log("tag 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) => { 
  Logger.info("tag in 100 quality data" + data.byteLength) 
  this.compressPictures(data, targetPixelSize, 99); 
}); 
4.调用递归算法,当压缩后的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 = fs.openSync(path9, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 
  fs.write(file.fd, data).then(async (writeLen) => { 
  console.info("write data to file succeed2 and size is:" + writeLen); 
  fs.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) { 
        Logger.info("PACKING66666"+quality) 
        return this.compressPictures(ret, size, quality - 1) 
      } 
      ​ 
    }).catch((err: BusinessError) => { 
    console.log(`111`) 
    return false 
  }) 
} 
}
分享
微博
QQ
微信
回复
2024-11-22 15:57:36
相关问题
HarmonyOS 图片base64
60浏览 • 1回复 待解决
怎么把PixelMap图片BASE64
9439浏览 • 1回复 待解决
HarmonyOS如何将图片Base64
918浏览 • 1回复 待解决
js canvasbase64编码图片不全
10810浏览 • 1回复 待解决
如何base64换成图片
10329浏览 • 2回复 待解决
HarmonyOS 图片Base64编码
55浏览 • 1回复 待解决
HarmonyOS base64image
61浏览 • 1回复 待解决
HarmonyOS 图片进行base64编码报错
699浏览 • 1回复 待解决
HarmonyOS arraybufferbase64乱码
161浏览 • 1回复 待解决
如何将PixelMap图片base64字符串?
539浏览 • 1回复 待解决
HarmonyOS Image如何显示base64图片
472浏览 • 1回复 待解决
HarmonyOS 关于Image组件使用base64图片
112浏览 • 1回复 待解决
如何将图片base64字符串PixelMap?
612浏览 • 1回复 待解决
HarmonyOS 图片操作,合成后导出base64
228浏览 • 1回复 待解决
PixelMap怎么Base64?(非Java)
3349浏览 • 2回复 待解决
HarmonyOS 如何将base64数据转换为图片
518浏览 • 1回复 待解决
HarmonyOS 图片转为base64字符串
194浏览 • 1回复 待解决
HarmonyOS 网络传输BASE64义问题
44浏览 • 1回复 待解决
验证pixelmap数据bufferbase64是否正常
1841浏览 • 1回复 待解决