如何将相册返回的file://xxx/xxx/xx.jpg转化为ImageSource

从相册获取图片、将图片数据转为Component中的byteBuffer

HarmonyOS
2024-05-08 23:18:47
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
开心的兔子
import { BusinessError } from ‘@ohos.base’; 
import photoAccessHelper from ‘@ohos.file.photoAccessHelper’; 
import image from ‘@ohos.multimedia.image’; 
import fs from ‘@ohos.file.fs’; 
import { fileIO } from ‘@kit.CoreFileKit’; 
 
@Entry 
@Component 
struct buttonTestDemo { 
  @State txt: string = ‘拉起图库选择图片’; 
  @State widthShortSize: number = 200; 
  @State num:number = 0 
  @State pexel:image.PixelMap | undefined = undefined 
  //创建pixelmap对象 
  async Demo1() { 
const file = fs.openSync(this.aa, fs.OpenMode.READ_WRITE); 
console.log(‘11111111111fd’+file.fd) 
 
this.num = fs.statSync(file.fd).size; 
console.log("11111111111statSync"+ this.num) 
let buf = new ArrayBuffer(this.num); 
console.log('1111111test' + fs.readSync(file.fd, buf)); 
fs.closeSync(file); 
 
const imageSource = image.createImageSource(buf); 
console.log('11111111souce' + JSON.stringify(imageSource)) 
const pixelMap = await imageSource.createPixelMap({}) 
this.pexel = pixelMap 
console.log('111111111pixelMap' + JSON.stringify(pixelMap)) 
const readBuffer: ArrayBuffer = new ArrayBuffer(pixelMap.getPixelBytesNumber()) 
console.log('111111111ArrayBuffer' + JSON.stringify(pixelMap.getPixelBytesNumber())) 
const ccc =  await pixelMap.readPixelsToBuffer(readBuffer) 
// 本地测试 传入上面readBuffer 保存到本地 通过PixelViewer查看图片 
let context = getContext(this) 
let filedata = context.filesDir 
let filename = “test222” 
let filePath = filedata + “/” + filename + “.jpg” 
 
console.log('savePicture packing in path = ' + filePath) 
let fd = fileIO.openSync(filePath, 0o100 | 0o2, 0o666) 
fileIO.write(fd, readBuffer).then(number => { 
  console.log("savePicture write data to file succeed and size is:" + number) 
}).catch((err : BusinessError) => { 
  console.log("savePicture write data to file failed with error:" + err) 
}); 
console.info('save image done') 
} 
  // 拉起图库获取公共目录媒体类路径 
  @State aa:string = ‘’ 
  async example01() { 
    try { 
      let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions();x000D 
      /******/x000D 
      PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE; 
      PhotoSelectOptions.maxSelectNumber = 5; 
      let photoPicker = new photoAccessHelper.PhotoViewPicker(); 
      photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult: photoAccessHelper.PhotoSelectResult) => { 
console.info('c, PhotoSelectResult uri: ’ + JSON.stringify(PhotoSelectResult)); 
console.log(‘1111111111uri’ + JSON.stringify(PhotoSelectResult.photoUris[0])) 
this.aa = PhotoSelectResult.photoUris[0] 
}).catch((err: BusinessError) => { 
console.error('PhotoViewPicker.select failed with err: ’ + JSON.stringify(err)); 
}); 
    } catch (error) { 
let err: BusinessError = error as BusinessError; 
console.error('PhotoViewPicker failed with err: ’ + JSON.stringify(err)); 
} 
  } 
 
  build() { 
    Row() { 
      Column() { 
Button(this.txt) 
.width(this.widthShortSize) 
.height(100) 
.fontSize(40) 
.onClick(()=>{ 
this.example01() 
}) 
Image(this.aa) 
.width(100).height(100) 
Button(‘piexlmap对象转换’) 
.width(this.widthShortSize) 
.height(100) 
.fontSize(40) 
.onClick(()=>{ 
this.Demo1() 
}) 
Text(‘图片展示pixel对象创建成功’) 
Image(this.pexel) 
.width(100).height(100) 
} 
      .width(‘100%’) 
    } 
    .height(‘100%’) 
  } 
} 
分享
微博
QQ
微信
回复
2024-05-09 12:22:03
相关问题
如何将一张图片转化为PixelMapElement
10023浏览 • 1回复 待解决
指定页面区域转化为图片
1986浏览 • 1回复 待解决