鸿蒙5.0版开发:图片开发(ArkTS)-编辑图片EXIF信息

 往期鸿蒙全套实战文章必看:


编辑图片EXIF信息

图片工具当前主要提供图片EXIF信息的读取与编辑能力。

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

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

HarmonyOS目前仅支持对部分EXIF信息的查看和修改。

开发步骤

EXIF信息的读取与编辑相关API的详细介绍。

  1. 获取图片,创建图片源ImageSource。

    // 导入相关模块包
    import { image } from '@kit.ImageKit';
    
    // 获取沙箱路径创建ImageSource
    const fd : number = 0; // 获取需要被处理的图片的fd
    const imageSourceApi : image.ImageSource = image.createImageSource(fd);
  2. 读取、编辑EXIF信息。

    import { BusinessError } from '@kit.BasicServicesKit';
    // 读取EXIF信息,BitsPerSample为每个像素比特数
    let options : image.ImagePropertyOptions = { index: 0, defaultValue: '9999' }
    imageSourceApi.getImageProperty(image.PropertyKey.BITS_PER_SAMPLE, options).then((data : string) => {
        console.log('Succeeded in getting the value of the specified attribute key of the image.');
    }).catch((error : BusinessError) => {
        console.error('Failed to get the value of the specified attribute key of the image.');
    })
    
    // 编辑EXIF信息
    imageSourceApi.modifyImageProperty(image.PropertyKey.IMAGE_WIDTH, "120").then(() => {
        imageSourceApi.getImageProperty(image.PropertyKey.IMAGE_WIDTH).then((width : string) => {
            console.info('The new imageWidth is ' + width);
        }).catch((error : BusinessError) => {
            console.error('Failed to get the Image Width.');
        })
    }).catch((error : BusinessError) => {
        console.error('Failed to modify the Image Width');
    })

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值