harmony 鸿蒙图片编码

2023-06-24 浏览 (709)

图片编码

图片编码指将PixelMap编码成不同格式的存档图片(当前仅支持打包为JPEG、WebP 和 png 格式),用于后续处理,如保存、传输等。

开发步骤

图片编码相关API的详细介绍请参见:图片编码接口说明

  1. 创建图像编码ImagePacker对象。

    // 导入相关模块包
    import image from '@ohos.multimedia.image';
    
    const imagePackerApi = image.createImagePacker();
    
  2. 设置编码输出流和编码参数。

    format为图像的编码格式;quality为图像质量,范围从0-100,100为最佳质量。

    let packOpts : image.PackingOption = { format:"image/jpeg", quality:98 };
    
  3. 创建PixelMap对象或创建ImageSource对象。

  4. 进行图片编码,并保存编码后的图片。

    方法一:通过PixelMap进行编码。

    import {BusinessError} from '@ohos.base'
    imagePackerApi.packing(pixelMap, packOpts).then( (data : ArrayBuffer) => {
      // data 为打包获取到的文件流,写入文件保存即可得到一张图片
    }).catch((error : BusinessError) => { 
      console.error('Failed to pack the image. And the error is: ' + error); 
    })
    

    方法二:通过imageSource进行编码。

    import {BusinessError} from '@ohos.base'
    imagePackerApi.packing(imageSource, packOpts).then( (data : ArrayBuffer) => {
        // data 为打包获取到的文件流,写入文件保存即可得到一张图片
    }).catch((error : BusinessError) => { 
      console.error('Failed to pack the image. And the error is: ' + error); 
    })
    

你可能感兴趣的鸿蒙文章

harmony 鸿蒙媒体

harmony 鸿蒙开发音频通话功能

harmony 鸿蒙音频通话开发概述

harmony 鸿蒙音频解码

harmony 鸿蒙音效管理

harmony 鸿蒙音频编码

harmony 鸿蒙音频输入设备管理

harmony 鸿蒙音频输出设备管理

harmony 鸿蒙多音频播放的并发策略

harmony 鸿蒙音频播放开发概述

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/126c378bc2a04335a4afdc63c3501da5