• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Using ImageSource to Decode Images
2<!--Kit: Image Kit-->
3<!--Subsystem: Multimedia-->
4<!--Owner: @aulight02-->
5<!--SE: @liyang_bryan-->
6<!--TSE: @xchaosioda-->
7
8Image decoding refers to the process of decoding an image in a supported format into a [PixelMap](../../reference/apis-image-kit/arkts-apis-image-PixelMap.md) for image display or processing. Currently, the following image formats are supported: JPEG, PNG, GIF, WebP, BMP, SVG, ICO, DNG, and HEIF. The supported formats may vary depending on the hardware.
9
10## How to Develop
11
12Read the [API reference](../../reference/apis-image-kit/arkts-apis-image-ImageSource.md) for APIs related to image decoding.
13
141. Import the image module.
15
16   ```ts
17   import { image } from '@kit.ImageKit';
18   ```
19
202. Obtain an image.
21   - Method 1: Directly obtain the image through the sandbox path. This method applies only to images in the application sandbox path. For details about how to obtain the sandbox path, see [Obtaining Application File Paths](../../application-models/application-context-stage.md#obtaining-application-file-paths). For details about the application sandbox and how to push files to the application sandbox directory, see [File Management](../../file-management/app-sandbox-directory.md).
22
23      ```ts
24      function getFilePath(context: Context): string {
25        const filePath: string = context.cacheDir + '/test.jpg';
26        return filePath;
27      }
28      ```
29
30   - Method 2: Obtain the file descriptor of the image through the sandbox path. For details, see [file.fs API Reference](../../reference/apis-core-file-kit/js-apis-file-fs.md). To use this method, you must import the \@kit.CoreFileKit module first.
31
32      ```ts
33      import { fileIo as fs } from '@kit.CoreFileKit';
34
35      function getFileFd(context: Context): number | undefined {
36        const filePath: string = context.cacheDir + '/test.jpg';
37        const file: fs.File = fs.openSync(filePath, fs.OpenMode.READ_WRITE);
38        const fd: number = file?.fd;
39        return fd;
40      }
41      ```
42
43   - Method 3: Obtain the array buffer of the resource file through the resource manager. For details, see [ResourceManager API Reference](../../reference/apis-localization-kit/js-apis-resource-manager.md#getrawfilecontent9-1). To use this method, you must import the \@kit.LocalizationKit module first.
44
45      ```ts
46      import { resourceManager } from '@kit.LocalizationKit';
47
48      async function getFileBuffer(context: Context): Promise<ArrayBuffer | undefined> {
49         try {
50            const resourceMgr: resourceManager.ResourceManager = context.resourceManager;
51            // Obtain the resource file content. The Uint8Array is returned.
52            const fileData: Uint8Array = await resourceMgr.getRawFileContent('test.jpg');
53            console.info('Successfully got RawFileContent');
54            // Convert the array to an ArrayBuffer and return the ArrayBuffer.
55            const buffer: ArrayBuffer = fileData.buffer.slice(0);
56            return buffer;
57         } catch (error) {
58            console.error("Failed to get RawFileContent");
59            return undefined;
60         }
61      }
62      ```
63
64   - Method 4: Obtain the raw file descriptor of the resource file through the resource manager. For details, see [ResourceManager API Reference](../../reference/apis-localization-kit/js-apis-resource-manager.md#getrawfd9-1). To use this method, you must import the \@kit.LocalizationKit module first.
65      ```ts
66      import { resourceManager } from '@kit.LocalizationKit';
67
68      async function getRawFd(context: Context): Promise<resourceManager.RawFileDescriptor | undefined> {
69         try {
70            const resourceMgr: resourceManager.ResourceManager = context.resourceManager;
71            const rawFileDescriptor: resourceManager.RawFileDescriptor = await resourceMgr.getRawFd('test.jpg');
72            console.info('Successfully got RawFileDescriptor');
73            return rawFileDescriptor;
74         } catch (error) {
75            console.error('Failed to get RawFileDescriptor:');
76            return undefined;
77         }
78      }
79      ```
80
813. Create an ImageSource instance.
82
83   - Method 1: Create an ImageSource instance using the sandbox path. The sandbox path can be obtained by using method 1 in step 2.
84
85      ```ts
86      // path indicates the obtained sandbox path.
87      const imageSource : image.ImageSource = image.createImageSource(filePath);
88      ```
89
90   - Method 2: Create an ImageSource instance using the file descriptor. The file descriptor can be obtained by using method 2 in step 2.
91
92      ```ts
93      // fd is the obtained file descriptor.
94      const imageSource : image.ImageSource = image.createImageSource(fd);
95      ```
96
97   - Method 3: Create an ImageSource instance using an array buffer. The array buffer can be obtained by using method 3 in step 2.
98
99      ```ts
100      const imageSource : image.ImageSource = image.createImageSource(buffer);
101      ```
102
103   - Method 4: Create an ImageSource instance using the raw file descriptor of the resource file. The raw file descriptor can be obtained by using method 4 in step 2.
104
105      ```ts
106      const imageSource : image.ImageSource = image.createImageSource(rawFileDescriptor);
107      ```
108
1094. Set **DecodingOptions** and decode the image to obtain a PixelMap.
110   - Set the expected format for decoding.
111      ```ts
112      import { BusinessError } from '@kit.BasicServicesKit';
113      import { image } from '@kit.ImageKit';
114
115      // Create an ImageSource object. Select a proper method in step 3 to replace the preceding code.
116      let fd : number = 0;
117      let imageSource : image.ImageSource = image.createImageSource(fd);
118      // Set the decoding options.
119      let decodingOptions : image.DecodingOptions = {
120         editable: true,
121         desiredPixelFormat: image.PixelMapFormat.RGBA_8888,
122      };
123      // Create a PixelMap.
124      imageSource.createPixelMap(decodingOptions).then((pixelMap : image.PixelMap) => {
125         console.info("Succeeded in creating PixelMap");
126      }).catch((err : BusinessError) => {
127         console.error("Failed to create PixelMap");
128      });
129      ```
130   - Decode an HDR image.
131      ```ts
132      import { BusinessError } from '@kit.BasicServicesKit';
133      import { image } from '@kit.ImageKit';
134
135      // Create an ImageSource object. Select a proper method in step 3 to replace the preceding code.
136      let fd : number = 0;
137      let imageSource : image.ImageSource = image.createImageSource(fd);
138      // Set the decoding options.
139      let decodingOptions : image.DecodingOptions = {
140         // If IMAGE_DYNAMIC_RANGE_AUTO is passed in, decoding is performed based on the image format. If the image is an HDR resource, an HDR PixelMap is obtained after decoding.
141         desiredDynamicRange: image.DecodingDynamicRange.AUTO,
142      };
143      // Create a PixelMap.
144      imageSource.createPixelMap(decodingOptions).then((pixelMap : image.PixelMap) => {
145         console.info("Succeeded in creating PixelMap");
146         // Check whether the PixelMap is the HDR content.
147         let info = pixelMap.getImageInfoSync();
148         console.info("pixelmap isHdr:" + info.isHdr);
149      }).catch((err : BusinessError) => {
150         console.error("Failed to create PixelMap");
151      });
152      ```
153   After the decoding is complete and the PixelMap is obtained, you can perform subsequent [image processing](image-transformation.md).
154
1555. Release the PixelMap and ImageSource instances.
156
157   Ensure that the asynchronous operations of the PixelMap and ImageSource instances have finished executing. After these variables are no longer needed, you can manually call the APIs below to release them.
158   ```ts
159   pixelMap.release();
160   imageSource.release();
161   ```
162
163   > **NOTE**
164   > 1. When to release the ImageSource instance: After successfully executing **createPixelMap** and obtaining the PixelMap instance, if you are certain that no other APIs of ImageSource will be used, you can manually release the ImageSource instance. Since the PixelMap instance obtained from decoding is independent, releasing the ImageSource instance will not make the PixelMap instance unusable.
165   > 2. When to release the PixelMap instance: If you are using the [**Image** component](../../reference/apis-arkui/arkui-ts/ts-basic-components-image.md) for displaying images, there is no need to manually release the PixelMap instance, as the Image component will automatically manage the PixelMap instance passed to it. If your application handles the PixelMap instance on its own, you are advised to manually release the PixelMap instance of the old page during page transitions or when the application switches to the background. In scenarios where memory resources are tight, you are advised to release the PixelMap instances of all invisible pages except the current one.
166
167<!--RP1-->
168<!--RP1End-->
169