鸿蒙媒体开发系列17——图片编码与EXIF处理

如果你也对鸿蒙开发感兴趣,加入“Harmony自习室”吧!扫描下方名片,关注公众号,公众号更新更快,同时也有更多学习资料和技术讨论群。

1、图片编码

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

图片的解码请参看鸿蒙媒体开发系列15——图片解码(PixcelMap)

编码步骤如下:

👉🏻 1)创建图像编码ImagePacker对象

// 导入相关模块包import image from '@ohos.multimedia.image';const imagePackerApi = image.createImagePacker();

👉🏻 2)设置编码输出流和编码参数

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

let packOpts = { format:"image/jpeg", quality:98 };

👉🏻 3)对图片进行编码,保存编码后的图片

编码支持两种对象:PixcelMap和ImageSource。

* 对PixcelMap对象编码代码如下:​​​​​​​

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

* 对ImageSource对象编码的代码如下:​​​​​​​

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

2、EXIF处理

EXIF(Exchangeable image file format)是专门为数码相机的照片设定的文件格式,可以记录数码照片的属性信息和拍摄数据。当前仅支持JPEG格式图片。

在图库等应用中,需要查看或修改数码照片的EXIF信息。由于摄像机的手动镜头的参数无法自动写入到EXIF信息中或者因为相机断电等原因经常会导致拍摄时间出错,这时候就需要手动修改错误的EXIF数据,即可使用本功能。

HarmonyOS目前仅支持对部分EXIF信息的查看和修改,具体支持的范围如下:

图片

步骤如下:

👉🏻 1)创建ImageSource图片源对象,获取图片​​​​​​​

// 导入相关模块包import image from '@ohos.multimedia.image';// 获取沙箱路径创建ImageSourceconst fd = ...; // 获取需要被处理的图片的fdconst imageSource = image.createImageSource(fd);

👉🏻 2)读取 / 编辑EXIF信息。​​​​​​​

// 读取EXIF信息,BitsPerSample为每个像素比特数imageSource.getImageProperty('BitsPerSample', (error, data) => {  if (error) {    console.error('Failed to get the value of the specified attribute key of the image.And the error is: ' + error);  } else {    console.info('Succeeded in getting the value of the specified attribute key of the image ' + data);  }})// 编辑EXIF信息imageSource.modifyImageProperty('ImageWidth', '120').then(() => {  const width = imageSource.getImageProperty("ImageWidth");  console.info('The new imageWidth is ' + width);})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值