当前位置: 首页 > news >正文

HarmonyOS Next应用开发——多种方式实现图片解码

【高心星出品】

图片解码

图片处理就是将设备中保存的图片进行编辑处理然后再存储下来,整个过程需要先图片解码,图片处理,最后在图片编码保存。

图片解码指将所支持格式的存档图片解码成统一的PixelMap,以便在应用或系统中进行图片显示或图片处理。当前支持的存档图片格式包括JPEG、PNG、GIF、WebP、BMP、SVG、ICO、DNG、HEIF(不同硬件设备支持情况不同)。

网络图片解码

通过网络请求获取网络图片的字节,生成ImageSource然后生成PixelMap。

在这里插入图片描述

获取图片
// 创建请求对象
let req = http.createHttp()
req.request('https://pic.616pic.com/ys_img/01/17/47/eVnQ1JdIY4.jpg').then((res) => {// 获取图片字节let buffer = res.result as ArrayBuffer//   生成ImageSourcelet source = image.createImageSource(buffer)// 转化成pixelmapthis.src = source.createPixelMapSync()
完整代码
import { http } from '@kit.NetworkKit';
import { image } from '@kit.ImageKit';@Entry
@Component
struct InternetPage {@State message: string = 'Hello World';@State src: PixelMap | undefined = undefinedbuild() {Column() {Image(this.src).width(150).height(150).objectFit(ImageFit.Fill).margin({ top: 20 }).border({ width: 2, color: Color.Black })Button('加载图片').margin({ top: 20 }).onClick(() => {// 创建请求对象let req = http.createHttp()req.request('https://pic.616pic.com/ys_img/01/17/47/eVnQ1JdIY4.jpg').then((res) => {// 获取图片字节let buffer = res.result as ArrayBuffer//   生成ImageSourcelet source = image.createImageSource(buffer)// 转化成pixelmapthis.src = source.createPixelMapSync()})})}.height('100%').width('100%')}
}
应用内图片解码

通过获取应用沙箱中文件的描述信息,生成ImageSource然后生成PixelMap。

效果图同上。

获取图片
//   选择图片
let file = fileIo.openSync(getContext(this).getApplicationContext().cacheDir + '/test.jpg',fileIo.OpenMode.READ_ONLY)
// 创建imagesource
let source = image.createImageSource(file.fd)
//  获得pixelmap
this.src = source.createPixelMapSync()
完整代码
import { http } from '@kit.NetworkKit';
import { fileIo } from '@kit.CoreFileKit';
import { image } from '@kit.ImageKit';@Entry
@Component
struct Index {@State message: string = 'Hello World';@State src: PixelMap | undefined = undefinedaboutToAppear(): void {// 下载一个文件保存在cache中let req = http.createHttp()req.request('https://pic.616pic.com/ys_img/01/17/47/eVnQ1JdIY4.jpg').then((res) => {let buffer = res.result as ArrayBufferlet file = fileIo.openSync(getContext(this).getApplicationContext().cacheDir + '/test.jpg',fileIo.OpenMode.CREATE | fileIo.OpenMode.WRITE_ONLY)fileIo.writeSync(file.fd, buffer)fileIo.close(file)})}build() {Column() {Image(this.src).width(150).height(150).objectFit(ImageFit.Fill).margin({ top: 20 }).border({width:2,color:Color.Black})Button('加载图片').margin({ top: 20 }).onClick(() => {//   选择图片let file = fileIo.openSync(getContext(this).getApplicationContext().cacheDir + '/test.jpg',fileIo.OpenMode.READ_ONLY)// 创建imagesourcelet source = image.createImageSource(file.fd)//  获得pixelmapthis.src = source.createPixelMapSync()})}.height('100%').width('100%')}
}
应用资源包图片解码

假设在应用的rawfile中存放着一张图片,我们使用方法可以将其转化为PixelMap对象。

运行效果同上。

获取图片
//  获取资源管理器
let resoucemanager = getContext(this).resourceManager
//  获取rawfile中图片的描述符
let fd = resoucemanager.getRawFdSync('head.jpg')
// 根据文件描述符创建imagesource
let source = image.createImageSource(fd)
// 创建pixelmap
this.src = source.createPixelMapSync()
完整代码
import { image } from '@kit.ImageKit';@Entry
@Component
struct ResourcePage {@State message: string = 'Hello World';@State src: PixelMap | undefined = undefinedbuild() {Column() {Image(this.src).width(150).height(150).objectFit(ImageFit.Fill).margin({ top: 20 }).border({ width: 2, color: Color.Black })Button('加载图片').margin({ top: 20 }).onClick(() => {//  获取资源管理器let resoucemanager = getContext(this).resourceManager//  获取rawfile中图片的描述符let fd = resoucemanager.getRawFdSync('head.jpg')// 根据文件描述符创建imagesourcelet source = image.createImageSource(fd)// 创建pixelmapthis.src = source.createPixelMapSync()})}.height('100%').width('100%')}
}
用户图库图片解码

可以从用户相册中选择图片解码,由于鸿蒙做了用户和应用的隔离机制,所以图库选择图片较为复杂。

在这里插入图片描述

获取图片
//   创建图片选择器
let photopicker = new photoAccessHelper.PhotoViewPicker()
// 选择图片的配置  最多选择一张图片,支持编辑
let selectoption: photoAccessHelper.PhotoSelectOptions = {maxSelectNumber: 1,isEditSupported: true
}
photopicker.select(selectoption).then((value) => {// 获取相册图片uri 注意此处uri不能直接使用 它是用户urilet uri = value.photoUris[0]//   获取该uri对应的文件let file = fileIo.openSync(uri, fileIo.OpenMode.READ_ONLY)//  生成sourcelet source = image.createImageSource(file.fd)//   获取pixelmap对象this.src = source.createPixelMapSync()
完整代码
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { fileIo } from '@kit.CoreFileKit';
import { image } from '@kit.ImageKit';@Entry
@Component
struct UserPage {@State message: string = 'Hello World';@State src: PixelMap | undefined = undefinedbuild() {Column() {Image(this.src).width(150).height(150).objectFit(ImageFit.Fill).margin({ top: 20 }).border({ width: 2, color: Color.Black })Button('加载图片').margin({ top: 20 }).onClick(() => {//   创建图片选择器let photopicker = new photoAccessHelper.PhotoViewPicker()// 选择图片的配置  最多选择一张图片,支持编辑let selectoption: photoAccessHelper.PhotoSelectOptions = {maxSelectNumber: 1,isEditSupported: true}photopicker.select(selectoption).then((value) => {// 获取相册图片uri 注意此处uri不能直接使用 它是用户urilet uri = value.photoUris[0]//   获取该uri对应的文件,这里要只读打开否则无权限在let file = fileIo.openSync(uri, fileIo.OpenMode.READ_ONLY)//  生成sourcelet source = image.createImageSource(file.fd)//   获取pixelmap对象this.src = source.createPixelMapSync()})})}.height('100%').width('100%')}
}

http://www.mrgr.cn/news/55446.html

相关文章:

  • lazyLoad
  • 数据治理(1)-数据规划
  • 求刚体移动后的转换矩阵
  • java计算两个日期之间相差几天(小时、天)小时计算
  • 有同学问:拿到大厂JAVA OFFER,但是会不会不稳定,有失业风险?!
  • Java的魔法世界:面向对象编程(OOP)是什么?
  • 【论文精读】把一切转成mesh!MeshAnything和MeshAnythingV2论文解析
  • 挖掘 M2 Pro 32G UMA 内存潜力:在 Mac 上本地运行清华大模型 ChatGLM2-6B
  • 云服务器遭受攻击后的应急响应与解决策略
  • 【前端Vue学习笔记】组件注册方式 组件传递数据 组件事件 透传 插槽slot 组件生命周期 动态组件 异步组件 依赖注入 Vue应用
  • leetcode动态规划(八)-不同的二叉搜索树
  • 生信学院|10月22日《SOLIDWORKS 自定义属性卡片应用》
  • React第十一章(useReducer)
  • 如何解决企业异地办公网络难题?
  • 持续输出面试题系列之SpringCloud篇
  • 数造科技荣获2024DAMA中国“数据治理创新奖”
  • 4.1粒子系统
  • C++游戏开发入门:用 SDL 实现你的第一个 2D 游戏
  • 汕头市自闭症全托管学校,为孩子打开未来大门
  • consumer 角度讲一下i2c外设
  • 实现了通过摄像头检测手部手势来控制 B 站视频播放的功能。它使用了 OpenCV 进行视频捕获和图像处理,MediaPipe 进行手部检测和关键点识别
  • 五台山景点购票系统——后附计算机源码
  • Windows下搭建VUE开发环境
  • 87 VRRPV2/V3 综合技术实操
  • 冒泡排序(Python)
  • 用Python制作《我的世界》风格小游戏:入门指南