js-apis-image.md 56.1 KB
Newer Older
W
wusongqing 已提交
1
# Image Processing
W
wusongqing 已提交
2

W
wusongqing 已提交
3 4
> **NOTE**
>
W
wusongqing 已提交
5
> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
W
wusongqing 已提交
6

W
wusongqing 已提交
7
## Modules to Import
W
wusongqing 已提交
8

W
wusongqing 已提交
9
```js
W
wusongqing 已提交
10 11 12 13
import image from '@ohos.multimedia.image';
```

## image.createPixelMap<sup>8+</sup>
W
wusongqing 已提交
14
createPixelMap(colors: ArrayBuffer, options: InitializationOptions): Promise\<PixelMap>
W
wusongqing 已提交
15 16 17 18 19 20 21

Creates a **PixelMap** object. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

W
wusongqing 已提交
22 23
| Name   | Type                                            | Mandatory| Description                                                        |
| ------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
24
| colors  | ArrayBuffer                                      | Yes  | Color array in BGRA_8888 format.                                   |
W
wusongqing 已提交
25
| options | [InitializationOptions](#initializationoptions8) | Yes  | Pixel properties, including the alpha type, size, scale mode, pixel format, and editable.|
W
wusongqing 已提交
26 27 28 29 30

**Return value**

| Type                            | Description          |
| -------------------------------- | -------------- |
W
wusongqing 已提交
31
| Promise\<[PixelMap](#pixelmap7)> | Promise used to return the **PixelMap** object.|
W
wusongqing 已提交
32 33 34 35

**Example**

```js
W
wusongqing 已提交
36
const color = new ArrayBuffer(96);
W
wusongqing 已提交
37
let bufferArr = new Unit8Array(color);
W
wusongqing 已提交
38 39 40 41
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts)
    .then((pixelmap) => {
        })
W
wusongqing 已提交
42 43 44 45
```

## image.createPixelMap<sup>8+</sup>

W
wusongqing 已提交
46
createPixelMap(colors: ArrayBuffer, options: InitializationOptions, callback: AsyncCallback\<PixelMap>): void
W
wusongqing 已提交
47 48 49 50 51 52 53

Creates a **PixelMap** object. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

W
wusongqing 已提交
54 55
| Name    | Type                                            | Mandatory| Description                      |
| -------- | ------------------------------------------------ | ---- | -------------------------- |
W
wusongqing 已提交
56
| colors   | ArrayBuffer                                      | Yes  | Color array in BGRA_8888 format. |
W
wusongqing 已提交
57
| options  | [InitializationOptions](#initializationoptions8) | Yes  | Pixel properties.                    |
W
wusongqing 已提交
58
| callback | AsyncCallback\<[PixelMap](#pixelmap7)>           | Yes  | Callback used to return the **PixelMap** object.|
W
wusongqing 已提交
59 60 61 62

**Example**

```js
W
wusongqing 已提交
63
const color = new ArrayBuffer(96);
W
wusongqing 已提交
64
let bufferArr = new Unit8Array(color);
W
wusongqing 已提交
65 66 67
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts, (pixelmap) => {
        })
W
wusongqing 已提交
68 69 70 71 72 73 74 75
```

## PixelMap<sup>7+</sup>

Provides APIs to read or write image pixel map data and obtain image pixel map information. Before calling any API in **PixelMap**, you must use **createPixelMap** to create a **PixelMap** object.

 ### Attributes

W
wusongqing 已提交
76 77 78 79 80
**System capability**: SystemCapability.Multimedia.Image

| Name                   | Type   | Readable| Writable| Description                      |
| ----------------------- | ------- | ---- | ---- | -------------------------- |
| isEditable<sup>7+</sup> | boolean | Yes  | No  | Whether the image pixel map is editable.|
W
wusongqing 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

### readPixelsToBuffer<sup>7+</sup>

readPixelsToBuffer(dst: ArrayBuffer): Promise\<void>

Reads image pixel map data and writes the data to an **ArrayBuffer**. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

| Name| Type       | Mandatory| Description                                                        |
| ------ | ----------- | ---- | ------------------------------------------------------------ |
| dst    | ArrayBuffer | Yes  | Buffer to which the image pixel map data will be written.|

**Return value**

| Type          | Description                                           |
| :------------- | :---------------------------------------------- |
| Promise\<void> | Promise used to return the operation result. If the operation fails, an error message is returned.|

**Example**

```js
W
wusongqing 已提交
105 106 107
const readBuffer = new ArrayBuffer(400);
pixelmap.readPixelsToBuffer(readBuffer).then(() => {
    console.log('Succeeded in reading image pixel data.'); // Called if the condition is met.
W
wusongqing 已提交
108
}).catch(error => {
W
wusongqing 已提交
109
    ('Failed to read image pixel data.'); // Called if no condition is met.
W
wusongqing 已提交
110
})
W
wusongqing 已提交
111 112 113 114 115 116
```

### readPixelsToBuffer<sup>7+</sup>

readPixelsToBuffer(dst: ArrayBuffer, callback: AsyncCallback\<void>): void

117
Reads image pixel map data and writes the data to an **ArrayBuffer**. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

| Name  | Type                | Mandatory| Description                                                        |
| -------- | -------------------- | ---- | ------------------------------------------------------------ |
| dst      | ArrayBuffer          | Yes  | Buffer to which the image pixel map data will be written.|
| callback | AsyncCallback\<void> | Yes  | Callback used to return the operation result. If the operation fails, an error message is returned.                              |

**Example**

```js
W
wusongqing 已提交
131 132
const readBuffer = new ArrayBuffer(400);
pixelmap.readPixelsToBuffer(readBuffer, (err, res) => {
W
wusongqing 已提交
133
    if(err) {
W
wusongqing 已提交
134
        console.log('Failed to read image pixel data.');  // Called if no condition is met.
W
wusongqing 已提交
135
    } else {
W
wusongqing 已提交
136
        console.log('Succeeded in reading image pixel data.'); // Called if the condition is met.
W
wusongqing 已提交
137 138
    }
})
W
wusongqing 已提交
139 140 141 142 143 144
```

### readPixels<sup>7+</sup>

readPixels(area: PositionArea): Promise\<void>

145
Reads image pixel map data in an area. This API uses a promise to return the data read.
W
wusongqing 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

| Name| Type                          | Mandatory| Description                    |
| ------ | ------------------------------ | ---- | ------------------------ |
| area   | [PositionArea](#positionarea7) | Yes  | Area from which the image pixel map data will be read.|

**Return value**

| Type          | Description                                               |
| :------------- | :-------------------------------------------------- |
| Promise\<void> | Promise used to return the operation result. If the operation fails, an error message is returned.|

**Example**

```js
W
wusongqing 已提交
164 165 166
const area = new ArrayBuffer(400);
pixelmap.readPixels(area).then(() => {
    console.log('Succeeded in reading the image data in the area.'); // Called if the condition is met.
W
wusongqing 已提交
167
}).catch(error => {
W
wusongqing 已提交
168
    console.log('Failed to read the image data in the area.'); // Called if no condition is met.
W
wusongqing 已提交
169
})
W
wusongqing 已提交
170 171 172 173 174 175
```

### readPixels<sup>7+</sup>

readPixels(area: PositionArea, callback: AsyncCallback\<void>): void

176
Reads image pixel map data in an area. This API uses an asynchronous callback to return the data read.
W
wusongqing 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

| Name  | Type                          | Mandatory| Description                          |
| -------- | ------------------------------ | ---- | ------------------------------ |
| area     | [PositionArea](#positionarea7) | Yes  | Area from which the image pixel map data will be read.      |
| callback | AsyncCallback\<void>           | Yes  | Callback used to return the operation result. If the operation fails, an error message is returned.|

**Example**

```js
W
wusongqing 已提交
190 191
const color = new ArrayBuffer(96);
let bufferArr = new Unit8Array(color);
W
wusongqing 已提交
192 193
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts, (err, pixelmap) => {
W
wusongqing 已提交
194 195 196 197 198 199 200 201 202 203 204
    if(pixelmap == undefined){
        console.info('createPixelMap failed.');
    } else {
        const area = { pixels: new ArrayBuffer(8),
            offset: 0,
            stride: 8,
            region: { size: { height: 1, width: 2 }, x: 0, y: 0 }};
        pixelmap.readPixels(area, () => {
            console.info('readPixels success');
        })
    }
W
wusongqing 已提交
205
})
W
wusongqing 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219
```

### writePixels<sup>7+</sup>

writePixels(area: PositionArea): Promise\<void>

Writes image pixel map data to an area. This API uses a promise to return the operation result.

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

| Name| Type                          | Mandatory| Description                |
| ------ | ------------------------------ | ---- | -------------------- |
220
| area   | [PositionArea](#positionarea7) | Yes  | Area to which the image pixel map data will be written.|
W
wusongqing 已提交
221 222 223 224 225 226 227 228 229 230

**Return value**

| Type          | Description                                               |
| :------------- | :-------------------------------------------------- |
| Promise\<void> | Promise used to return the operation result. If the operation fails, an error message is returned.|

**Example**

```js
W
wusongqing 已提交
231
const color = new ArrayBuffer(96);
W
wusongqing 已提交
232
let bufferArr = new Unit8Array(color);
W
wusongqing 已提交
233 234 235 236
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts)
    .then( pixelmap => {
        if (pixelmap == undefined) {
W
wusongqing 已提交
237
            console.info('createPixelMap failed.');
W
wusongqing 已提交
238 239 240 241 242 243
        }
        const area = { pixels: new ArrayBuffer(8),
            offset: 0,
            stride: 8,
            region: { size: { height: 1, width: 2 }, x: 0, y: 0 }
        }
W
wusongqing 已提交
244
        let bufferArr = new Uint8Array(area.pixels);
W
wusongqing 已提交
245 246 247 248 249 250 251 252 253 254 255 256
        for (var i = 0; i < bufferArr.length; i++) {
            bufferArr[i] = i + 1;
        }

        pixelmap.writePixels(area).then(() => {
            const readArea = { pixels: new ArrayBuffer(8),
                offset: 0,
                stride: 8,
                // region.size.width + x < opts.width, region.size.height + y < opts.height
                region: { size: { height: 1, width: 2 }, x: 0, y: 0 }
            }        
        })
W
wusongqing 已提交
257
    }).catch(error => {
W
wusongqing 已提交
258 259
        console.log('error: ' + error);
    })
W
wusongqing 已提交
260 261 262 263 264 265
```

### writePixels<sup>7+</sup>

writePixels(area: PositionArea, callback: AsyncCallback\<void>): void

266
Writes image pixel map data to an area. This API uses an asynchronous callback to return the operation result.
W
wusongqing 已提交
267 268 269 270 271 272 273

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

| Name   | Type                          | Mandatory| Description                          |
| --------- | ------------------------------ | ---- | ------------------------------ |
274
| area      | [PositionArea](#positionarea7) | Yes  | Area to which the image pixel map data will be written.          |
W
wusongqing 已提交
275 276 277 278 279
| callback: | AsyncCallback\<void>           | Yes  | Callback used to return the operation result. If the operation fails, an error message is returned.|

**Example**

```js
W
wusongqing 已提交
280 281 282 283 284 285 286 287 288 289 290 291
const area = new ArrayBuffer(400);
pixelmap.writePixels(area, (error) => {
    if (error!=undefined) {
		console.info('Failed to write pixelmap into the specified area.');
	} else {
	    const readArea = {
            pixels: new ArrayBuffer(20),
            offset: 0,
            stride: 8,
            region: { size: { height: 1, width: 2 }, x: 0, y: 0 },
        }
	}
W
wusongqing 已提交
292
})
W
wusongqing 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
```

### writeBufferToPixels<sup>7+</sup>

writeBufferToPixels(src: ArrayBuffer): Promise\<void>

Reads image data in an **ArrayBuffer** and writes the data to a **PixelMap** object. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

| Name| Type       | Mandatory| Description          |
| ------ | ----------- | ---- | -------------- |
| src    | ArrayBuffer | Yes  | Buffer from which the image data will be read.|

**Return value**

| Type          | Description                                           |
| -------------- | ----------------------------------------------- |
| Promise\<void> | Promise used to return the operation result. If the operation fails, an error message is returned.|

**Example**

```js
W
wusongqing 已提交
318 319 320 321
const color = new ArrayBuffer(96);
const pixelMap = new ArrayBuffer(400);
let bufferArr = new Unit8Array(color);
pixelMap.writeBufferToPixels(color).then(() => {
W
wusongqing 已提交
322 323 324
    console.log("Succeeded in writing data from a buffer to a PixelMap.");
}).catch((err) => {
    console.error("Failed to write data from a buffer to a PixelMap.");
W
wusongqing 已提交
325
})
W
wusongqing 已提交
326 327 328 329 330 331
```

### writeBufferToPixels<sup>7+</sup>

writeBufferToPixels(src: ArrayBuffer, callback: AsyncCallback\<void>): void

332
Reads image data in an **ArrayBuffer** and writes the data to a **PixelMap** object. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

| Name  | Type                | Mandatory| Description                          |
| -------- | -------------------- | ---- | ------------------------------ |
| src      | ArrayBuffer          | Yes  | Buffer from which the image data will be read.                |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the operation result. If the operation fails, an error message is returned.|

**Example**

```js
W
wusongqing 已提交
346 347 348 349
const color = new ArrayBuffer(96);\
const pixelMap = new ArrayBuffer(400);
let bufferArr = new Unit8Array(color);
pixelMap.writeBufferToPixels(color, function(err) {
W
wusongqing 已提交
350 351 352
    if (err) {
        console.error("Failed to write data from a buffer to a PixelMap.");
        return;
W
wusongqing 已提交
353 354 355
    } else {
		console.log("Succeeded in writing data from a buffer to a PixelMap.");
	}
W
wusongqing 已提交
356 357 358 359 360 361 362
});
```

### getImageInfo<sup>7+</sup>

getImageInfo(): Promise\<ImageInfo>

363
Obtains pixel map information of this image. This API uses a promise to return the information.
W
wusongqing 已提交
364 365 366 367 368 369 370 371 372 373 374 375

**System capability**: SystemCapability.Multimedia.Image

**Return value**

| Type                             | Description                                                       |
| --------------------------------- | ----------------------------------------------------------- |
| Promise\<[ImageInfo](#imageinfo)> | Promise used to return the pixel map information. If the operation fails, an error message is returned.|

**Example**

```js
W
wusongqing 已提交
376 377
const pixelMap = new ArrayBuffer(400);
pixelMap.getImageInfo().then(function(info) {
W
wusongqing 已提交
378 379 380 381 382 383 384 385 386 387
    console.log("Succeeded in obtaining the image pixel map information.");
}).catch((err) => {
    console.error("Failed to obtain the image pixel map information.");
});
```

### getImageInfo<sup>7+</sup>

getImageInfo(callback: AsyncCallback\<ImageInfo>): void

388
Obtains pixel map information of this image. This API uses an asynchronous callback to return the information.
W
wusongqing 已提交
389 390 391 392 393 394 395 396 397 398 399 400

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

| Name  | Type                                   | Mandatory| Description                                                        |
| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
| callback | AsyncCallback\<[ImageInfo](#imageinfo)> | Yes  | Callback used to return the pixel map information. If the operation fails, an error message is returned.|

**Example**

```js
W
wusongqing 已提交
401
pixelmap.getImageInfo((imageInfo) => { 
W
wusongqing 已提交
402
    console.log("Succeeded in obtaining the image pixel map information..");
W
wusongqing 已提交
403
})
W
wusongqing 已提交
404 405 406 407 408 409
```

### getBytesNumberPerRow<sup>7+</sup>

getBytesNumberPerRow(): number

410
Obtains the number of bytes per line of the image pixel map.
W
wusongqing 已提交
411 412 413 414 415 416 417

**System capability**: SystemCapability.Multimedia.Image

**Return value**

| Type  | Description                |
| ------ | -------------------- |
418
| number | Number of bytes per line.|
W
wusongqing 已提交
419 420 421 422

**Example**

```js
W
wusongqing 已提交
423 424 425 426
const color = new ArrayBuffer(96);
let bufferArr = new Unit8Array(color);
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts, (err,pixelmap) => {
W
wusongqing 已提交
427 428
    let rowCount = pixelmap.getBytesNumberPerRow();
})
W
wusongqing 已提交
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
```

### getPixelBytesNumber<sup>7+</sup>

getPixelBytesNumber(): number

Obtains the total number of bytes of the image pixel map.

**System capability**: SystemCapability.Multimedia.Image

**Return value**

| Type  | Description                |
| ------ | -------------------- |
| number | Total number of bytes.|

**Example**

```js
W
wusongqing 已提交
448
let pixelBytesNumber = pixelmap.getPixelBytesNumber();
W
wusongqing 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
```

### release<sup>7+</sup>

release():Promise\<void>

Releases this **PixelMap** object. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.Image

**Return value**

| Type          | Description              |
| -------------- | ------------------ |
| Promise\<void> | Promise used to return the operation result.|

**Example**

```js
W
wusongqing 已提交
468 469 470
const color = new ArrayBuffer(96);
let bufferArr = new Unit8Array(color);
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
W
wusongqing 已提交
471 472
image.createPixelMap(color, opts, (pixelmap) => {
    pixelmap.release().then(() => {
W
wusongqing 已提交
473
	    console.log('Succeeded in releasing pixelmap object.');
W
wusongqing 已提交
474
    }).catch(error => {
W
wusongqing 已提交
475
	    console.log('Failed to release pixelmap object.');
W
wusongqing 已提交
476 477
    })
})
W
wusongqing 已提交
478 479 480 481 482 483
```

### release<sup>7+</sup>

release(callback: AsyncCallback\<void>): void

484
Releases this **PixelMap** object. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
485 486 487 488 489 490 491 492 493 494 495 496

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

| Name    | Type                | Mandatory| Description              |
| -------- | -------------------- | ---- | ------------------ |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the operation result.|

**Example**

```js
W
wusongqing 已提交
497 498 499
const color = new ArrayBuffer(96);
let bufferArr = new Unit8Array(color);
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
W
wusongqing 已提交
500 501
image.createPixelMap(color, opts, (pixelmap) => {
    pixelmap.release().then(() => {
W
wusongqing 已提交
502
	    console.log('Succeeded in releasing pixelmap object.');
W
wusongqing 已提交
503
    }).catch(error => {
W
wusongqing 已提交
504
	    console.log('Failed to release pixelmap object.');
W
wusongqing 已提交
505 506
    })
})
W
wusongqing 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519 520
```

## image.createImageSource

createImageSource(uri: string): ImageSource

Creates an **ImageSource** instance based on the URI.

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

| Name| Type  | Mandatory| Description                              |
| ------ | ------ | ---- | ---------------------------------- |
W
wusongqing 已提交
521
| uri    | string | Yes  | Image path. Currently, only the application sandbox path is supported.|
W
wusongqing 已提交
522 523 524

**Return value**

W
wusongqing 已提交
525 526 527
| Type                       | Description                                        |
| --------------------------- | -------------------------------------------- |
| [ImageSource](#imagesource) | Returns the **ImageSource** instance if the operation is successful; returns **undefined** otherwise.|
W
wusongqing 已提交
528 529 530 531

**Example**

```js
W
wusongqing 已提交
532 533
let path = this.context.getApplicationContext().fileDirs + "test.jpg";
const imageSourceApi = image.createImageSource(path);
W
wusongqing 已提交
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
```

## image.createImageSource<sup>7+</sup>

createImageSource(fd: number): ImageSource

Creates an **ImageSource** instance based on the file descriptor.

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

| Name| Type  | Mandatory| Description          |
| ------ | ------ | ---- | -------------- |
| fd     | number | Yes  | File descriptor.|

**Return value**

W
wusongqing 已提交
552 553 554
| Type                       | Description                                        |
| --------------------------- | -------------------------------------------- |
| [ImageSource](#imagesource) | Returns the **ImageSource** instance if the operation is successful; returns **undefined** otherwise.|
W
wusongqing 已提交
555 556 557 558

**Example**

```js
W
wusongqing 已提交
559
const imageSourceApi = image.createImageSource(0);
W
wusongqing 已提交
560 561 562 563 564 565 566 567
```

## ImageSource

Provides APIs to obtain image information. Before calling any API in **ImageSource**, you must use **createImageSource** to create an **ImageSource** instance.

### Attributes

W
wusongqing 已提交
568 569
**System capability**: SystemCapability.Multimedia.Image

W
wusongqing 已提交
570 571
| Name            | Type          | Readable| Writable| Description                                                        |
| ---------------- | -------------- | ---- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
572
| supportedFormats | Array\<string> | Yes  | No  | Supported image formats, including png, jpeg, wbmp, bmp, gif, webp, and heif.|
W
wusongqing 已提交
573 574 575 576 577

### getImageInfo

getImageInfo(index: number, callback: AsyncCallback\<ImageInfo>): void

578
Obtains information about an image with the specified index. This API uses an asynchronous callback to return the information.
W
wusongqing 已提交
579 580 581 582 583 584 585 586 587 588 589 590 591

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

| Name  | Type                                  | Mandatory| Description                                    |
| -------- | -------------------------------------- | ---- | ---------------------------------------- |
| index    | number                                 | Yes  | Index of the image.                    |
| callback | AsyncCallback<[ImageInfo](#imageinfo)> | Yes  | Callback used to return the image information.|

**Example**

```js
W
wusongqing 已提交
592 593 594 595 596 597 598
imageSourceApi.getImageInfo(0,(error, imageInfo) => { 
    if(error) {
        console.log('getImageInfo failed.');
    } else {
        console.log('getImageInfo succeeded.');
    }
})
W
wusongqing 已提交
599 600 601 602 603 604
```

### getImageInfo

getImageInfo(callback: AsyncCallback\<ImageInfo>): void

605
Obtains information about this image. This API uses an asynchronous callback to return the information.
W
wusongqing 已提交
606 607 608 609 610 611 612 613 614 615 616 617

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

| Name    | Type                                  | Mandatory| Description                                    |
| -------- | -------------------------------------- | ---- | ---------------------------------------- |
| callback | AsyncCallback<[ImageInfo](#imageinfo)> | Yes  | Callback used to return the image information.|

**Example**

```js
W
wusongqing 已提交
618
imageSourceApi.getImageInfo(imageInfo => { 
W
wusongqing 已提交
619
    console.log('Succeeded in obtaining the image information.');
W
wusongqing 已提交
620
})
W
wusongqing 已提交
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
```

### getImageInfo

getImageInfo(index?: number): Promise\<ImageInfo>

Obtains information about an image with the specified index. This API uses a promise to return the image information.

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

| Name | Type  | Mandatory| Description                                 |
| ----- | ------ | ---- | ------------------------------------- |
| index | number | No  | Index of the image. If this parameter is not set, the default value **0** is used.|

**Return value**

| Type                            | Description                  |
| -------------------------------- | ---------------------- |
| Promise<[ImageInfo](#imageinfo)> | Promise used to return the image information.|

**Example**

```js
imageSourceApi.getImageInfo(0)
W
wusongqing 已提交
647
    .then(imageInfo => {
W
wusongqing 已提交
648
		console.log('Succeeded in obtaining the image information.');
W
wusongqing 已提交
649
	}).catch(error => {
W
wusongqing 已提交
650
		console.log('Failed to obtain the image information.');
W
wusongqing 已提交
651
	})
W
wusongqing 已提交
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
```

### getImageProperty<sup>7+</sup>

getImageProperty(key:string, options?: GetImagePropertyOptions): Promise\<string>

Obtains the value of a property with the specified index in this image. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.Image

 **Parameters**

| Name   | Type                                                | Mandatory| Description                                |
| ------- | ---------------------------------------------------- | ---- | ------------------------------------ |
| key     | string                                               | Yes  | Name of the property.                        |
| options | [GetImagePropertyOptions](#getimagepropertyoptions7) | No  | Image properties, including the image index and default property value.|

**Return value**

| Type            | Description                                                        |
| ---------------- | ------------------------------------------------------------ |
| Promise\<string> | Promise used to return the property value. If the operation fails, the default value is returned.|

**Example**

```js
W
wusongqing 已提交
678
imageSourceApi.getImageProperty("BitsPerSample")
W
wusongqing 已提交
679
    .then(data => {
W
wusongqing 已提交
680
		console.log('Succeeded in getting the value of the specified attribute key of the image.');
W
wusongqing 已提交
681
	})
W
wusongqing 已提交
682 683 684 685 686 687
```

### getImageProperty<sup>7+</sup>

getImageProperty(key:string, callback: AsyncCallback\<string>): void

688
Obtains the value of a property with the specified index in this image. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
689 690 691 692 693 694 695 696 697 698 699 700 701

**System capability**: SystemCapability.Multimedia.Image

 **Parameters**

| Name  | Type                  | Mandatory| Description                                                        |
| -------- | ---------------------- | ---- | ------------------------------------------------------------ |
| key      | string                 | Yes  | Name of the property.                                                |
| callback | AsyncCallback\<string> | Yes  | Callback used to return the property value. If the operation fails, the default value is returned.|

**Example**

```js
W
wusongqing 已提交
702 703
imageSourceApi.getImageProperty("BitsPerSample",(error,data) => { 
    if(error) {
W
wusongqing 已提交
704
        console.log('Failed to get the value of the specified attribute key of the image.');
W
wusongqing 已提交
705
    } else {
W
wusongqing 已提交
706
        console.log('Succeeded in getting the value of the specified attribute key of the image.');
W
wusongqing 已提交
707 708
    }
})
W
wusongqing 已提交
709 710 711 712 713 714
```

### getImageProperty<sup>7+</sup>

getImageProperty(key:string, options: GetImagePropertyOptions, callback: AsyncCallback\<string>): void

715
Obtains the value of a property in this image. This API uses an asynchronous callback to return the property value in a string.
W
wusongqing 已提交
716 717 718 719 720 721 722 723 724 725 726 727 728 729

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

| Name  | Type                                                | Mandatory| Description                                                        |
| -------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------ |
| key      | string                                               | Yes  | Name of the property.                                                |
| options  | [GetImagePropertyOptions](#getimagepropertyoptions7) | Yes  | Image properties, including the image index and default property value.                        |
| callback | AsyncCallback\<string>                               | Yes  | Callback used to return the property value. If the operation fails, the default value is returned.|

**Example**

```js
W
wusongqing 已提交
730 731
const property = new ArrayBuffer(400);
imageSourceApi.getImageProperty("BitsPerSample",property,(error,data) => { 
W
wusongqing 已提交
732
    if(error) {
W
wusongqing 已提交
733
        console.log('Failed to get the value of the specified attribute key of the image.');
W
wusongqing 已提交
734
    } else {
W
wusongqing 已提交
735
        console.log('Succeeded in getting the value of the specified attribute key of the image.');
W
wusongqing 已提交
736 737
    }
})
W
wusongqing 已提交
738 739 740 741
```

### createPixelMap<sup>7+</sup>

W
wusongqing 已提交
742
createPixelMap(options?: DecodingOptions): Promise\<PixelMap>
W
wusongqing 已提交
743

744
Creates a **PixelMap** object based on image decoding parameters. This API uses a promise to return the result.
W
wusongqing 已提交
745 746 747 748 749

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

W
wusongqing 已提交
750 751 752 753 754 755 756 757 758
| Name   | Type                                | Mandatory| Description      |
| ------- | ------------------------------------ | ---- | ---------- |
| options | [DecodingOptions](#decodingoptions7) | No  | Image decoding parameters.|

**Return value**

| Type                            | Description                 |
| -------------------------------- | --------------------- |
| Promise\<[PixelMap](#pixelmap7)> | Promise used to return the **PixelMap** object.|
W
wusongqing 已提交
759 760 761 762

**Example**

```js
W
wusongqing 已提交
763
imageSourceApi.createPixelMap().then(pixelmap => {
W
wusongqing 已提交
764
    console.log('Succeeded in creating pixelmap object through image decoding parameters.');
W
wusongqing 已提交
765
}).catch(error => {
W
wusongqing 已提交
766
    console.log('Failed to create pixelmap object through image decoding parameters.');
W
wusongqing 已提交
767
})
W
wusongqing 已提交
768 769 770 771
```

### createPixelMap<sup>7+</sup>

W
wusongqing 已提交
772
createPixelMap(callback: AsyncCallback\<PixelMap>): void
W
wusongqing 已提交
773

774
Creates a **PixelMap** object based on the default parameters. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
775 776 777 778 779

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

W
wusongqing 已提交
780 781 782
| Name    | Type                                 | Mandatory| Description                      |
| -------- | ------------------------------------- | ---- | -------------------------- |
| callback | AsyncCallback<[PixelMap](#pixelmap7)> | Yes  | Callback used to return the **PixelMap** object.|
W
wusongqing 已提交
783 784 785 786

**Example**

```js
W
wusongqing 已提交
787
imageSourceApi.createPixelMap(pixelmap => { 
W
wusongqing 已提交
788
    console.log('Succeeded in creating pixelmap object.');
W
wusongqing 已提交
789
}).catch(error => {
W
wusongqing 已提交
790
    console.log('Failed to create pixelmap object.');
W
wusongqing 已提交
791
})
W
wusongqing 已提交
792 793 794 795
```

### createPixelMap<sup>7+</sup>

W
wusongqing 已提交
796
createPixelMap(options: DecodingOptions, callback: AsyncCallback\<PixelMap>): void
W
wusongqing 已提交
797

798
Creates a **PixelMap** object based on image decoding parameters. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
799 800 801 802 803

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

W
wusongqing 已提交
804 805 806 807
| Name    | Type                                 | Mandatory| Description                      |
| -------- | ------------------------------------- | ---- | -------------------------- |
| options  | [DecodingOptions](#decodingoptions7)  | Yes  | Image decoding parameters.                |
| callback | AsyncCallback<[PixelMap](#pixelmap7)> | Yes  | Callback used to return the **PixelMap** object.|
W
wusongqing 已提交
808 809 810 811

**Example**

```js
W
wusongqing 已提交
812
const decodingOptions = new ArrayBuffer(400);
W
wusongqing 已提交
813
imageSourceApi.createPixelMap(decodingOptions, pixelmap => { 
W
wusongqing 已提交
814 815
    console.log('Succeeded in creating pixelmap object.');
})
W
wusongqing 已提交
816 817 818 819 820 821
```

### release

release(callback: AsyncCallback\<void>): void

822
Releases this **ImageSource** instance. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
823 824 825 826 827 828 829 830 831 832 833 834

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

| Name    | Type                | Mandatory| Description                              |
| -------- | -------------------- | ---- | ---------------------------------- |
| callback | AsyncCallback\<void> | Yes  | Callback invoked for instance release. If the operation fails, an error message is returned.|

**Example**

```js
W
wusongqing 已提交
835 836 837
imageSourceApi.release(() => { 
    console.log('release succeeded.');
})
W
wusongqing 已提交
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
```

### release

release(): Promise\<void>

Releases this **ImageSource** instance. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.Image

**Return value**

| Type          | Description                       |
| -------------- | --------------------------- |
| Promise\<void> | Promise used to return the operation result.|

**Example**

```js
W
wusongqing 已提交
857
imageSourceApi.release().then(()=>{
W
wusongqing 已提交
858
    console.log('Succeeded in releasing the image source instance.');
W
wusongqing 已提交
859
}).catch(error => {
W
wusongqing 已提交
860
    console.log('Failed to release the image source instance.');
W
wusongqing 已提交
861
})
W
wusongqing 已提交
862 863 864 865 866 867 868 869 870 871 872 873
```

## image.createImagePacker

createImagePacker(): ImagePacker

Creates an **ImagePacker** instance.

**System capability**: SystemCapability.Multimedia.Image

**Return value**

W
wusongqing 已提交
874 875 876
| Type                       | Description                 |
| --------------------------- | --------------------- |
| [ImagePacker](#imagepacker) | **ImagePacker** instance created.|
W
wusongqing 已提交
877 878 879 880 881 882 883 884 885 886 887 888 889

**Example**

```js
const imagePackerApi = image.createImagePacker();
```

## ImagePacker

Provide APIs to pack images. Before calling any API in **ImagePacker**, you must use **createImagePacker** to create an **ImagePacker** instance.

### Attributes

W
wusongqing 已提交
890 891 892 893 894
**System capability**: SystemCapability.Multimedia.Image

| Name            | Type          | Readable| Writable| Description                      |
| ---------------- | -------------- | ---- | ---- | -------------------------- |
| supportedFormats | Array\<string> | Yes  | No  | Supported image format, which can be jpeg.|
W
wusongqing 已提交
895 896 897

### packing

W
wusongqing 已提交
898
packing(source: ImageSource, option: PackingOption, callback: AsyncCallback\<ArrayBuffer>): void
W
wusongqing 已提交
899

900
Packs an image. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
901 902 903 904 905 906 907 908 909

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

| Name  | Type                              | Mandatory| Description                              |
| -------- | ---------------------------------- | ---- | ---------------------------------- |
| source   | [ImageSource](#imagesource)        | Yes  | Image to pack.                    |
| option   | [PackingOption](#packingoption)    | Yes  | Option for image packing.                    |
W
wusongqing 已提交
910
| callback | AsyncCallback\<ArrayBuffer>        | Yes  | Callback used to return the packed data.|
W
wusongqing 已提交
911 912 913 914

**Example**

```js
W
wusongqing 已提交
915 916 917
let packOpts = { format:"image/jpeg", quality:98 };
const imageSourceApi = new ArrayBuffer(400);
imagePackerApi.packing(imageSourceApi, packOpts, data => {})
W
wusongqing 已提交
918 919 920 921
```

### packing

W
wusongqing 已提交
922
packing(source: ImageSource, option: PackingOption): Promise\<ArrayBuffer>
W
wusongqing 已提交
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938

Packs an image. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

| Name| Type                           | Mandatory| Description          |
| ------ | ------------------------------- | ---- | -------------- |
| source | [ImageSource](#imagesource)     | Yes  | Image to pack.|
| option | [PackingOption](#packingoption) | Yes  | Option for image packing.|

**Return value**

| Type                        | Description                                         |
| :--------------------------- | :-------------------------------------------- |
W
wusongqing 已提交
939
| Promise\<ArrayBuffer> | Promise used to return the packed data.|
W
wusongqing 已提交
940 941 942 943

**Example**

```js
W
wusongqing 已提交
944 945 946
let packOpts = { format:"image/jpeg", quality:98 }
const imageSourceApi = new ArrayBuffer(400);
imagePackerApi.packing(imageSourceApi, packOpts)
W
wusongqing 已提交
947 948 949 950 951
    .then( data => {
        console.log('packing succeeded.');
	}).catch(error => {
	    console.log('packing failed.');
	})
W
wusongqing 已提交
952 953
```

W
wusongqing 已提交
954
### packing<sup>8+</sup>
W
wusongqing 已提交
955 956

packing(source: PixelMap, option: PackingOption, callback: AsyncCallback\<ArrayBuffer>): void
W
wusongqing 已提交
957

958
Packs an image. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
959 960 961 962 963 964 965

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

| Name  | Type                           | Mandatory| Description                              |
| -------- | ------------------------------- | ---- | ---------------------------------- |
W
wusongqing 已提交
966
| source   | [PixelMap](#pixelmap)           | Yes  | **PixelMap** object to pack.              |
W
wusongqing 已提交
967
| option   | [PackingOption](#packingoption) | Yes  | Option for image packing.                    |
W
wusongqing 已提交
968
| callback | AsyncCallback\<ArrayBuffer>     | Yes  | Callback used to return the packed data.|
W
wusongqing 已提交
969 970 971 972

**Example**

```js
W
wusongqing 已提交
973 974 975 976
let packOpts = { format:"image/jpeg", quality:98 }
const pixelMapApi = new ArrayBuffer(400);
imagePackerApi.packing(pixelMapApi, packOpts, data => { 
    console.log('Succeeded in packing the image.');
W
wusongqing 已提交
977
}).catch(error => {
W
wusongqing 已提交
978
	console.log('Failed to pack the image.');
W
wusongqing 已提交
979
})
W
wusongqing 已提交
980 981
```

W
wusongqing 已提交
982
### packing<sup>8+</sup>
W
wusongqing 已提交
983

W
wusongqing 已提交
984
packing(source: PixelMap, option: PackingOption): Promise\<ArrayBuffer>
W
wusongqing 已提交
985 986 987 988 989 990 991

Packs an image. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

W
wusongqing 已提交
992 993
| Name| Type                           | Mandatory| Description              |
| ------ | ------------------------------- | ---- | ------------------ |
W
wusongqing 已提交
994
| source | [PixelMap](#pixelmap)           | Yes  | **PixelMap** object to pack.|
W
wusongqing 已提交
995
| option | [PackingOption](#packingoption) | Yes  | Option for image packing.    |
W
wusongqing 已提交
996 997 998

**Return value**

W
wusongqing 已提交
999 1000
| Type                        | Description                                         |
| :--------------------------- | :-------------------------------------------- |
W
wusongqing 已提交
1001
| Promise\<ArrayBuffer> | Promise used to return the packed data.|
W
wusongqing 已提交
1002 1003 1004 1005

**Example**

```js
W
wusongqing 已提交
1006 1007 1008
let packOpts = { format:"image/jpeg", quality:98 }
const pixelMapApi = new ArrayBuffer(400);
imagePackerApi.packing(pixelMapApi, packOpts)
W
wusongqing 已提交
1009
    .then( data => {
W
wusongqing 已提交
1010
	    console.log('Succeeded in packing the image.');
W
wusongqing 已提交
1011
	}).catch(error => {
W
wusongqing 已提交
1012
	    console.log('Failed to pack the image..');
W
wusongqing 已提交
1013
	})
W
wusongqing 已提交
1014 1015 1016 1017 1018 1019
```

### release

release(callback: AsyncCallback\<void>): void

1020
Releases this **ImagePacker** instance. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032

**System capability**: SystemCapability.Multimedia.Image

**Parameters**

| Name  | Type                | Mandatory| Description                          |
| -------- | -------------------- | ---- | ------------------------------ |
| callback | AsyncCallback\<void> | Yes  | Callback invoked for instance release. If the operation fails, an error message is returned.|

**Example**

```js
W
wusongqing 已提交
1033
imagePackerApi.release(()=>{ 
W
wusongqing 已提交
1034
    console.log('Succeeded in releasing image packaging.');
W
wusongqing 已提交
1035
})
W
wusongqing 已提交
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
```

### release

release(): Promise\<void>

Releases this **ImagePacker** instance. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.Image

**Return value**

| Type          | Description                                                   |
| :------------- | :------------------------------------------------------ |
| Promise\<void> | Promise used to return the instance release result. If the operation fails, an error message is returned.|

**Example**

```js
W
wusongqing 已提交
1055
imagePackerApi.release().then(()=>{
W
wusongqing 已提交
1056
    console.log('Succeeded in releasing image packaging.');
W
wusongqing 已提交
1057
}).catch((error)=>{ 
W
wusongqing 已提交
1058
    console.log('Failed to release image packaging.'); 
W
wusongqing 已提交
1059
}) 
W
wusongqing 已提交
1060 1061
```

W
wusongqing 已提交
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
## image.createImageReceiver<sup>9+</sup>

createImageReceiver(width: number, height: number, format: number, capacity: number): ImageReceiver

Create an **ImageReceiver** instance by specifying the image width, height, format, and capacity.

**System capability**: SystemCapability.Multimedia.Image.ImageReceiver

**Parameters**

| Name    | Type  | Mandatory| Description                  |
| -------- | ------ | ---- | ---------------------- |
| width    | number | Yes  | Default image width.      |
| height   | number | Yes  | Default image height.      |
| format   | number | Yes  | Image format.            |
| capacity | number | Yes  | Maximum number of images that can be accessed at the same time.|

**Return value**

| Type                            | Description                                   |
| -------------------------------- | --------------------------------------- |
| [ImageReceiver](#imagereceiver9) | Returns an **ImageReceiver** instance if the operation is successful.|

**Example**

```js
W
wusongqing 已提交
1088
var receiver = image.createImageReceiver(8192, 8, 4, 8);
W
wusongqing 已提交
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
```

## ImageReceiver<sup>9+</sup>

Provides APIs to obtain the surface ID of a component, read the latest image, read the next image, and release the **ImageReceiver** instance.

Before calling any APIs in **ImageReceiver**, you must create an **ImageReceiver** instance.

### Attributes

**System capability**: SystemCapability.Multimedia.Image.ImageReceiver

| Name                 | Type                        | Readable| Writable| Description              |
| --------------------- | ---------------------------- | ---- | ---- | ------------------ |
| size<sup>9+</sup>     | [Size](#size)                | Yes  | No  | Image size.        |
| capacity<sup>9+</sup> | number                       | Yes  | No  | Maximum number of images that can be accessed at the same time.|
| format<sup>9+</sup>   | [ImageFormat](#imageformat9) | Yes  | No  | Image format.        |

### getReceivingSurfaceId<sup>9+</sup>

getReceivingSurfaceId(callback: AsyncCallback\<string>): void

Obtains a surface ID for the camera or other components. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Multimedia.Image.ImageReceiver

**Parameters**

| Name    | Type                  | Mandatory| Description                      |
| -------- | ---------------------- | ---- | -------------------------- |
| callback | AsyncCallback\<string> | Yes  | Callback used to return the surface ID.|

**Example**

```js
W
wusongqing 已提交
1124
receiver.getReceivingSurfaceId((err, id) => { 
W
wusongqing 已提交
1125 1126 1127 1128 1129 1130
    if(err) {
        console.log('getReceivingSurfaceId failed.');
    } else {
        console.log('getReceivingSurfaceId succeeded.');
    }
});
W
wusongqing 已提交
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
```

### getReceivingSurfaceId<sup>9+</sup>

getReceivingSurfaceId(): Promise\<string>

Obtains a surface ID for the camera or other components. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.Image.ImageReceiver

**Return value**

| Type            | Description                |
| ---------------- | -------------------- |
| Promise\<string> | Promise used to return the surface ID.|

**Example**

```js
receiver.getReceivingSurfaceId().then( id => { 
W
wusongqing 已提交
1151 1152 1153 1154
    console.log('getReceivingSurfaceId succeeded.');
}).catch(error => {
    console.log('getReceivingSurfaceId failed.');
})
W
wusongqing 已提交
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
```

### readLatestImage<sup>9+</sup>

readLatestImage(callback: AsyncCallback\<Image>): void

Reads the latest image from the **ImageReceiver** instance. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Multimedia.Image.ImageReceiver

**Parameters**

| Name    | Type                           | Mandatory| Description                    |
| -------- | ------------------------------- | ---- | ------------------------ |
| callback | AsyncCallback<[Image](#image9)> | Yes  | Callback used to return the latest image.|

**Example**

```js
W
wusongqing 已提交
1174 1175 1176 1177 1178 1179 1180
receiver.readLatestImage((err, img) => { 
    if(err) {
        console.log('readLatestImage failed.');
    } else {
        console.log('readLatestImage succeeded.');
    }
});
W
wusongqing 已提交
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
```

### readLatestImage<sup>9+</sup>

readLatestImage(): Promise\<Image>

Reads the latest image from the **ImageReceiver** instance. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.Image.ImageReceiver

**Return value**

| Type                     | Description              |
| ------------------------- | ------------------ |
| Promise<[Image](#image8)> | Promise used to return the latest image.|

**Example**

```js
W
wusongqing 已提交
1200 1201 1202 1203 1204
receiver.readLatestImage().then(img => {
    console.log('readLatestImage succeeded.');
}).catch(error => {
    console.log('readLatestImage failed.');
})
W
wusongqing 已提交
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
```

### readNextImage<sup>9+</sup>

readNextImage(callback: AsyncCallback\<Image>): void

Reads the next image from the **ImageReceiver** instance. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Multimedia.Image.ImageReceiver

**Parameters**

| Name    | Type                           | Mandatory| Description                      |
| -------- | ------------------------------- | ---- | -------------------------- |
| callback | AsyncCallback<[Image](#image9)> | Yes  | Callback used to return the next image.|

**Example**

```js
W
wusongqing 已提交
1224 1225 1226 1227 1228 1229 1230
receiver.readNextImage((err, img) => { 
    if(err) {
        console.log('readNextImage failed.');
    } else {
        console.log('readNextImage succeeded.');
    }
});
W
wusongqing 已提交
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
```

### readNextImage<sup>9+</sup>

readNextImage(): Promise\<Image>

Reads the next image from the **ImageReceiver** instance. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.Image.ImageReceiver

**Return value**

| Type                     | Description                |
| ------------------------- | -------------------- |
| Promise<[Image](#image9)> | Promise used to return the next image.|

**Example**

```js
W
wusongqing 已提交
1250 1251 1252 1253 1254
receiver.readNextImage().then(img => {
    console.log('readNextImage succeeded.');
}).catch(error => {
    console.log('readNextImage failed.');
})
W
wusongqing 已提交
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
```

### on('imageArrival')<sup>9+</sup>

on(type: 'imageArrival', callback: AsyncCallback\<void>): void

Listens for image arrival events.

**System capability**: SystemCapability.Multimedia.Image.ImageReceiver

**Parameters**

| Name    | Type                | Mandatory| Description                                                  |
| -------- | -------------------- | ---- | ------------------------------------------------------ |
| type     | string               | Yes  | Type of event to listen for. The value is fixed at **imageArrival**, which is triggered when an image is received.|
| callback | AsyncCallback\<void> | Yes  | Callback invoked for the event.                                      |

**Example**

```js
W
wusongqing 已提交
1275
receiver.on('imageArrival', () => {})
W
wusongqing 已提交
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
```

### release<sup>9+</sup>

release(callback: AsyncCallback\<void>): void

Releases this **ImageReceiver** instance. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Multimedia.Image.ImageReceiver

**Parameters**

| Name    | Type                | Mandatory| Description                    |
| -------- | -------------------- | ---- | ------------------------ |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|

**Example**

```js
W
wusongqing 已提交
1295
receiver.release(() => {})
W
wusongqing 已提交
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
```

### release<sup>9+</sup>

release(): Promise\<void>

Releases this **ImageReceiver** instance. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.Image.ImageReceiver

**Return value**

| Type          | Description              |
| -------------- | ------------------ |
| Promise\<void> | Promise used to return the result.|

**Example**

```js
W
wusongqing 已提交
1315 1316 1317 1318 1319
receiver.release().then(() => {
    console.log('release succeeded.');
}).catch(error => {
    console.log('release failed.');
})
W
wusongqing 已提交
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
```

## Image<sup>9+</sup>

Provides APIs for basic image operations, including obtaining image information and reading and writing image data. An **Image** instance is returned when [readNextImage](#readnextimage9) and [readLatestImage](#readlatestimage9) are called.

### Attributes

**System capability**: SystemCapability.Multimedia.Image.Core

| Name                 | Type              | Readable| Writable| Description                                              |
| --------------------- | ------------------ | ---- | ---- | -------------------------------------------------- |
| clipRect<sup>9+</sup> | [Region](#region7) | Yes  | Yes  | Image area to be cropped.                                |
| size<sup>9+</sup>     | [Size](#size)      | Yes  | No  | Image size.                                        |
| format<sup>9+</sup>   | number             | Yes  | No  | Image format. For details, see [PixelMapFormat](#pixelmapformat7).|

### getComponent<sup>9+</sup>

getComponent(componentType: ComponentType, callback: AsyncCallback\<Component>): void

Obtains the component buffer from the **Image** instance based on the color component type. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Multimedia.Image.Core

**Parameters**

| Name         | Type                                   | Mandatory| Description                |
| ------------- | --------------------------------------- | ---- | -------------------- |
| componentType | [ComponentType](#componenttype9)        | Yes  | Color component type of the image.    |
| callback      | AsyncCallback<[Component](#component9)> | Yes  | Callback used to return the component buffer.|

**Example**

```js
W
wusongqing 已提交
1354 1355 1356 1357 1358 1359 1360
img.getComponent(4, (err, component) => {
    if(err) {
        console.log('getComponent failed.');
    } else {
        console.log('getComponent succeeded.');
    }
})
W
wusongqing 已提交
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
```

### getComponent<sup>9+</sup>

getComponent(componentType: ComponentType): Promise\<Component>

Obtains the component buffer from the **Image** instance based on the color component type. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.Image.Core

**Parameters**

| Name         | Type                            | Mandatory| Description            |
| ------------- | -------------------------------- | ---- | ---------------- |
| componentType | [ComponentType](#componenttype9) | Yes  | Color component type of the image.|

**Return value**

| Type                             | Description                             |
| --------------------------------- | --------------------------------- |
| Promise<[Component](#component9)> | Promise used to return the component buffer.|

**Example**

```js
img.getComponent(4).then(component => { })
```

### release<sup>9+</sup>

release(callback: AsyncCallback\<void>): void

Releases this **Image** instance. This API uses an asynchronous callback to return the result.

The corresponding resources must be released before another image arrives.

**System capability**: SystemCapability.Multimedia.Image.Core

**Parameters**

| Name    | Type                | Mandatory| Description          |
| -------- | -------------------- | ---- | -------------- |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|

**Example**

```js
W
wusongqing 已提交
1408 1409 1410 1411 1412
img.release(() =>{ 
    console.log('release succeeded.');
}).catch(error => {
    console.log('release failed.');
}) 
W
wusongqing 已提交
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
```

### release<sup>9+</sup>

release(): Promise\<void>

Releases this **Image** instance. This API uses a promise to return the result.

The corresponding resources must be released before another image arrives.

**System capability**: SystemCapability.Multimedia.Image.Core

**Return value**

| Type          | Description                 |
| -------------- | --------------------- |
| Promise\<void> | Promise used to return the result.|

**Example**

```js
img.release().then(() =>{
W
wusongqing 已提交
1435 1436 1437 1438
    console.log('release succeeded.');
}).catch(error => {
    console.log('release failed.');
})
W
wusongqing 已提交
1439 1440
```

W
wusongqing 已提交
1441 1442 1443 1444 1445 1446
## PositionArea<sup>7+</sup>

Describes area information in an image.

**System capability**: SystemCapability.Multimedia.Image

W
wusongqing 已提交
1447 1448 1449 1450
| Name  | Type              | Readable| Writable| Description                                                        |
| ------ | ------------------ | ---- | ---- | ------------------------------------------------------------ |
| pixels | ArrayBuffer        | Yes  | No  | Pixels of the image.                                                      |
| offset | number             | Yes  | No  | Offset for data reading.                                                    |
W
wusongqing 已提交
1451 1452
| stride | number             | Yes  | No  | Number of bytes from one row of pixels in memory to the next row of pixels in memory. The value of **stride** must be greater than or equal to the value of **region.size.width** multiplied by 4.                   |
| region | [Region](#region7) | Yes  | No  | Region to read or write. The width of the region to write plus the X coordinate cannot be greater than the width of the original image. The height of the region to write plus the Y coordinate cannot be greater than the height of the original image.|
W
wusongqing 已提交
1453

W
wusongqing 已提交
1454
## ImageInfo
W
wusongqing 已提交
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476

Describes image information.

**System capability**: SystemCapability.Multimedia.Image

| Name| Type         | Readable| Writable| Description      |
| ---- | ------------- | ---- | ---- | ---------- |
| size | [Size](#size) | Yes  | Yes  | Image size.|

## Size

Describes the size of an image.

**System capability**: SystemCapability.Multimedia.Image

| Name  | Type  | Readable| Writable| Description          |
| ------ | ------ | ---- | ---- | -------------- |
| height | number | Yes  | Yes  | Image height.|
| width  | number | Yes  | Yes  | Image width.|

## PixelMapFormat<sup>7+</sup>

W
wusongqing 已提交
1477
Enumerates the pixel formats of images.
W
wusongqing 已提交
1478 1479 1480 1481 1482 1483 1484 1485 1486

**System capability**: SystemCapability.Multimedia.Image

| Name     | Default Value| Description             |
| --------- | ------ | ----------------- |
| UNKNOWN   | 0      | Unknown format.       |
| RGBA_8888 | 3      | RGBA_8888.|
| RGB_565   | 2      | RGB_565.  |

W
wusongqing 已提交
1487
## AlphaType<sup>9+</sup>
W
wusongqing 已提交
1488

W
wusongqing 已提交
1489
Enumerates the alpha types of images.
W
wusongqing 已提交
1490 1491 1492 1493 1494 1495

**System capability**: SystemCapability.Multimedia.Image

| Name    | Default Value| Description                   |
| -------- | ------ | ----------------------- |
| UNKNOWN  | 0      | Unknown alpha type.           |
1496
| OPAQUE   | 1      | There is no alpha or the image is opaque.|
W
wusongqing 已提交
1497 1498 1499
| PREMUL   | 2      | Premultiplied alpha.         |
| UNPREMUL | 3      | Unpremultiplied alpha, that is, straight alpha.       |

W
wusongqing 已提交
1500
## ScaleMode<sup>9+</sup>
W
wusongqing 已提交
1501

W
wusongqing 已提交
1502
Enumerates the scale modes of images.
W
wusongqing 已提交
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512

**System capability**: SystemCapability.Multimedia.Image

| Name           | Default Value| Description                                              |
| --------------- | ------ | -------------------------------------------------- |
| CENTER_CROP     | 1      | Scales the image so that it fills the requested bounds of the target and crops the extra.|
| FIT_TARGET_SIZE | 2      | Reduces the image size to the dimensions of the target.                          |

## InitializationOptions<sup>8+</sup>

W
wusongqing 已提交
1513 1514
Defines pixel map initialization options.

W
wusongqing 已提交
1515 1516
**System capability**: SystemCapability.Multimedia.Image

W
wusongqing 已提交
1517 1518 1519 1520 1521 1522 1523
| Name                  | Type                              | Readable| Writable| Description          |
| ---------------------- | ---------------------------------- | ---- | ---- | -------------- |
| alphaType<sup>9+</sup> | [AlphaType](#alphatype9)           | Yes  | Yes  | Alpha type.      |
| editable               | boolean                            | Yes  | Yes  | Whether the image is editable.  |
| pixelFormat            | [PixelMapFormat](#pixelmapformat7) | Yes  | Yes  | Pixel map format.    |
| scaleMode<sup>9+</sup> | [ScaleMode](#scalemode9)           | Yes  | Yes  | Scale mode.      |
| size                   | [Size](#size)                      | Yes  | Yes  | Image size.|
W
wusongqing 已提交
1524 1525 1526

## DecodingOptions<sup>7+</sup>

W
wusongqing 已提交
1527
Defines image decoding options.
W
wusongqing 已提交
1528 1529 1530 1531 1532 1533

**System capability**: SystemCapability.Multimedia.Image

| Name              | Type                              | Readable| Writable| Description            |
| ------------------ | ---------------------------------- | ---- | ---- | ---------------- |
| sampleSize         | number                             | Yes  | Yes  | Thumbnail sampling size.|
W
wusongqing 已提交
1534
| rotate             | number                             | Yes  | Yes  | Rotation angle.      |
W
wusongqing 已提交
1535 1536
| editable           | boolean                            | Yes  | Yes  | Whether the image is editable.    |
| desiredSize        | [Size](#size)                      | Yes  | Yes  | Expected output size.  |
W
wusongqing 已提交
1537
| desiredRegion      | [Region](#region7)                 | Yes  | Yes  | Region to decode.      |
W
wusongqing 已提交
1538
| desiredPixelFormat | [PixelMapFormat](#pixelmapformat7) | Yes  | Yes  | Pixel map format for decoding.|
W
wusongqing 已提交
1539
| index              | number                             | Yes  | Yes  | Index of the image to decode.  |
W
wusongqing 已提交
1540

W
wusongqing 已提交
1541
## Region<sup>7+</sup>
W
wusongqing 已提交
1542 1543 1544 1545 1546

Describes region information.

**System capability**: SystemCapability.Multimedia.Image

W
wusongqing 已提交
1547 1548 1549
| Name| Type         | Readable| Writable| Description        |
| ---- | ------------- | ---- | ---- | ------------ |
| size | [Size](#size) | Yes  | Yes  | Region size.  |
W
wusongqing 已提交
1550 1551 1552 1553 1554
| x    | number        | Yes  | Yes  | X coordinate of the region.|
| y    | number        | Yes  | Yes  | Y coordinate of the region.|

## PackingOption

W
wusongqing 已提交
1555
Defines the option for image packing.
W
wusongqing 已提交
1556 1557 1558 1559 1560 1561

**System capability**: SystemCapability.Multimedia.Image

| Name   | Type  | Readable| Writable| Description          |
| ------- | ------ | ---- | ---- | -------------- |
| format  | string | Yes  | Yes  | Format of the packed image.    |
W
wusongqing 已提交
1562
| quality | number | Yes  | Yes  | Quality of the output image during JPEG encoding. The value ranges from 1 to 100.|
W
wusongqing 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578

## GetImagePropertyOptions<sup>7+</sup>

Describes image properties.

**System capability**: SystemCapability.Multimedia.Image

| Name        | Type  | Readable| Writable| Description        |
| ------------ | ------ | ---- | ---- | ------------ |
| index        | number | Yes  | Yes  | Index of an image.  |
| defaultValue | string | Yes  | Yes  | Default property value.|

## PropertyKey<sup>7+</sup>

Describes the exchangeable image file format (Exif) information of an image.

W
wusongqing 已提交
1579 1580
**System capability**: SystemCapability.Multimedia.Image

W
wusongqing 已提交
1581 1582
| Name             | Default Value           | Description                |
| ----------------- | ----------------- | -------------------- |
1583
| BITS_PER_SAMPLE   | "BitsPerSample"   | Number of bits per pixel.    |
W
wusongqing 已提交
1584 1585 1586 1587 1588 1589 1590
| ORIENTATION       | "Orientation"     | Image orientation.          |
| IMAGE_LENGTH      | "ImageLength"     | Image length.          |
| IMAGE_WIDTH       | "ImageWidth"      | Image width.          |
| GPS_LATITUDE      | "GPSLatitude"     | Image latitude.          |
| GPS_LONGITUDE     | "GPSLongitude"    | Image longitude.          |
| GPS_LATITUDE_REF  | "GPSLatitudeRef"  | Latitude reference, for example, N or S.|
| GPS_LONGITUDE_REF | "GPSLongitudeRef" | Longitude reference, for example, W or E.|
W
wusongqing 已提交
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627

## ImageFormat<sup>9+</sup>

Enumerates the image formats.

**System capability**: SystemCapability.Multimedia.Image.Core

| Name        | Default Value| Description                |
| ------------ | ------ | -------------------- |
| YCBCR_422_SP | 1000   | YCBCR422 semi-planar format.|
| JPEG         | 2000   | JPEG encoding format.      |

## ComponentType<sup>9+</sup>

Enumerates the color component types of images.

**System capability**: SystemCapability.Multimedia.Image.ImageReceiver

| Name | Default Value| Description       |
| ----- | ------ | ----------- |
| YUV_Y | 1      | Luminance component. |
| YUV_U | 2      | Chrominance component. |
| YUV_V | 3      | Chrominance component. |
| JPEG  | 4      | JPEG type.|

## Component<sup>9+</sup>

Describes the color components of an image.

**System capability**: SystemCapability.Multimedia.Image.Core

| Name         | Type                            | Readable| Writable| Description        |
| ------------- | -------------------------------- | ---- | ---- | ------------ |
| componentType | [ComponentType](#componenttype9) | Yes  | No  | Color component type.  |
| rowStride     | number                           | Yes  | No  | Row stride.      |
| pixelStride   | number                           | Yes  | No  | Pixel stride.  |
| byteBuffer    | ArrayBuffer                      | Yes  | No  | Component buffer.|