HarmonyOS 鸿蒙Next 图片路径(string类型)如何转为image.PixelMap图片对象

发布于 1周前 作者 h691938207 来自 鸿蒙OS

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

8 回复
您好可以尝试如下方式

```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类。具体步骤如下:

  1. 使用ImageSource.create方法,传入图片路径字符串,创建ImageSource对象。
  2. 调用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对象。

在HarmonyOS中,可以通过ImageSource类将图片路径转换为PixelMap对象。首先使用ImageSource.create方法创建ImageSource实例,然后调用createPixelmap方法生成PixelMap对象。示例代码如下:

String imagePath = "your_image_path";
ImageSource imageSource = ImageSource.create(imagePath, null);
PixelMap pixelMap = imageSource.createPixelmap(null);

确保路径正确且图片存在,否则会抛出异常。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!