可以将图片转二进制数组转base64进行传输到客服端/服务端再进行base64转图片,也可以将二进制进行加解密的业务操作。在服务器加密,客户端解密再将base64转成图片。图片转PixelMap示例:import photoAccessHelper from '@ohos.file.photoAccessHelper'; import image from '@ohos.multimedia.image'; import fs from '@ohos.file.fs'; import { buffer } from '@kit.ArkTS'; @Entry @Component struct Page2 { @State resultBase64Str: string = ''; @State getAlbum: string = '显示相册中的图片'; @State pixel: image.PixelMap | undefined = undefined; @State albumPath: string = ''; @State photoSize: number = 0; async getPictureFromAlbum() { // 拉起相册,选择图片 let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions(); PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE; PhotoSelectOptions.maxSelectNumber = 1; let photoPicker = new photoAccessHelper.PhotoViewPicker(); let photoSelectResult: photoAccessHelper.PhotoSelectResult = await photoPicker.select(PhotoSelectOptions); this.albumPath = photoSelectResult.photoUris[0]; console.info('albumPath: ' + this.albumPath) // 读取图片为buffer const file = fs.openSync(this.albumPath, fs.OpenMode.READ_WRITE); this.photoSize = fs.statSync(file.fd).size; console.info('Photo Size: ' + this.photoSize); let buffer1 = new ArrayBuffer(this.photoSize); fs.readSync(file.fd, buffer1); let base64Str: string = buffer.from(buffer1).toString('base64') let resultBase64Str = "data:image/png;base64," + base64Str this.resultBase64Str = resultBase64Str fs.closeSync(file); // 解码成PixelMap const imageSource = image.createImageSource(buffer1); console.log('imageSource: ' + JSON.stringify(imageSource)); this.pixel = await imageSource.createPixelMap({}); } build() { Scroll() { Column() { Text('获取图片') .onClick(async () => { await this.getPictureFromAlbum() }) // Image(this.pixel) Image(this.resultBase64Str).width(40).height(40) Text(this.resultBase64Str) } } } }base64转图片的demo:import { util } from '@kit.ArkTS'; import fs from '@ohos.file.fs'; import { BusinessError } from '@kit.BasicServicesKit'; const base64String = 'xxxx'; @Entry @Component struct Index { @State imagestr: string = '' base64Image() { let begin = 'data:image/jpeg;base64,' return begin + base64String } aboutToAppear(): void { this.imagestr = this.base64Image() } build() { Row() { Column() { Image(this.imagestr) .width(300) .height(100) .backgroundColor(Color.Green) Button("保存图片") .onClick(() => { this.picture() }) } .width('100%') } .height('100%') } picture() { let that = new util.Base64Helper(); let result: Uint8Array = that.decodeSync(base64String, util.Type.MIME); let buf: ArrayBuffer = result.buffer as ArrayBuffer const context: Context = getContext(this); const path1: string = context.cacheDir + "/pixel_map1169.jpg"; let file = fs.openSync(path1, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); fs.write(file.fd, buf).then(async (writeLen) => { console.info("write data to file succeed and size is:" + writeLen); fs.closeSync(file); }).catch((err: BusinessError) => { console.info("write data to file failed with error message: " + err.message + ", error code: " + err.code); }); } }base64转码参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-util-V5\#base64helper9加解密参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/crypto-encryption-decryption-overview-V5图片api指南:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-image-V5
可以将图片转二进制数组转base64进行传输到客服端/服务端再进行base64转图片,也可以将二进制进行加解密的业务操作。在服务器加密,客户端解密再将base64转成图片。
图片转PixelMap示例:
base64转图片的demo:
base64转码参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-util-V5\#base64helper9
加解密参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/crypto-encryption-decryption-overview-V5
图片api指南:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-image-V5