1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { image } from '@kit.ImageKit'; 17import { BusinessError } from '@kit.BasicServicesKit'; 18import Logger from './Logger'; 19 20 21/** 22 * 读取图片的EXIF信息,并将IMAGE_WIDTH设置为120。 23 * 24 * @param fd - 文件描述符。 25 **/ 26export async function readAndEditEXIF(fd: number) { 27 Logger.info('readAndEditEXIF Start'); 28 // 获取沙箱路径创建ImageSource 29 const imageSourceApi: image.ImageSource = image.createImageSource(fd); 30 if (imageSourceApi == null) { 31 Logger.error('fd undefined'); 32 } 33 34 // 读取EXIF信息,BitsPerSample为每个像素比特数 35 let options: image.ImagePropertyOptions = { index: 0, defaultValue: '9999' } 36 await imageSourceApi.getImageProperty(image.PropertyKey.BITS_PER_SAMPLE, options).then((data: string) => { 37 Logger.info('Succeeded in getting the value of the specified attribute key of the image.'); 38 }).catch((error: BusinessError) => { 39 Logger.error('Failed to get the value of the specified attribute key of the image.', String(error)); 40 }) 41 42 // 编辑EXIF信息 43 await imageSourceApi.modifyImageProperty(image.PropertyKey.IMAGE_WIDTH, '120').then(() => { 44 imageSourceApi.getImageProperty(image.PropertyKey.IMAGE_WIDTH).then((width: string) => { 45 Logger.info('The new imageWidth is ', width); 46 }).catch((error: BusinessError) => { 47 Logger.error('Failed to get the Image Width.', String(error)); 48 }) 49 }).catch((error: BusinessError) => { 50 Logger.error('Failed to modify the Image Width.', String(error)); 51 }) 52 53 Logger.info('readAndEditEXIF End'); 54}