目录

Using ImagePacker to Encode Pictures

Image encoding refers to the process of encoding a Picture object into image files of different formats (currently only JPEG and HEIF formats are supported) for subsequent processing, such as saving and transmission.

How to Develop

For details about image encoding APIs, see ImagePacker.

  1. Import the required modules.

    // Import the related modules.
    import { image } from '@kit.ImageKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { common } from '@kit.AbilityKit';
    import { fileIo } from '@kit.CoreFileKit';
    import { resourceManager } from '@kit.LocalizationKit';
  2. Set the encoding options PackingOption.

    Here, encoding to a JPEG image is used as an example. The target encoding format follows the MIME standard. Therefore, PackingOption.format must be set to image/jpeg, and the encoded file extension can be .jpg or .jpeg.

    let packOpts: image.PackingOption = {
      format: 'image/jpeg',
      // The default value of quality is 0. It is recommended to set it to no less than 80. In this example, it is set to 90 to balance image quality and file size.
      quality: 90,
      needsPackProperties: true
    };
  3. Encapsulate a function, pass in picture, and use the packing API to encode to an ArrayBuffer, or use the packToFile API to encode to a file.

    Before encoding, you need to obtain the picture through decoding. For details, see Using ImageSource to Decode Pictures.

    • Encode the picture to an ArrayBuffer.

      async function packing(picture: image.Picture, packOpts: image.PackingOption) {
        const imagePackerApi = image.createImagePacker();
        try {
          let data = await imagePackerApi.packing(picture, packOpts);
          copyData = data;
          console.info('Succeeded in packing the image.');
        } catch (error) {
          console.error('Failed to pack the picture to data. And the error is: ' + error);
        } finally {
          await imagePackerApi.release();
        }
      }
    • Encode the picture to a file.

      async function packToFile(picture: image.Picture, packOpts: image.PackingOption, context: Context) {
        let imagePackerApi: image.ImagePacker | undefined = undefined;
        let file: fileIo.File | undefined = undefined;
        try {
          const path : string = context.cacheDir + '/picture.jpg';
          file = fileIo.openSync(path, fileIo.OpenMode.CREATE | fileIo.OpenMode.READ_WRITE);
          imagePackerApi = image.createImagePacker();
          await imagePackerApi.packToFile(picture, file.fd, packOpts);
        } catch (error) {
          console.error('Failed to pack the picture to file. And the error is: ' + error);
        } finally {
          if (file) {
            fileIo.closeSync(file.fd);
          }
          if (imagePackerApi) {
            await imagePackerApi.release();
          }
        }
      }