• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Image Decoding (ArkTS)
2
3Image decoding refers to the process of decoding an archived image in a supported format into a [pixel map](image-overview.md) for image display or [processing](image-transformation.md). Currently, the following image formats are supported: JPEG, PNG, GIF, RAW, WebP, BMP, SVG, and ICO.
4
5## How to Develop
6
7Read [Image](../reference/apis-image-kit/js-apis-image.md#imagesource) for APIs related to image decoding.
8
91. Import the image module.
10
11   ```ts
12   import image from '@ohos.multimedia.image';
13   ```
14
152. Obtain an image.
16   - Method 1: Obtain the 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, see [File Management](../file-management/app-sandbox-directory.md).
17
18      ```ts
19      // Code on the stage model
20      const context : Context = getContext(this);
21      const filePath : string = context.cacheDir + '/test.jpg';
22      ```
23
24      ```ts
25      // Code on the FA model
26      import featureAbility from '@ohos.ability.featureAbility';
27
28      const context = featureAbility.getContext();
29      const filePath = context.getCacheDir() + "/test.jpg";
30      ```
31   - 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).
32      To use this method, you must import the \@ohos.file.fs module first.
33
34      ```ts
35      import fs from '@ohos.file.fs';
36      ```
37
38      Then call **fs.openSync()** to obtain the file descriptor.
39
40      ```ts
41      // Code on the stage model
42      const context = getContext(this);
43      const filePath = context.cacheDir + '/test.jpg';
44      const file : fs.File = fs.openSync(filePath, fs.OpenMode.READ_WRITE);
45      const fd : number = file?.fd;
46      ```
47
48      ```ts
49      // Code on the FA model
50      import featureAbility from '@ohos.ability.featureAbility';
51
52      const context = featureAbility.getContext();
53      const filePath = context.getCacheDir() + "/test.jpg";
54      const file : fs.File = fs.openSync(filePath, fs.OpenMode.READ_WRITE);
55      const fd : number = file?.fd;
56      ```
57   - 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).
58
59      ```ts
60      // Code on the stage model
61      const context : Context = getContext(this);
62      // Obtain a resource manager.
63      const resourceMgr : resourceManager.ResourceManager = context.resourceManager;
64      ```
65
66      ```ts
67      // Code on the FA model
68      // Import the resourceManager module.
69      import resourceManager from '@ohos.resourceManager';
70      import {BusinessError} from '@ohos.base';
71      resourceManager.getResourceManager().then((resourceMgr : resourceManager.ResourceManager) => {
72         console.log("Succeeded in getting resourceManager")
73      }).catch((err : BusinessError) => {
74         console.error("Failed to get resourceManager")
75      });
76      ```
77
78      The method of obtaining the resource manager varies according to the application model. After obtaining the resource manager, call **resourceMgr.getRawFileContent()** to obtain the array buffer of the resource file.
79
80      ```ts
81      resourceMgr.getRawFileContent('test.jpg').then((fileData : Uint8Array) => {
82         console.log("Succeeded in getting RawFileContent")
83         // Obtain the array buffer of the image.
84         const buffer = fileData.buffer.slice(0);
85      }).catch((err : BusinessError) => {
86         console.error("Failed to get RawFileContent")
87      });
88
89      ```
90   - 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).
91
92      ```ts
93      // Code on the stage model
94      const context : Context = getContext(this);
95      // Obtain a resource manager.
96      const resourceMgr : resourceManager.ResourceManager = context.resourceManager;
97      ```
98
99      ```ts
100      // Code on the FA model
101      // Import the resourceManager module.
102      import resourceManager from '@ohos.resourceManager';
103      import {BusinessError} from '@ohos.base';
104      resourceManager.getResourceManager().then((resourceMgr : resourceManager.ResourceManager) => {
105         console.log("Succeeded in getting resourceManager")
106      }).catch((err : BusinessError) => {
107         console.error("Failed to get resourceManager")
108      });
109      ```
110
111      The method of obtaining the resource manager varies according to the application model. After obtaining the resource manager, call **resourceMgr.getRawFd()** to obtain the raw file descriptor of the resource file.
112
113      ```ts
114
115      resourceMgr.getRawFd('test.jpg').then((rawFileDescriptor : resourceManager.RawFileDescriptor) => {
116         console.log("Succeeded in getting resourceManager")
117      }).catch((err : BusinessError) => {
118         console.error("Failed to get resourceManager")
119      });
120      ```
121
122
1233. Create an **ImageSource** instance.
124   - Method 1: Create an **ImageSource** instance using the sandbox path. The sandbox path can be obtained by using method 1 in step 2.
125
126      ```ts
127      // path indicates the obtained sandbox path.
128      const imageSource : image.ImageSource = image.createImageSource(filePath);
129      ```
130   - Method 2: Create an **ImageSource** instance using the file descriptor. The file descriptor can be obtained by using method 2 in step 2.
131
132      ```ts
133      // fd is the obtained file descriptor.
134      const imageSource : image.ImageSource = image.createImageSource(fd);
135      ```
136   - Method 3: Create an **ImageSource** instance using an array buffer. The array buffer can be obtained by using method 3 in step 2.
137
138      ```ts
139      const imageSource : image.ImageSource = image.createImageSource(buffer);
140      ```
141   - 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.
142
143      ```ts
144      const imageSource : image.ImageSource = image.createImageSource(rawFileDescriptor);
145      ```
146
1474. Set **DecodingOptions** and decode the image to obtain a pixel map.
148
149   ```ts
150   import {BusinessError} from '@ohos.base';
151   let decodingOptions : image.DecodingOptions = {
152       editable: true,
153       desiredPixelFormat: 3,
154   }
155   // Create a pixel map and perform rotation and scaling on it.
156   imageSource.createPixelMap(decodingOptions).then((pixelMap : image.PixelMap) => {
157      console.log("Succeeded in creating PixelMap")
158   }).catch((err : BusinessError) => {
159      console.error("Failed to create PixelMap")
160   });
161   ```
162
163   After the decoding is complete and the pixel map is obtained, you can perform subsequent [image processing](image-transformation.md).
164
1655. Release the **PixelMap** instance.
166   ```ts
167   pixelMap.release();
168   ```
169
170## Sample Code - Decoding an Image in Resource Files
171
1721. Obtain a resource manager.
173
174   ```ts
175   const context : Context = getContext(this);
176   // Obtain a resource manager.
177   const resourceMgr : resourceManager.ResourceManager = context.resourceManager;
178   ```
179
1802. Create an **ImageSource** instance.
181   - Create an **ImageSource** instance by using the array buffer of **test.jpg** in the **rawfile** folder.
182     ```ts
183      resourceMgr.getRawFileContent('test.jpg').then((fileData : Uint8Array) => {
184         console.log("Succeeded in getting RawFileContent")
185         // Obtain the array buffer of the image.
186         const buffer = fileData.buffer.slice(0);
187         const imageSource : image.ImageSource = image.createImageSource(buffer);
188      }).catch((err : BusinessError) => {
189         console.error("Failed to get RawFileContent")
190      });
191     ```
192
193   - Create an **ImageSource** instance by using the raw file descriptor of **test.jpg** in the **rawfile** folder.
194     ```ts
195      resourceMgr.getRawFd('test.jpg').then((rawFileDescriptor : resourceManager.RawFileDescriptor) => {
196         console.log("Succeeded in getting RawFd")
197         const imageSource : image.ImageSource = image.createImageSource(rawFileDescriptor);
198      }).catch((err : BusinessError) => {
199         console.error("Failed to get RawFd")
200      });
201     ```
2023. Create a **PixelMap** instance.
203
204   ```ts
205   imageSource.createPixelMap().then((image.PixelMap) => {
206      console.log("Succeeded in creating PixelMap")
207      const imageSource : image.ImageSource = image.createImageSource(rawFileDescriptor);
208   }).catch((err : BusinessError) => {
209      console.error("Failed to creating PixelMap")
210   });
211   ```
212
2134. Release the **PixelMap** instance.
214   ```ts
215   pixelMap.release();
216   ```
217