中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
从相册获取图片、将图片数据转为Component中的byteBuffer
微信扫码分享
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%’) } }