• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Using ImageSource to Decode Pictures
2
3Image decoding refers to the process of decoding an archived image in a supported format (JPEG and HEIF currently) into a [picture](image-overview.md).
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 '@kit.ImageKit';
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 directory, see [File Management](../../file-management/app-sandbox-directory.md).
17
18      ```ts
19      const context : Context = getContext(this);
20      const filePath : string = context.cacheDir + '/test.jpg';
21      ```
22
23   - 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.
24
25      ```ts
26      import { fileIo } from '@kit.CoreFileKit';
27      ```
28
29      Then call **fileIo.openSync()** to obtain the file descriptor.
30
31      ```ts
32      const context = getContext(this);
33      const filePath = context.cacheDir + '/test.jpg';
34      const file : fileIo.File = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE);
35      const fd : number = file?.fd;
36      ```
37
38   - 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).
39
40      ```ts
41      const context : Context = getContext(this);
42      // Obtain a resource manager.
43      const resourceMgr : resourceManager.ResourceManager = context.resourceManager;
44      ```
45
46      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.
47
48      ```ts
49      resourceMgr.getRawFileContent('test.jpg').then((fileData : Uint8Array) => {
50         console.log("Succeeded in getting RawFileContent")
51         // Obtain the array buffer of the image.
52         const buffer = fileData.buffer.slice(0);
53      }).catch((err : BusinessError) => {
54         console.error("Failed to get RawFileContent")
55      });
56
57      ```
58
59   - 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).
60
61      ```ts
62      const context : Context = getContext(this);
63      // Obtain a resource manager.
64      const resourceMgr : resourceManager.ResourceManager = context.resourceManager;
65      ```
66
67      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.
68
69      ```ts
70
71      resourceMgr.getRawFd('test.jpg').then((rawFileDescriptor : resourceManager.RawFileDescriptor) => {
72         console.log("Succeeded in getting RawFileDescriptor")
73      }).catch((err : BusinessError) => {
74         console.error("Failed to get RawFileDescriptor")
75      });
76      ```
77
783. Create an **ImageSource** instance.
79
80   - Method 1: Create an **ImageSource** instance using the sandbox path. The sandbox path can be obtained by using method 1 in step 2.
81
82      ```ts
83      // path indicates the obtained sandbox path.
84      const imageSource : image.ImageSource = image.createImageSource(filePath);
85      ```
86
87   - Method 2: Create an **ImageSource** instance using the file descriptor. The file descriptor can be obtained by using method 2 in step 2.
88
89      ```ts
90      // fd is the obtained file descriptor.
91      const imageSource : image.ImageSource = image.createImageSource(fd);
92      ```
93
94   - Method 3: Create an **ImageSource** instance using an array buffer. The array buffer can be obtained by using method 3 in step 2.
95
96      ```ts
97      const imageSource : image.ImageSource = image.createImageSource(buffer);
98      ```
99
100   - 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.
101
102      ```ts
103      const imageSource : image.ImageSource = image.createImageSource(rawFileDescriptor);
104      ```
105
1064. Set **DecodingOptions** and decode the image to obtain a picture.
107
108   Set the expected format for decoding.
109      ```ts
110      import { BusinessError } from '@kit.BasicServicesKit';
111      import image from '@kit.ImageKit';
112      let img = await getContext(this).resourceManager.getMediaContent($r('app.media.picture'));
113      let imageSource:image.ImageSource = image.createImageSource(img.buffer.slice(0));
114      let options: image.DecodingOptionsForPicture = {
115         desiredAuxiliaryPictures: [image.AuxiliaryPictureType.GAINMAP] // GAINMAP indicates the type of the auxiliary picture to be decoded.
116      };
117      // Create a Picture instance.
118      imageSource.createPicture(options).then((picture: image.Picture) => {
119         console.log("Create picture succeeded.")
120      }).catch((err: BusinessError) => {
121         console.error("Create picture failed.")
122      });
123      ```
124
1255. Manipulate the picture, for example, obtaining an auxiliary picture. For details about how to manipulate a picture and auxiliary picture, see [Image API](../../reference/apis-image-kit/js-apis-image.md#picture13).
126
127   ```ts
128   // Obtain an auxiliary picture.
129   let type: image.AuxiliaryPictureType = image.AuxiliaryPictureType.GAINMAP;
130   let auxPicture: image.AuxiliaryPicture | null = picture.getAuxiliaryPicture(type);
131   // Obtain the information of the auxiliary picture.
132   let auxinfo: image.AuxiliaryPictureInfo = auxPicture.getAuxiliaryPictureInfo();
133   console.info('GetAuxiliaryPictureInfo Type: ' + auxinfo.auxiliaryPictureType +
134      ' height: ' + auxinfo.size.height + ' width: ' + auxinfo.size.width +
135      ' rowStride: ' +  auxinfo.rowStride +  ' pixelFormat: ' + auxinfo.pixelFormat +
136      ' colorSpace: ' +  auxinfo.colorSpace);
137   // Read data of the auxiliary picture and write the data to an ArrayBuffer.
138   auxPicture.readPixelsToBuffer().then((pixelsBuffer: ArrayBuffer) => {
139      console.info('Read pixels to buffer success.');
140   }).catch((error: BusinessError) => {
141      console.error('Read pixels to buffer failed error.code: ' + JSON.stringify(error.code) + ' ,error.message:' + JSON.stringify(error.message));
142   });
143   auxPicture.release();
144   ```
145
1466. Release the **Picture** instance.
147
148   ```ts
149   picture.release();
150   ```
151
152## Sample Code - Decoding an Image in Resource Files
153
1541. Obtain a resource manager.
155
156   ```ts
157   const context : Context = getContext(this);
158   // Obtain a resource manager.
159   const resourceMgr : resourceManager.ResourceManager = context.resourceManager;
160   ```
161
1622. Create an **ImageSource** instance.
163   - Create an **ImageSource** instance by using the array buffer of **test.jpg** in the **rawfile** folder.
164
165     ```ts
166      resourceMgr.getRawFileContent('test.jpg').then((fileData : Uint8Array) => {
167         console.log("Succeeded in getting RawFileContent")
168         // Obtain the array buffer of the image.
169         const buffer = fileData.buffer.slice(0);
170         const imageSource : image.ImageSource = image.createImageSource(buffer);
171      }).catch((err : BusinessError) => {
172         console.error("Failed to get RawFileContent")
173      });
174     ```
175
176   - Create an **ImageSource** instance by using the raw file descriptor of **test.jpg** in the **rawfile** folder.
177
178     ```ts
179      resourceMgr.getRawFd('test.jpg').then((rawFileDescriptor : resourceManager.RawFileDescriptor) => {
180         console.log("Succeeded in getting RawFd")
181         const imageSource : image.ImageSource = image.createImageSource(rawFileDescriptor);
182      }).catch((err : BusinessError) => {
183         console.error("Failed to get RawFd")
184      });
185     ```
186
1873. Create a **Picture** instance.
188
189   ```ts
190   let options: image.DecodingOptionsForPicture = {
191      desiredAuxiliaryPictures: [image.AuxiliaryPictureType.GAINMAP] // GAINMAP indicates the type of the auxiliary picture to be decoded.
192   };
193   imageSource.createPicture(options).then((picture: image.Picture) => {
194      console.log("Create picture succeeded.")
195   }).catch((err : BusinessError) => {
196      console.error("Create picture failed.")
197   });
198   ```
199
2004. Manipulate the picture, for example, obtaining an auxiliary picture. For details about how to manipulate a picture and auxiliary picture, see [Image API](../../reference/apis-image-kit/js-apis-image.md#picture13).
201
202   ```ts
203   // Obtain an auxiliary picture.
204   let type: image.AuxiliaryPictureType = image.AuxiliaryPictureType.GAINMAP;
205   let auxPicture: image.AuxiliaryPicture | null = picture.getAuxiliaryPicture(type);
206   // Obtain the information of the auxiliary picture.
207   let auxinfo: image.AuxiliaryPictureInfo = auxPicture.getAuxiliaryPictureInfo();
208   console.info('GetAuxiliaryPictureInfo Type: ' + auxinfo.auxiliaryPictureType +
209      ' height: ' + auxinfo.size.height + ' width: ' + auxinfo.size.width +
210      ' rowStride: ' +  auxinfo.rowStride +  ' pixelFormat: ' + auxinfo.pixelFormat +
211      ' colorSpace: ' +  auxinfo.colorSpace);
212   // Read data of the auxiliary picture and write the data to an ArrayBuffer.
213   auxPicture.readPixelsToBuffer().then((pixelsBuffer: ArrayBuffer) => {
214      console.info('Read pixels to buffer success.');
215   }).catch((error: BusinessError) => {
216      console.error('Read pixels to buffer failed error.code: ' + JSON.stringify(error.code) + ' ,error.message:' + JSON.stringify(error.message));
217   });
218   auxPicture.release();
219   ```
220
2215. Release the **Picture** instance.
222
223   ```ts
224   picture.release();
225   ```
226