1# Using ImageSource to Decode Pictures 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 (JPEG and HEIF currently) into a [picture](image-overview.md). 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.filesDir + '/test.jpg'; 37 const file: fs.File = fs.openSync(filePath, fs.OpenMode.READ_WRITE); 38 if (file != undefined) { 39 const fd: number = file.fd; 40 return fd; 41 } 42 return undefined; 43 } 44 ``` 45 46 - 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. 47 48 ```ts 49 import { resourceManager } from '@kit.LocalizationKit'; 50 51 async function getFileBuffer(context: Context): Promise<ArrayBuffer | undefined> { 52 try { 53 const resourceMgr: resourceManager.ResourceManager = context.resourceManager; 54 // Obtain the resource file content. The Uint8Array is returned. 55 const fileData: Uint8Array = await resourceMgr.getRawFileContent('test.jpg'); 56 console.info('Successfully got RawFileContent'); 57 // Convert the array to an ArrayBuffer and return the ArrayBuffer. 58 const buffer: ArrayBuffer = fileData.buffer.slice(0); 59 return buffer; 60 } catch (error) { 61 console.error("Failed to get RawFileContent"); 62 return undefined; 63 } 64 } 65 ``` 66 67 - 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. 68 ```ts 69 import { resourceManager } from '@kit.LocalizationKit'; 70 71 async function getRawFd(context: Context): Promise<resourceManager.RawFileDescriptor | undefined> { 72 try { 73 const resourceMgr: resourceManager.ResourceManager = context.resourceManager; 74 const rawFileDescriptor: resourceManager.RawFileDescriptor = await resourceMgr.getRawFd('test.jpg'); 75 console.info('Successfully got RawFileDescriptor'); 76 return rawFileDescriptor; 77 } catch (error) { 78 console.error('Failed to get RawFileDescriptor:'); 79 return undefined; 80 } 81 } 82 ``` 83 843. Create an ImageSource instance. 85 86 - Method 1: Create an ImageSource instance using the sandbox path. The sandbox path can be obtained by using method 1 in step 2. 87 88 ```ts 89 // path indicates the obtained sandbox path. 90 const imageSource : image.ImageSource = image.createImageSource(filePath); 91 ``` 92 93 - Method 2: Create an ImageSource instance using the file descriptor. The file descriptor can be obtained by using method 2 in step 2. 94 95 ```ts 96 // fd is the obtained file descriptor. 97 const imageSource : image.ImageSource = image.createImageSource(fd); 98 ``` 99 100 - Method 3: Create an ImageSource instance using an array buffer. The array buffer can be obtained by using method 3 in step 2. 101 102 ```ts 103 const imageSource : image.ImageSource = image.createImageSource(buffer); 104 ``` 105 106 - 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. 107 108 ```ts 109 const imageSource : image.ImageSource = image.createImageSource(rawFileDescriptor); 110 ``` 111 1124. Set **DecodingOptions** and decode the image to obtain a picture. 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/arkts-apis-image-Picture.md). 113 114 Set the expected format for decoding. 115 ```ts 116 import { BusinessError } from '@kit.BasicServicesKit'; 117 import { image } from '@kit.ImageKit'; 118 // Create an ImageSource object. Select a proper method in step 3 to replace the preceding code. 119 let fd : number = 0; 120 let imageSource : image.ImageSource = image.createImageSource(fd); 121 // Set the decoding options. 122 let options: image.DecodingOptionsForPicture = { 123 desiredAuxiliaryPictures: [image.AuxiliaryPictureType.GAINMAP] // GAINMAP indicates the type of the auxiliary picture to be decoded. 124 }; 125 // Create a Picture instance. 126 imageSource.createPicture(options).then((picture: image.Picture) => { 127 console.info("Create picture succeeded."); 128 let type: image.AuxiliaryPictureType = image.AuxiliaryPictureType.GAINMAP; 129 let auxPicture: image.AuxiliaryPicture | null = picture.getAuxiliaryPicture(type); 130 // Obtain the information of the auxiliary picture. 131 if(auxPicture != null) { 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 }).catch((err: BusinessError) => { 146 console.error("Create picture failed."); 147 }); 148 ``` 149 1505. Release the Picture instance. 151 152 Ensure that the asynchronous operations of the Picture instance have finished executing. After these variables are no longer needed, you can manually call the APIs below to release it. 153 ```ts 154 picture.release(); 155 ``` 156