HarmonyOS 图片编码打包报错

HarmonyOS 图片编码打包报错。

HarmonyOS
2024-11-12 10:17:12
浏览
收藏 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
微信
回复
2024-11-12 15:13:05
相关问题
HarmonyOS 图片进行base64编码报错
636浏览 • 1回复 待解决
编码器的bufferQueue报错
114浏览 • 0回复 待解决
HarmonyOS 视频编码surface模式运行报错
316浏览 • 1回复 待解决
HarmonyOS 打包签名失败报错code:109
29浏览 • 1回复 待解决
鸿蒙打包app报错,想打包app文件发布
8771浏览 • 4回复 已解决
js canvas转base64编码图片不全
10744浏览 • 1回复 待解决
HarmonyOS 相册保存图片设置title报错
340浏览 • 1回复 待解决
HarmonyOS URL特殊字符编码
648浏览 • 1回复 待解决
HarmonyOS获取图片旋转值一直报错
424浏览 • 1回复 待解决
HarmonyOS Image组件加载在线图片报错
131浏览 • 1回复 待解决
HarmonyOS TLV格式编码和解码库
278浏览 • 1回复 待解决
HarmonyOS TextEncoder支持ISO-8859-1编码
407浏览 • 1回复 待解决
HarmonyOS module打包问题
10浏览 • 1回复 待解决
HarmonyOS base64编码问题
597浏览 • 1回复 待解决
HarmonyOS 单测打包执行
419浏览 • 1回复 待解决