1# Image Development 2 3## When to Use 4 5You can use image development APIs to decode images into pixel maps and encode the pixel maps into a supported format. 6 7## Available APIs 8 9For details about the APIs, see [Image Processing](../reference/apis/js-apis-image.md). 10 11## How to Develop 12 13### Full-Process Scenario 14 15The full process includes creating an instance, reading image information, reading and writing pixel maps, updating data, packaging pixels, and releasing resources. 16 17```js 18const color = new ArrayBuffer(96); // Create a buffer to store image pixel data. 19let opts = { alphaType: 0, editable: true, pixelFormat: 4, scaleMode: 1, size: { height: 2, width: 3 } } // Image pixel data. 20 21// Create a PixelMap object. 22image.createPixelMap(color, opts, (err, pixelmap) => { 23 console.log('Succeeded in creating pixelmap.'); 24 // Failed to create the PixelMap object. 25 if (err) { 26 console.info('create pixelmap failed, err' + err); 27 return 28 } 29 30 // Read pixels. 31 const area = { 32 pixels: new ArrayBuffer(8), 33 offset: 0, 34 stride: 8, 35 region: { size: { height: 1, width: 2 }, x: 0, y: 0 } 36 } 37 pixelmap.readPixels(area,() => { 38 let bufferArr = new Uint8Array(area.pixels); 39 let res = true; 40 for (let i = 0; i < bufferArr.length; i++) { 41 console.info(' buffer ' + bufferArr[i]); 42 if(res) { 43 if(bufferArr[i] == 0) { 44 res = false; 45 console.log('readPixels end.'); 46 break; 47 } 48 } 49 } 50 }) 51 52 // Store pixels. 53 const readBuffer = new ArrayBuffer(96); 54 pixelmap.readPixelsToBuffer(readBuffer,() => { 55 let bufferArr = new Uint8Array(readBuffer); 56 let res = true; 57 for (let i = 0; i < bufferArr.length; i++) { 58 if(res) { 59 if (bufferArr[i] !== 0) { 60 res = false; 61 console.log('readPixelsToBuffer end.'); 62 break; 63 } 64 } 65 } 66 }) 67 68 // Write pixels. 69 pixelmap.writePixels(area,() => { 70 const readArea = { pixels: new ArrayBuffer(20), offset: 0, stride: 8, region: { size: { height: 1, width: 2 }, x: 0, y: 0 }} 71 pixelmap.readPixels(readArea,() => { 72 let readArr = new Uint8Array(readArea.pixels); 73 let res = true; 74 for (let i = 0; i < readArr.length; i++) { 75 if(res) { 76 if (readArr[i] !== 0) { 77 res = false; 78 console.log('readPixels end.please check buffer'); 79 break; 80 } 81 } 82 } 83 }) 84 }) 85 86 const writeColor = new ArrayBuffer(96); // Pixel data of the image. 87 // Write pixels to the buffer. 88 pixelmap.writeBufferToPixels(writeColor).then(() => { 89 const readBuffer = new ArrayBuffer(96); 90 pixelmap.readPixelsToBuffer(readBuffer).then (() => { 91 let bufferArr = new Uint8Array(readBuffer); 92 let res = true; 93 for (let i = 0; i < bufferArr.length; i++) { 94 if(res) { 95 if (bufferArr[i] !== i) { 96 res = false; 97 console.log('readPixels end.please check buffer'); 98 break; 99 } 100 } 101 } 102 }) 103 }) 104 105 // Obtain image information. 106 pixelmap.getImageInfo((err, imageInfo) => { 107 // Failed to obtain the image information. 108 if (err || imageInfo == null) { 109 console.info('getImageInfo failed, err' + err); 110 return 111 } 112 if (imageInfo !== null) { 113 console.log('Succeeded in getting imageInfo'); 114 } 115 }) 116 117 // Release the PixelMap object. 118 pixelmap.release(()=>{ 119 console.log('Succeeded in releasing pixelmap'); 120 }) 121}) 122 123// Create an image source (uri). 124let path = '/data/local/tmp/test.jpg'; 125const imageSourceApi1 = image.createImageSource(path); 126 127// Create an image source (fd). 128let fd = 29; 129const imageSourceApi2 = image.createImageSource(fd); 130 131// Create an image source (data). 132const data = new ArrayBuffer(96); 133const imageSourceApi3 = image.createImageSource(data); 134 135// Release the image source. 136imageSourceApi3.release(() => { 137 console.log('Succeeded in releasing imagesource'); 138}) 139 140// Encode the image. 141const imagePackerApi = image.createImagePacker(); 142const imageSourceApi = image.createImageSource(0); 143let packOpts = { format:"image/jpeg", quality:98 }; 144imagePackerApi.packing(imageSourceApi, packOpts, (err, data) => { 145 if (err) { 146 console.info('packing from imagePackerApi failed, err' + err); 147 return 148 } 149 console.log('Succeeded in packing'); 150}) 151 152// Release the ImagePacker object. 153imagePackerApi.release(); 154``` 155 156### Decoding Scenario 157 158```js 159let path = '/data/local/tmp/test.jpg'; // Set the path for creating an image source. 160 161// Create an image source using a path. 162const imageSourceApi = image.createImageSource(path); // '/data/local/tmp/test.jpg' 163 164// Set parameters. 165let decodingOptions = { 166 sampleSize:1, // Sampling size of the thumbnail. 167 editable: true, // Whether the image can be edited. 168 desiredSize:{ width:1, height:2}, // Desired output size of the image. 169 rotateDegrees:10, // Rotation angle of the image. 170 desiredPixelFormat:2, // Decoded pixel format. 171 desiredRegion: { size: { height: 1, width: 2 }, x: 0, y: 0 }, // Region of the image to decode. 172 index:0// Image sequence number. 173 }; 174 175// Create a pixel map in callback mode. 176imageSourceApi.createPixelMap(decodingOptions, (err, pixelmap) => { 177 // Failed to create the PixelMap object. 178 if (err) { 179 console.info('create pixelmap failed, err' + err); 180 return 181 } 182 console.log('Succeeded in creating pixelmap.'); 183}) 184 185// Create a pixel map in promise mode. 186imageSourceApi.createPixelMap().then(pixelmap => { 187 console.log('Succeeded in creating pixelmap.'); 188 189 // Obtain the number of bytes in each line of pixels. 190 let num = pixelmap.getBytesNumberPerRow(); 191 192 // Obtain the total number of pixel bytes. 193 let pixelSize = pixelmap.getPixelBytesNumber(); 194 195 // Obtain the pixel map information. 196 pixelmap.getImageInfo().then( imageInfo => {}); 197 198 // Release the PixelMap object. 199 pixelmap.release(()=>{ 200 console.log('Succeeded in releasing pixelmap'); 201 }) 202}).catch(error => { 203 console.log('Failed in creating pixelmap.' + error); 204}) 205``` 206 207### Encoding Scenario 208 209```js 210let path = '/data/local/tmp/test.png' // Set the path for creating an image source. 211 212// Set the image source. 213const imageSourceApi = image.createImageSource(path); // '/data/local/tmp/test.png' 214 215// Print the error message if the image source fails to be created. 216if (imageSourceApi == null) { 217 console.log('Failed in creating imageSource.'); 218} 219 220// Create an image packer if the image source is successfully created. 221const imagePackerApi = image.createImagePacker(); 222 223// Print the error information if the image packer fails to be created. 224if (imagePackerApi == null) { 225 console.log('Failed in creating imagePacker.'); 226} 227 228// Set encoding parameters if the image packer is successfully created. 229let packOpts = { format:"image/jpeg", // The supported encoding format is jpg. 230 quality:98 } // Image quality, which ranges from 0 to 100. 231 232// Encode the image. 233imagePackerApi.packing(imageSourceApi, packOpts) 234.then( data => { 235 console.log('Succeeded in packing'); 236}) 237 238// Release the image packer after the encoding is complete. 239imagePackerApi.release(); 240 241// Obtain the image source information. 242imageSourceApi.getImageInfo((err, imageInfo) => { 243 console.log('Succeeded in getting imageInfo'); 244}) 245 246const array = new ArrayBuffer(100); // Incremental data. 247// Update incremental data. 248imageSourceApi.updateData(array, false, 0, 10,(error, data)=> {}) 249 250``` 251 252### Using ImageReceiver 253 254Example scenario: The camera functions as the client to transmit image data to the server. 255 256```js 257public async init(surfaceId: any) { 258 259 // (Server code) Create an ImageReceiver object. 260 let receiver = image.createImageReceiver(8 * 1024, 8, image.ImageFormat.JPEG, 1); 261 262 // Obtain the surface ID. 263 receiver.getReceivingSurfaceId((err, surfaceId) => { 264 // Failed to obtain the surface ID. 265 if (err) { 266 console.info('getReceivingSurfaceId failed, err' + err); 267 return 268 } 269 console.info("receiver getReceivingSurfaceId success"); 270 }); 271 // Register a surface listener, which is triggered after the buffer of the surface is ready. 272 receiver.on('imageArrival', () => { 273 // Obtain the latest buffer of the surface. 274 receiver.readNextImage((err, img) => { 275 img.getComponent(4, (err, component) => { 276 // Consume component.byteBuffer. For example, save the content in the buffer as an image. 277 }) 278 }) 279 }) 280 281 // Call a Camera API to transfer the surface ID to the camera, which then obtains the surface based on the surface ID and generates a surface buffer. 282} 283``` 284