1# 编辑图片EXIF信息 2<!--Kit: Image Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @aulight02--> 5<!--Designer: @liyang_bryan--> 6<!--Tester: @xchaosioda--> 7<!--Adviser: @zengyawen--> 8 9Image Kit提供图片EXIF信息的读取与编辑能力。 10 11EXIF(Exchangeable image file format)是专门为数码相机的照片设定的文件格式,可以记录数码照片的属性信息和拍摄数据。当前支持JPEG、PNG、HEIF格式,且需要图片包含EXIF信息。 12 13在图库等应用中,需要查看或修改数码照片的EXIF信息。由于摄像机的手动镜头参数无法自动写入到EXIF信息中或者因为相机断电等原因会导致拍摄时间出错,这时需要手动修改错误的EXIF数据,即可使用本功能。 14 15OpenHarmony目前仅支持对部分EXIF信息的查看和修改,具体支持的范围请参见:[Exif信息](../../reference/apis-image-kit/arkts-apis-image-e.md#propertykey7)。 16 17## 开发步骤 18 19EXIF信息的读取与编辑相关API的详细介绍请参见[API参考](../../reference/apis-image-kit/arkts-apis-image-ImageSource.md#getimageproperty11)。 20 21获取图片,创建ImageSource。读取、编辑EXIF信息。示例代码如下: 22 23```ts 24// 导入相关模块包。 25import { image } from '@kit.ImageKit'; 26import { BusinessError } from '@kit.BasicServicesKit'; 27 28// 获取沙箱路径创建ImageSource。 29const fd : number = 0; // 获取需要被处理的图片的fd。 30const imageSourceApi : image.ImageSource = image.createImageSource(fd); 31 32// 读取EXIF信息,BitsPerSample为每个像素比特数。 33let options : image.ImagePropertyOptions = { index: 0, defaultValue: 'This key has no value!' }; 34imageSourceApi.getImageProperty(image.PropertyKey.BITS_PER_SAMPLE, options).then((data : string) => { 35 console.info('Succeeded in getting the value of the specified attribute key of the image.'); 36}).catch((error : BusinessError) => { 37 console.error(`Failed to get the value of the specified attribute key of the image, error.code: ${error.code}, error.message: ${error.message}`); 38}) 39 40// 编辑EXIF信息。 41imageSourceApi.modifyImageProperty(image.PropertyKey.IMAGE_WIDTH, "120").then(() => { 42 imageSourceApi.getImageProperty(image.PropertyKey.IMAGE_WIDTH).then((width : string) => { 43 console.info('The new imageWidth is ' + width); 44 }).catch((error : BusinessError) => { 45 console.error(`Failed to get the Image Width, error.code: ${error.code}, error.message: ${error.message}`); 46 }) 47}).catch((error : BusinessError) => { 48 console.error(`Failed to modify the Image Width, error.code: ${error.code}, error.message: ${error.message}`); 49}) 50``` 51