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 // 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 '@kit.AbilityKit'; 27 28 const context = featureAbility.getContext(); 29 const filePath = context.getCacheDir() + "/test.jpg"; 30 ``` 31 32 - 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). 33 To use this method, you must import the \@kit.CoreFileKit module first. 34 35 ```ts 36 import { fileIo } from '@kit.CoreFileKit'; 37 ``` 38 39 Then call **fileIo.openSync()** to obtain the file descriptor. 40 41 ```ts 42 // Code on the stage model 43 const context = getContext(this); 44 const filePath = context.cacheDir + '/test.jpg'; 45 const file : fileIo.File = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE); 46 const fd : number = file?.fd; 47 ``` 48 49 ```ts 50 // Code on the FA model 51 import { featureAbility } from '@kit.AbilityKit'; 52 53 const context = featureAbility.getContext(); 54 const filePath = context.getCacheDir() + "/test.jpg"; 55 const file : fs.File = fs.openSync(filePath, fs.OpenMode.READ_WRITE); 56 const fd : number = file?.fd; 57 ``` 58 59 - 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). 60 61 ```ts 62 // Code on the stage model 63 const context : Context = getContext(this); 64 // Obtain a resource manager. 65 const resourceMgr : resourceManager.ResourceManager = context.resourceManager; 66 ``` 67 68 ```ts 69 // Code on the FA model 70 // Import the resourceManager module. 71 import { resourceManager } from '@kit.LocalizationKit'; 72 import { BusinessError } from '@kit.BasicServicesKit'; 73 resourceManager.getResourceManager().then((resourceMgr : resourceManager.ResourceManager) => { 74 console.log("Succeeded in getting resourceManager") 75 }).catch((err : BusinessError) => { 76 console.error("Failed to get resourceManager") 77 }); 78 ``` 79 80 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. 81 82 ```ts 83 resourceMgr.getRawFileContent('test.jpg').then((fileData : Uint8Array) => { 84 console.log("Succeeded in getting RawFileContent") 85 // Obtain the array buffer of the image. 86 const buffer = fileData.buffer.slice(0); 87 }).catch((err : BusinessError) => { 88 console.error("Failed to get RawFileContent") 89 }); 90 91 ``` 92 93 - 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). 94 95 ```ts 96 // Code on the stage model 97 const context : Context = getContext(this); 98 // Obtain a resource manager. 99 const resourceMgr : resourceManager.ResourceManager = context.resourceManager; 100 ``` 101 102 ```ts 103 // Code on the FA model 104 // Import the resourceManager module. 105 import { resourceManager } from '@kit.LocalizationKit'; 106 import { BusinessError } from '@kit.BasicServicesKit'; 107 resourceManager.getResourceManager().then((resourceMgr : resourceManager.ResourceManager) => { 108 console.log("Succeeded in getting resourceManager") 109 }).catch((err : BusinessError) => { 110 console.error("Failed to get resourceManager") 111 }); 112 ``` 113 114 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. 115 116 ```ts 117 118 resourceMgr.getRawFd('test.jpg').then((rawFileDescriptor : resourceManager.RawFileDescriptor) => { 119 console.log("Succeeded in getting RawFileDescriptor") 120 }).catch((err : BusinessError) => { 121 console.error("Failed to get RawFileDescriptor") 122 }); 123 ``` 124 1253. Create an **ImageSource** instance. 126 127 - Method 1: Create an **ImageSource** instance using the sandbox path. The sandbox path can be obtained by using method 1 in step 2. 128 129 ```ts 130 // path indicates the obtained sandbox path. 131 const imageSource : image.ImageSource = image.createImageSource(filePath); 132 ``` 133 134 - Method 2: Create an **ImageSource** instance using the file descriptor. The file descriptor can be obtained by using method 2 in step 2. 135 136 ```ts 137 // fd is the obtained file descriptor. 138 const imageSource : image.ImageSource = image.createImageSource(fd); 139 ``` 140 141 - Method 3: Create an **ImageSource** instance using an array buffer. The array buffer can be obtained by using method 3 in step 2. 142 143 ```ts 144 const imageSource : image.ImageSource = image.createImageSource(buffer); 145 ``` 146 147 - 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. 148 149 ```ts 150 const imageSource : image.ImageSource = image.createImageSource(rawFileDescriptor); 151 ``` 152 1534. Set **DecodingOptions** and decode the image to obtain a picture. 154 155 Set the expected format for decoding. 156 ```ts 157 import { BusinessError } from '@kit.BasicServicesKit'; 158 import image from '@kit.ImageKit'; 159 let img = await getContext(this).resourceManager.getMediaContent($r('app.media.picture')); 160 let imageSource:image.ImageSource = image.createImageSource(img.buffer.slice(0)); 161 let options: image.DecodingOptionsForPicture = { 162 desiredAuxiliaryPictures: [image.AuxiliaryPictureType.GAINMAP] // GAINMAP indicates the type of the auxiliary picture to be decoded. 163 }; 164 // Create a Picture instance. 165 imageSource.createPicture(options).then((picture: image.Picture) => { 166 console.log("Create picture succeeded.") 167 }).catch((err: BusinessError) => { 168 console.error("Create picture failed.") 169 }); 170 ``` 171 1725. Release the **Picture** instance. 173 174 ```ts 175 picture.release(); 176 ``` 177 178## Sample Code - Decoding an Image in Resource Files 179 1801. Obtain a resource manager. 181 182 ```ts 183 const context : Context = getContext(this); 184 // Obtain a resource manager. 185 const resourceMgr : resourceManager.ResourceManager = context.resourceManager; 186 ``` 187 1882. Create an **ImageSource** instance. 189 - Create an **ImageSource** instance by using the array buffer of **test.jpg** in the **rawfile** folder. 190 191 ```ts 192 resourceMgr.getRawFileContent('test.jpg').then((fileData : Uint8Array) => { 193 console.log("Succeeded in getting RawFileContent") 194 // Obtain the array buffer of the image. 195 const buffer = fileData.buffer.slice(0); 196 const imageSource : image.ImageSource = image.createImageSource(buffer); 197 }).catch((err : BusinessError) => { 198 console.error("Failed to get RawFileContent") 199 }); 200 ``` 201 202 - Create an **ImageSource** instance by using the raw file descriptor of **test.jpg** in the **rawfile** folder. 203 204 ```ts 205 resourceMgr.getRawFd('test.jpg').then((rawFileDescriptor : resourceManager.RawFileDescriptor) => { 206 console.log("Succeeded in getting RawFd") 207 const imageSource : image.ImageSource = image.createImageSource(rawFileDescriptor); 208 }).catch((err : BusinessError) => { 209 console.error("Failed to get RawFd") 210 }); 211 ``` 212 2133. Create a **Picture** instance. 214 215 ```ts 216 let options: image.DecodingOptionsForPicture = { 217 desiredAuxiliaryPictures: [image.AuxiliaryPictureType.GAINMAP] // GAINMAP indicates the type of the auxiliary picture to be decoded. 218 }; 219 imageSource.createPicture(options).then((picture: image.Picture) => { 220 console.log("Create picture succeeded.") 221 }).catch((err : BusinessError) => { 222 console.error("Create picture failed.") 223 }); 224 ``` 225 2264. Release the **Picture** instance. 227 228 ```ts 229 picture.release(); 230 ``` 231