目录

Reading and Editing Image Exif Data

Image Kit provides the capabilities of reading and editing Exchangeable Image File Format (Exif) data.

Exchangeable image file format (Exif) is a file format dedicated for photos taken by digital cameras and is used to record attribute information and shooting data of digital photos. The image must contain Exif data.

In applications such as Gallery, camera, and image editing, you can read Exif data including shooting time, orientation, focal length, and geographic location, and modify part of the Exif data when needed. For example, when the manual lens parameters of the camera are not automatically written as part of the Exif data, or when the shooting time is incorrect due to a camera power failure, you can manually correct the corresponding Exif data.

Currently, the system allows you to read and modify only part of the Exif data. For details about the supported range, see PropertyKey. The following table lists the read and write support for Exif data across different image formats.

Image Format Read Exif Data Modify Exif Data
JPG/JPEG Supported Supported
PNG Supported Supported
HEIF Supported Supported
WebP23+ Supported Supported
DNG23+ Supported Not supported

Available APIs

The following APIs are used for reading and editing Exif data. For details, see ImageSource.

API Description
getImageProperty Obtains the Exif data of a specified attribute key.
modifyImageProperty Modifies the Exif data of a specified attribute key.

Precautions

How to Develop

After obtaining the image and creating an ImageSource object, you can read or edit Exif data. The sample code is as follows:

  1. Import the required modules.

    // Import the related modules.
    import { image } from '@kit.ImageKit';
    import { BusinessError } from '@kit.BasicServicesKit';
  2. Obtain the Exif data of a specified key.

    // Example for obtaining Exif data of a specified key.
    async getExif(imageSourceApi: image.ImageSource | undefined, key: image.PropertyKey): Promise<string> {
      let info: string = '';
      if (imageSourceApi) {
        console.info('getExif: The imageSourceApi is not undefined.');
        // Obtain Exif data of a specified key.
        let options: image.ImagePropertyOptions = { index: 0, defaultValue: 'This key has no value!' };
        try {
          let data = await imageSourceApi.getImageProperty(key, options);
          info = `Succeeded in getting the ${key}'s value: ${data}.`;
          console.info(info);
          return info; // Return the key if it is obtained successfully.
        } catch (error) {
          info =
            `Failed to get the value of the ${key} with error: ${error}.`;
          console.error(info);
          return info; // Return an error message if the key fails to be obtained.
        }
      } else {
        info = 'getExif: The imageSourceApi is undefined.';
        console.info(info);
        return info; // Return the information if imageSourceApi is undefined.
      }
    }
  3. Modify the Exif data of a specified key.

    // Example for modifying Exif data of a specified key.
    async modifyExif(imageSourceApi: image.ImageSource | undefined, key: image.PropertyKey, value: string)
      : Promise<string> {
      let info: string = '';
      if (imageSourceApi) {
        // Edit the Exif data.
        try {
          await imageSourceApi.modifyImageProperty(key, value);
          try {
            let modifyValue = await imageSourceApi.getImageProperty(key)
            info = `The ${key}'s value is modified to ${modifyValue}.`
            console.info(info);
            return info; // Return a success message if the key is obtained successfully.
          } catch (error) {
            console.error(`Failed to get the ${key}'s value with ${error}`);
            console.error(info);
            return info; // Return an error message if the key fails to be obtained.
          }
        } catch (error) {
          info = `Failed to modify the ${key}'s value with ${error}`;
          console.error(info);
          return info; // Return an error message if the key fails to be modified.
        }
      } else {
        info = 'modifyExif: The imageSourceApi is undefined.';
        console.info(info);
        return info; // Return the information if imageSourceApi is undefined.
      }
    }