HarmonyOS 如何将拍照或者相册的图片压缩并转化base64格式?

HarmonyOS 如何将拍照或者相册的图片压缩并转化base64格式?

HarmonyOS
4天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

这个demo是拉起相机然后拍照保存到指定的目录,然后转为base64的整个流程,请参考;

import picker from '@ohos.multimedia.cameraPicker' 
import camera from '@ohos.multimedia.camera'; 
import common from '@ohos.app.ability.common'; 
import { BusinessError } from '@ohos.base'; 
import fileuri from '@ohos.file.fileuri'; 
import fs from '@ohos.file.fs'; 
import { buffer } from '@kit.ArkTS'; 
import { image } from '@kit.ImageKit'; 
 
let mContext = getContext(this) as common.Context; 
class CameraPosition { 
  cameraPosition : camera.CameraPosition 
  saveUri :string 
  constructor(cameraPosition : camera.CameraPosition,saveUri:string) { 
    this.cameraPosition = cameraPosition 
    this.saveUri = saveUri 
  } 
} 
let pathDir = getContext().filesDir; 
let filePath = pathDir + `/${new Date().getTime()}.jpg` 
fs.createRandomAccessFileSync(filePath, fs.OpenMode.CREATE); 
let uri = fileuri.getUriFromPath(filePath); 
async function demo() { 
  try { 
    let pickerProfile = new CameraPosition(camera.CameraPosition.CAMERA_POSITION_BACK,uri) 
    //前置摄像机传CAMERA_POSITION_FRONT,后置摄像机传CAMERA_POSITION_BACK,saveuri传想存到对应沙箱的uri 
    let pickerResult: picker.PickerResult = await picker.pick(mContext, [picker.PickerMediaType.PHOTO, picker.PickerMediaType.VIDEO], pickerProfile); 
    console.log("the pick pickerResult is:" + JSON.stringify(pickerResult)); 
  } catch (error) { 
    let err = error as BusinessError; 
    console.error(`the pick call failed. error code: ${err.code}`); 
  } 
} 
 
@Entry 
@Component struct Photopage{ 
  @State base64Str:string = '' 
  async getImageBase64WithUri(uri:string): Promise<string> { 
    const file = await fs.open(uri, fs.OpenMode.READ_ONLY); 
    const imageSource : image.ImageSource = image.createImageSource(file.fd); 
    const imagePackerApi = image.createImagePacker(); 
    // 设置打包参数 
    // format:当前仅支持打包为JPEG、WebP 和 png 格式 
    // quality:JPEG 编码输出图片质量 
    // bufferSize:图片大小,默认 10M 
    const packOpts: image.PackingOption = { format: "image/jpeg", quality: 100 }; 
    let imageBuffer: ArrayBuffer = new ArrayBuffer(1); 
    let resultBase64Str = '' 
    try { 
      // 图片压缩或重新打包 
      imageBuffer = await imagePackerApi.packing(imageSource, packOpts); 
      let base64Str = buffer.from(imageBuffer).toString('base64') 
      resultBase64Str="data:image/jpeg;base64,"+base64Str 
 
    } catch (err) { 
      console.error(`Invoke getImageArrayBufferWithUri failed, err: ${JSON.stringify(err)}`); 
    } 
    return resultBase64Str; 
  } 
 
  build(){ 
    Row(){ 
      Column(){ 
        Button('拉起后置摄像头').margin({bottom:20}).onClick(()=>{ demo() }) 
        Button('uri转为base64').onClick(async ()=>{ 
          this.base64Str = await this.getImageBase64WithUri(uri) 
        }) 
        Image(this.base64Str) 
          .width(300) 
          .height(400) 
      } 
    } 
 
  } 
}
分享
微博
QQ
微信
回复
4天前
相关问题
HarmonyOS如何将图片Base64
747浏览 • 1回复 待解决
HarmonyOS 如何将base64数据转换为图片
371浏览 • 1回复 待解决
如何将图片base64字符串转PixelMap?
442浏览 • 1回复 待解决
如何将PixelMap转图片base64字符串?
353浏览 • 1回复 待解决
HarmonyOS Image如何显示base64图片
271浏览 • 1回复 待解决
有没有base64转化相关代码?
679浏览 • 0回复 待解决
HarmonyOS 图片进行base64编码报错
487浏览 • 1回复 待解决
如何base64转换成图片
10090浏览 • 2回复 待解决
HarmonyOS 图片操作,合成后导出base64
43浏览 • 1回复 待解决
怎么把PixelMap图片BASE64
9201浏览 • 1回复 待解决
base64字符串保存为图片方法
597浏览 • 1回复 待解决
HarmonyOS base64编码API
82浏览 • 1回复 待解决
HarmonyOS base64编码问题
341浏览 • 1回复 待解决
HarmonyOS base64解码报错
606浏览 • 1回复 待解决
js canvas转base64编码图片不全
10565浏览 • 1回复 待解决
HarmonyOS 图片转为base64字符串
54浏览 • 1回复 待解决