1# Using ImageSource to Decode Images 2 3Image decoding refers to the process of decoding an archived image in a supported format into a [PixelMap](image-overview.md) for image display or [processing](image-transformation.md). Currently, the following image formats are supported: JPEG, PNG, GIF, WebP, BMP, SVG, ICO, DNG, and HEIF (depending on the hardware). 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 as fs } from '@kit.CoreFileKit'; 37 ``` 38 39 Then call **fs.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 : fs.File = fs.openSync(filePath, fs.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 PixelMap. 154 - Set the expected format for decoding. 155 ```ts 156 import { BusinessError } from '@kit.BasicServicesKit'; 157 import image from '@ohos.multimedia.image'; 158 let img = await getContext(this).resourceManager.getMediaContent($r('app.media.image')); 159 let imageSource:image.ImageSource = image.createImageSource(img.buffer.slice(0)); 160 let decodingOptions : image.DecodingOptions = { 161 editable: true, 162 desiredPixelFormat: 3, 163 } 164 // Create a PixelMap. 165 imageSource.createPixelMap(decodingOptions).then((pixelMap : image.PixelMap) => { 166 console.log("Succeeded in creating PixelMap") 167 }).catch((err : BusinessError) => { 168 console.error("Failed to create PixelMap") 169 }); 170 ``` 171 - Decode an HDR image. 172 ```ts 173 import { BusinessError } from '@kit.BasicServicesKit'; 174 import image from '@ohos.multimedia.image'; 175 let img = await getContext(this).resourceManager.getMediaContent($r('app.media.CUVAHdr')); 176 let imageSource:image.ImageSource = image.createImageSource(img.buffer.slice(0)); 177 let decodingOptions : image.DecodingOptions = { 178 // 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. 179 desiredDynamicRange: image.DecodingDynamicRange.AUTO, 180 } 181 // Create a PixelMap. 182 imageSource.createPixelMap(decodingOptions).then((pixelMap : image.PixelMap) => { 183 console.log("Succeeded in creating PixelMap") 184 // Check whether the PixelMap is the HDR content. 185 let info = pixelMap.getImageInfoSync(); 186 console.log("pixelmap isHdr:" + info.isHdr); 187 }).catch((err : BusinessError) => { 188 console.error("Failed to create PixelMap") 189 }); 190 ``` 191 After the decoding is complete and the PixelMap is obtained, you can perform subsequent [image processing](image-transformation.md). 192 1935. Release the **PixelMap** instance. 194 195 ```ts 196 pixelMap.release(); 197 ``` 198 199## Sample Code - Decoding an Image in Resource Files 200 2011. Obtain a resource manager. 202 203 ```ts 204 const context : Context = getContext(this); 205 // Obtain a resource manager. 206 const resourceMgr : resourceManager.ResourceManager = context.resourceManager; 207 ``` 208 2092. Create an **ImageSource** instance. 210 - Create an **ImageSource** instance by using the array buffer of **test.jpg** in the **rawfile** folder. 211 212 ```ts 213 resourceMgr.getRawFileContent('test.jpg').then((fileData : Uint8Array) => { 214 console.log("Succeeded in getting RawFileContent") 215 // Obtain the array buffer of the image. 216 const buffer = fileData.buffer.slice(0); 217 const imageSource : image.ImageSource = image.createImageSource(buffer); 218 }).catch((err : BusinessError) => { 219 console.error("Failed to get RawFileContent") 220 }); 221 ``` 222 223 - Create an **ImageSource** instance by using the raw file descriptor of **test.jpg** in the **rawfile** folder. 224 225 ```ts 226 resourceMgr.getRawFd('test.jpg').then((rawFileDescriptor : resourceManager.RawFileDescriptor) => { 227 console.log("Succeeded in getting RawFd") 228 const imageSource : image.ImageSource = image.createImageSource(rawFileDescriptor); 229 }).catch((err : BusinessError) => { 230 console.error("Failed to get RawFd") 231 }); 232 ``` 233 2343. Create a **PixelMap** instance. 235 236 ```ts 237 imageSource.createPixelMap().then((pixelMap: image.PixelMap) => { 238 console.log("Succeeded in creating PixelMap") 239 }).catch((err : BusinessError) => { 240 console.error("Failed to creating PixelMap") 241 }); 242 ``` 243 2444. Release the **PixelMap** instance. 245 246 ```ts 247 pixelMap.release(); 248 ``` 249