• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  // [Start pixelmap_create_image_source_by_fd_in_edit]
29  // 获取沙箱路径创建ImageSource
30  const imageSourceApi: image.ImageSource = image.createImageSource(fd);
31  // [End pixelmap_create_image_source_by_fd_in_edit]
32  if (imageSourceApi == null) {
33    Logger.error('fd undefined');
34  }
35  // [Start pixelmap_read_and_edit_EXIF_info]
36  // 读取EXIF信息,BitsPerSample为每个像素比特数
37  let options: image.ImagePropertyOptions = { index: 0, defaultValue: '9999' }
38  await imageSourceApi.getImageProperty(image.PropertyKey.BITS_PER_SAMPLE, options).then((data: string) => {
39    Logger.info('Succeeded in getting the value of the specified attribute key of the image.');
40  }).catch((error: BusinessError) => {
41    Logger.error('Failed to get the value of the specified attribute key of the image.', String(error));
42  })
43
44  // 编辑EXIF信息
45  await imageSourceApi.modifyImageProperty(image.PropertyKey.IMAGE_WIDTH, '120').then(() => {
46    imageSourceApi.getImageProperty(image.PropertyKey.IMAGE_WIDTH).then((width: string) => {
47      Logger.info('The new imageWidth is ', width);
48    }).catch((error: BusinessError) => {
49      Logger.error('Failed to get the Image Width.', String(error));
50    })
51  }).catch((error: BusinessError) => {
52    Logger.error('Failed to modify the Image Width.', String(error));
53  })
54  // [End pixelmap_read_and_edit_EXIF_info]
55
56  Logger.info('readAndEditEXIF End');
57}