HarmonyOS 拍照返回uri转bitmap问题

拍照返回 resultUri转换成bitmap 再转换base64 , 现在在Harmony OS中拍照返回uri 控件就可以直接显示了,怎么能把拍照返回的url先换bitmap ?

HarmonyOS
2024-08-09 16:00:21
6964浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

请参考以下代码案例:

@State pixel: string | undefined = undefined; 
async uri2PixelMap() { 
  try { 
    let pickerProfile: picker.PickerProfile = { 
      cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK 
    }; 
    let pickerResult: picker.PickerResult = await picker.pick(getContext(this), 
      [picker.PickerMediaType.PHOTO], pickerProfile); 
    console.error("相册返回值=" + pickerResult.resultUri); 
    console.log("the pick pickerResult is:" + JSON.stringify(pickerResult.resultUri)); 
    const tempath = await this.beginScan(pickerResult.resultUri); 
    console.log(tempath) 
    const imageSource: image.ImageSource = image.createImageSource(tempath); 
    const pixelMap: image.PixelMap = await imageSource.createPixelMap(); 
    const imagePackerApi: image.ImagePacker = image.createImagePacker(); 
    let packOpts: image.PackingOption = { format: "image/jpeg", quality: 98 }; 
    imagePackerApi.packing(pixelMap, packOpts) 
      .then((data: ArrayBuffer) => { 
        console.info('packing succeeded.'); 
        let bufferArr = new Uint8Array(data) 
        let help = new util.Base64Helper 
        this.pixel = help.encodeToStringSync(bufferArr) 
        console.log("base64 => " + this.pixel) 
      }).catch((error: BusinessError) => { 
      console.error('packing failed.'); 
    }) 
    // pixelMap.release(); 
  } catch (error) { 
    let err = error as BusinessError; 
    console.error(`the pick call failed. error code: ${err.code}`); 
  } 
} 
build() { 
  Row() { 
    Column() { 
      Button(this.message) 
        .fontSize(50) 
        .fontWeight(FontWeight.Bold) 
        .onClick(() => { 
          this.uri2PixelMap() 
        }) 
      Image('data:image/jpeg;base64,' + this.pixel) 
        .width("100%") 
        .height("70%") 
        .border({ width: 1, color: Color.Red }) 
    } 
    .width('100%') 
  } 
  .height('100%') 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
分享
微博
QQ
微信
回复
2024-08-09 18:02:17


相关问题
HarmonyOS 文件路径uri
1073浏览 • 1回复 待解决
HarmonyOS uri.port返回异常
516浏览 • 1回复 待解决
HarmonyOS 拍照无法确认返回
693浏览 • 1回复 待解决
HarmonyOS Uri问题
662浏览 • 1回复 待解决
HarmonyOS 拍照后的图片base64
698浏览 • 1回复 待解决
HarmonyOS uri编码解码问题
628浏览 • 1回复 待解决
HarmonyOS uri的解析问题
511浏览 • 1回复 待解决
HarmonyOS 图片PDF问题
640浏览 • 1回复 待解决
HarmonyOS 调用相机拍照问题
809浏览 • 1回复 待解决
HarmonyOS 拍照相关问题
835浏览 • 1回复 待解决
关于如何使用相机拍照模块拍照问题
2574浏览 • 0回复 待解决
HarmonyOS 应用测相关问题
658浏览 • 1回复 待解决
HarmonyOS TSArkTS用法问题
977浏览 • 1回复 待解决
HarmonyOS 关于JSON问题
770浏览 • 1回复 待解决
HarmonyOS 相机拍照镜头问题
628浏览 • 1回复 待解决
HarmonyOS px vp 单位转换问题
702浏览 • 1回复 待解决