HarmonyOS 图片编码打包报错

HarmonyOS 图片编码打包报错。

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

请参考如下demo:

import { photoAccessHelper } from '@kit.MediaLibraryKit'; 
import { image } from '@kit.ImageKit'; 
import fs from '@ohos.file.fs'; 
import { buffer } from '@kit.ArkTS'; 
 
@Entry 
@Component 
struct Index { 
  @State imagePixMap: string = ''; 
  build() { 
    Row() { 
      Column() { 
        Image(this.imagePixMap) 
          .height(300).width(300) 
        Button('选择图片') 
          .onClick(()=>{ 
            this.selectPhoto1() 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
 
  selectPhoto1(){ 
    const photoSelectOptions = new photoAccessHelper.PhotoSelectOptions(); 
    photoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE; 
    photoSelectOptions.maxSelectNumber = 1; 
    photoSelectOptions.isEditSupported=false 
    photoSelectOptions.isPhotoTakingSupported=false 
    let uris: Array<string> = []; 
    const photoViewPicker = new photoAccessHelper.PhotoViewPicker(); 
    photoViewPicker.select(photoSelectOptions).then(async (photoSelectResult: photoAccessHelper.PhotoSelectResult) => { 
 
      uris = photoSelectResult.photoUris; 
      if(uris && uris.length>0){ 
        this.imagePixMap = await this.getImageBase64WithUri(uris[0]) 
        console.debug('image Base64 = ' + this.imagePixMap ) 
      } 
    }) 
  } 
 
  async getImageBase64WithUri(uri:string): Promise<string> { 
    const file = fs.openSync(uri, fs.OpenMode.READ_ONLY); 
    const imageSource : image.ImageSource = image.createImageSource(file.fd); 
    const imagePackerApi = image.createImagePacker(); 
    let pixelMap = await imageSource.createPixelMap({ editable: true }); 
    let imgInfo=await pixelMap?.getImageInfo() 
    let width=imgInfo?.size.width; 
    let height=imgInfo?.size.height; 
    console.debug('width = ' + width + '   height = '+ height) 
    let  cropWidth = height 
    let  cropHeight = height 
    let  x = height 
    let  y = height 
    if(width>height){ 
      cropWidth = height 
      cropHeight = height 
      x = Math.round((width - height) / 2) 
      y = 0 
    }else{ 
      cropWidth = width 
      cropHeight = width 
      x = 0 
      y = Math.round((height - width) / 2) 
 
    } 
    await pixelMap.crop({ 
      size: { 
        width: cropWidth, 
        height: cropHeight 
      }, 
      x: x, 
      y: y 
    }) 
 
 
    const packOpts: image.PackingOption = { format: "image/jpeg", quality: 100 }; 
    let imageBuffer: ArrayBuffer = new ArrayBuffer(1); 
    let resultBase64Str = '' 
    try { 
      // 图片压缩或重新打包 
      imageBuffer = await imagePackerApi.packing(pixelMap, 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; 
  } 
 
}
分享
微博
QQ
微信
回复
5天前
相关问题
HarmonyOS 图片进行base64编码报错
491浏览 • 1回复 待解决
HarmonyOS 视频编码surface模式运行报错
243浏览 • 1回复 待解决
鸿蒙打包app报错,想打包app文件发布
8685浏览 • 4回复 已解决
js canvas转base64编码图片不全
10569浏览 • 1回复 待解决
HarmonyOS 相册保存图片设置title报错
262浏览 • 1回复 待解决
HarmonyOS URL特殊字符编码
513浏览 • 1回复 待解决
HarmonyOS获取图片旋转值一直报错
329浏览 • 1回复 待解决
HarmonyOS TLV格式编码和解码库
198浏览 • 1回复 待解决
HarmonyOS base64编码问题
351浏览 • 1回复 待解决
HarmonyOS 单测打包执行
304浏览 • 1回复 待解决
HarmonyOS base64编码的API
84浏览 • 1回复 待解决
HarmonyOS 打包或者上架相关
313浏览 • 1回复 待解决
HarmonyOS 引入RN后打包失败
540浏览 • 1回复 待解决
HarmonyOS HAR包被重复打包问题
376浏览 • 1回复 待解决
HarmonyOS TextEncoder支持ISO-8859-1编码
330浏览 • 1回复 待解决
HarmonyOS har包重打包工具
356浏览 • 1回复 待解决