HarmonyOS 鸿蒙Next 图片路径(string类型)如何转为image.PixelMap图片对象
HarmonyOS 鸿蒙Next 图片路径(string类型)如何转为image.PixelMap图片对象 如下使用@ohos.file.photoAccessHelper (相册管理模块) 获取到预选择图片的uri数据。是Array<string>类型
请问如何把这个string类型的图片地址转为PixelMap类型?
photoViewPicker.select(photoSelectOptions).then((photoSelectResult) => {
this.imgUrl = photoSelectResult.photoUris[0];
console.info('photoViewPicker.select to file succeed and uris are:' + this.imgUrl);
}).catch((err) => {
console.error(`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
})
更多关于HarmonyOS 鸿蒙Next 图片路径(string类型)如何转为image.PixelMap图片对象的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
您好可以尝试如下方式
```javascript
//打开文件
let file = fs.openSync(选择的uri, fs.OpenMode.READ_ONLY);
//读取文件大小
let info = fs.statSync(file.fd);
//缓存照片数据
let bufferImg: ArrayBuffer = new ArrayBuffer(info.size);
//写入缓存
fs.readSync(file.fd, bufferImg);
//关闭文件流
fs.closeSync(file);
//创建imageResource
let imageResource = image.createImageSource(bufferImg);
//创建pixelMap
let picture = await imageResource.createPixelMap();
更多关于HarmonyOS 鸿蒙Next 图片路径(string类型)如何转为image.PixelMap图片对象的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
利用picker从相册选择图片、使用fs拷贝路径后通过image.createImageSource与imageSource.createPixelMap得到image.PixelMap
参考:https://developer.huawei.com/consumer/cn/blog/topic/03152951769987011
哇塞,厉害👍,
string类型?string是指路径吧?
项目名称
- 项目状态: 已完成
- 项目周期: 2023年3月 - 2023年6月
- 项目描述: 这是一个在线教育平台,提供各种课程资源。
团队成员
- 张三
- 李四
- 王五
技术栈
- Python
- Django
- HTML
- CSS
- JavaScript
想实现的功能是在相册选择图片后,展示在页面并且可编辑
在HarmonyOS中,将图片路径(string类型)转换为image.PixelMap
图片对象,可以使用ImageSource
类。具体步骤如下:
- 使用
ImageSource.create
方法,传入图片路径字符串,创建ImageSource
对象。 - 调用
ImageSource.createPixelmap
方法,将ImageSource
对象转换为PixelMap
对象。
示例代码如下:
import image from '@ohos.multimedia.image';
let imagePath: string = "file:///path/to/your/image.jpg";
let imageSource: image.ImageSource = image.createImageSource(imagePath);
let pixelMap: image.PixelMap = await imageSource.createPixelmap();
这段代码首先通过createImageSource
方法创建了一个ImageSource
对象,然后通过createPixelmap
方法将其转换为PixelMap
对象。