js-apis-image.md 54.2 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 37 38 39 40
const color = new ArrayBuffer(96);
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts)
    .then((pixelmap) => {
        })
W
wusongqing 已提交
41 42 43 44
```

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

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

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

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

**Parameters**

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

**Example**

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

## 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 已提交
74 75 76 77 78
**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 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

### 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 已提交
103 104 105 106 107
pixelmap.readPixelsToBuffer(ReadBuffer).then(() => {
    console.log('readPixelsToBuffer succeeded.'); // Called if the condition is met.
}).catch(error => {
    console.log('readPixelsToBuffer failed.'); // Called if no condition is met.
})
W
wusongqing 已提交
108 109 110 111 112 113
```

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

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

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

**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 已提交
128 129 130 131 132 133 134
pixelmap.readPixelsToBuffer(ReadBuffer, (err, res) => {
    if(err) {
        console.log('readPixelsToBuffer failed.'); // Called if the condition is met.
    } else {
        console.log('readPixelsToBuffer succeeded.'); // Called if the condition is met.
    }
})
W
wusongqing 已提交
135 136 137 138 139 140
```

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

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

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

**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 已提交
160 161 162 163 164
pixelmap.readPixels(Area).then((data) => {
    console.log('readPixels succeeded.'); // Called if the condition is met.
}).catch(error => {
    console.log('readPixels failed.'); // Called if no condition is met.
})
W
wusongqing 已提交
165 166 167 168 169 170
```

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

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

171
Reads image pixel map data in an area. This API uses an asynchronous callback to return the data read.
W
wusongqing 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184

**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 已提交
185 186
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts, (err, pixelmap) => {
W
wusongqing 已提交
187 188 189 190 191 192 193 194 195 196 197
    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 已提交
198
})
W
wusongqing 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212
```

### 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                |
| ------ | ------------------------------ | ---- | -------------------- |
213
| area   | [PositionArea](#positionarea7) | Yes  | Area to which the image pixel map data will be written.|
W
wusongqing 已提交
214 215 216 217 218 219 220 221 222 223

**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 已提交
224 225 226 227 228
const color = new ArrayBuffer(96);
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts)
    .then( pixelmap => {
        if (pixelmap == undefined) {
W
wusongqing 已提交
229
            console.info('createPixelMap failed.');
W
wusongqing 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
        }
        const area = { pixels: new ArrayBuffer(8),
            offset: 0,
            stride: 8,
            region: { size: { height: 1, width: 2 }, x: 0, y: 0 }
        }
        var bufferArr = new Uint8Array(area.pixels);
        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 已提交
249
    }).catch(error => {
W
wusongqing 已提交
250 251
        console.log('error: ' + error);
    })
W
wusongqing 已提交
252 253 254 255 256 257
```

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

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

258
Writes image pixel map data to an area. This API uses an asynchronous callback to return the operation result.
W
wusongqing 已提交
259 260 261 262 263 264 265

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

**Parameters**

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

**Example**

```js
W
wusongqing 已提交
272 273 274 275 276 277 278 279
pixelmap.writePixels(Area, () => {
    const readArea = {
        pixels: new ArrayBuffer(20),
        offset: 0,
        stride: 8,
        region: { size: { height: 1, width: 2 }, x: 0, y: 0 },
    }
})
W
wusongqing 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
```

### 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 已提交
305
PixelMap.writeBufferToPixels(color).then(() => {
W
wusongqing 已提交
306 307 308
    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 已提交
309
})
W
wusongqing 已提交
310 311 312 313 314 315
```

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

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

316
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 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329

**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 已提交
330
PixelMap.writeBufferToPixels(color, function(err) {
W
wusongqing 已提交
331 332 333
    if (err) {
        console.error("Failed to write data from a buffer to a PixelMap.");
        return;
W
wusongqing 已提交
334 335 336
    } else {
		console.log("Succeeded in writing data from a buffer to a PixelMap.");
	}
W
wusongqing 已提交
337 338 339 340 341 342 343
});
```

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

getImageInfo(): Promise\<ImageInfo>

344
Obtains pixel map information of this image. This API uses a promise to return the information.
W
wusongqing 已提交
345 346 347 348 349 350 351 352 353 354 355 356

**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 已提交
357
PixelMap.getImageInfo().then(function(info) {
W
wusongqing 已提交
358 359 360 361 362 363 364 365 366 367
    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

368
Obtains pixel map information of this image. This API uses an asynchronous callback to return the information.
W
wusongqing 已提交
369 370 371 372 373 374 375 376 377 378 379 380

**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 已提交
381 382 383 384 385
pixelmap.getImageInfo((imageInfo) => { 
    console.log("getImageInfo succeeded.");
}).catch((err) => {
    console.error("getImageInfo failed.");
})
W
wusongqing 已提交
386 387 388 389 390 391
```

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

getBytesNumberPerRow(): number

392
Obtains the number of bytes per line of the image pixel map.
W
wusongqing 已提交
393 394 395 396 397 398 399

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

**Return value**

| Type  | Description                |
| ------ | -------------------- |
400
| number | Number of bytes per line.|
W
wusongqing 已提交
401 402 403 404

**Example**

```js
W
wusongqing 已提交
405 406 407
image.createPixelMap(clolr, opts, (err,pixelmap) => {
    let rowCount = pixelmap.getBytesNumberPerRow();
})
W
wusongqing 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
```

### 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 已提交
427
let pixelBytesNumber = pixelmap.getPixelBytesNumber();
W
wusongqing 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
```

### 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 已提交
447 448 449 450 451 452 453
image.createPixelMap(color, opts, (pixelmap) => {
    pixelmap.release().then(() => {
	    console.log('release succeeded.');
    }).catch(error => {
	    console.log('release failed.');
    })
})
W
wusongqing 已提交
454 455 456 457 458 459
```

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

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

460
Releases this **PixelMap** object. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
461 462 463 464 465 466 467 468 469 470 471 472

**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 已提交
473 474 475 476 477 478 479
image.createPixelMap(color, opts, (pixelmap) => {
    pixelmap.release().then(() => {
	    console.log('release succeeded.');
    }).catch(error => {
	    console.log('release failed.');
    })
})
W
wusongqing 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492 493
```

## 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 已提交
494
| uri    | string | Yes  | Image path. Currently, only the application sandbox path is supported.|
W
wusongqing 已提交
495 496 497

**Return value**

W
wusongqing 已提交
498 499 500
| Type                       | Description                                        |
| --------------------------- | -------------------------------------------- |
| [ImageSource](#imagesource) | Returns the **ImageSource** instance if the operation is successful; returns **undefined** otherwise.|
W
wusongqing 已提交
501 502 503 504

**Example**

```js
W
wusongqing 已提交
505 506
let path = this.context.getApplicationContext().fileDirs + "test.jpg";
const imageSourceApi = image.createImageSource(path);
W
wusongqing 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
```

## 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 已提交
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
const imageSourceApi = image.createImageSource(0);
W
wusongqing 已提交
533 534 535 536 537 538 539 540
```

## 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 已提交
541 542
**System capability**: SystemCapability.Multimedia.Image

W
wusongqing 已提交
543 544
| Name            | Type          | Readable| Writable| Description                                                        |
| ---------------- | -------------- | ---- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
545
| supportedFormats | Array\<string> | Yes  | No  | Supported image formats, including png, jpeg, wbmp, bmp, gif, webp, and heif.|
W
wusongqing 已提交
546 547 548 549 550

### getImageInfo

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

551
Obtains information about an image with the specified index. This API uses an asynchronous callback to return the information.
W
wusongqing 已提交
552 553 554 555 556 557 558 559 560 561 562 563 564

**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 已提交
565 566 567 568 569 570 571
imageSourceApi.getImageInfo(0,(error, imageInfo) => { 
    if(error) {
        console.log('getImageInfo failed.');
    } else {
        console.log('getImageInfo succeeded.');
    }
})
W
wusongqing 已提交
572 573 574 575 576 577
```

### getImageInfo

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

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

**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 已提交
591 592 593 594 595
imageSourceApi.getImageInfo(imageInfo => { 
    console.log('getImageInfo succeeded.');
}).catch(error => {
	console.log('getImageInfo failed.');
})
W
wusongqing 已提交
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
```

### 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 已提交
622 623 624 625 626
    .then(imageInfo => {
		console.log('getImageInfo succeeded.');
	}).catch(error => {
		console.log('getImageInfo failed.');
	})
W
wusongqing 已提交
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
```

### 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 已提交
653
imageSourceApi.getImageProperty("BitsPerSample")
W
wusongqing 已提交
654 655 656 657 658
    .then(data => {
		console.log('getImageProperty succeeded.');
	}).catch(error => {
		console.log('getImageProperty failed.');
	})
W
wusongqing 已提交
659 660 661 662 663 664
```

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

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

665
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 已提交
666 667 668 669 670 671 672 673 674 675 676 677 678

**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 已提交
679 680 681 682 683 684 685
imageSourceApi.getImageProperty("BitsPerSample",(error,data) => { 
    if(error) {
        console.log('getImageProperty failed.');
    } else {
        console.log('getImageProperty succeeded.');
    }
})
W
wusongqing 已提交
686 687 688 689 690 691
```

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

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

692
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 已提交
693 694 695 696 697 698 699 700 701 702 703 704 705 706

**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 已提交
707 708 709 710 711 712 713
imageSourceApi.getImageProperty("BitsPerSample",Property,(error,data) => { 
    if(error) {
        console.log('getImageProperty failed.');
    } else {
        console.log('getImageProperty succeeded.');
    }
})
W
wusongqing 已提交
714 715 716 717
```

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

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

720
Creates a **PixelMap** object based on image decoding parameters. This API uses a promise to return the result.
W
wusongqing 已提交
721 722 723 724 725

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

**Parameters**

W
wusongqing 已提交
726 727 728 729 730 731 732 733 734
| 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 已提交
735 736 737 738

**Example**

```js
W
wusongqing 已提交
739 740 741 742 743
imageSourceApi.createPixelMap().then(pixelmap => {
    console.log('createPixelMap succeeded.');
}).catch(error => {
    console.log('createPixelMap failed.');
})
W
wusongqing 已提交
744 745 746 747
```

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

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

750
Creates a **PixelMap** object based on the default parameters. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
751 752 753 754 755

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

**Parameters**

W
wusongqing 已提交
756 757 758
| Name    | Type                                 | Mandatory| Description                      |
| -------- | ------------------------------------- | ---- | -------------------------- |
| callback | AsyncCallback<[PixelMap](#pixelmap7)> | Yes  | Callback used to return the **PixelMap** object.|
W
wusongqing 已提交
759 760 761 762

**Example**

```js
W
wusongqing 已提交
763 764 765 766 767
imageSourceApi.createPixelMap(pixelmap => { 
    console.log('createPixelMap succeeded.');
}).catch(error => {
    console.log('createPixelMap failed.');
})
W
wusongqing 已提交
768 769 770 771
```

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

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

774
Creates a **PixelMap** object based on image decoding 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 783
| 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 已提交
784 785 786 787

**Example**

```js
W
wusongqing 已提交
788 789 790 791 792
imageSourceApi.createPixelMap(decodingOptions, pixelmap => { 
    console.log('createPixelMap succeeded.');
}).catch(error => {
    console.log('createPixelMap failed.');
}) 
W
wusongqing 已提交
793 794 795 796 797 798
```

### release

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

799
Releases this **ImageSource** instance. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
800 801 802 803 804 805 806 807 808 809 810 811

**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 已提交
812 813 814 815 816
imageSourceApi.release(() => { 
    console.log('release succeeded.');
}).catch(error => {
    console.log('release failed.');
})
W
wusongqing 已提交
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
```

### 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 已提交
836 837 838 839 840
imageSourceApi.release().then(()=>{
    console.log('release succeeded.');
}).catch(error => {
    console.log('release failed.');
})
W
wusongqing 已提交
841 842 843 844 845 846 847 848 849 850 851 852
```

## image.createImagePacker

createImagePacker(): ImagePacker

Creates an **ImagePacker** instance.

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

**Return value**

W
wusongqing 已提交
853 854 855
| Type                       | Description                 |
| --------------------------- | --------------------- |
| [ImagePacker](#imagepacker) | **ImagePacker** instance created.|
W
wusongqing 已提交
856 857 858 859 860 861 862 863 864 865 866 867 868

**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 已提交
869 870 871 872 873
**System capability**: SystemCapability.Multimedia.Image

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

### packing

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

879
Packs an image. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
880 881 882 883 884 885 886 887 888

**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 已提交
889
| callback | AsyncCallback\<ArrayBuffer>        | Yes  | Callback used to return the packed data.|
W
wusongqing 已提交
890 891 892 893

**Example**

```js
W
wusongqing 已提交
894 895
let packOpts = { format:["image/jpeg"], quality:98 };
imagePackerApi.packing(ImageSourceApi, packOpts, data => {})
W
wusongqing 已提交
896 897 898 899
```

### packing

W
wusongqing 已提交
900
packing(source: ImageSource, option: PackingOption): Promise\<ArrayBuffer>
W
wusongqing 已提交
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916

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 已提交
917
| Promise\<ArrayBuffer> | Promise used to return the packed data.|
W
wusongqing 已提交
918 919 920 921 922

**Example**

```js
let packOpts = { format:["image/jpeg"], quality:98 }
W
wusongqing 已提交
923 924 925 926 927 928
imagePackerApi.packing(ImageSourceApi, packOpts)
    .then( data => {
        console.log('packing succeeded.');
	}).catch(error => {
	    console.log('packing failed.');
	})
W
wusongqing 已提交
929 930
```

W
wusongqing 已提交
931
### packing<sup>8+</sup>
W
wusongqing 已提交
932 933

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

935
Packs an image. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
936 937 938 939 940 941 942

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

**Parameters**

| Name  | Type                           | Mandatory| Description                              |
| -------- | ------------------------------- | ---- | ---------------------------------- |
W
wusongqing 已提交
943
| source   | [PixelMap](#pixelmap)           | Yes  | **PixelMap** object to pack.              |
W
wusongqing 已提交
944
| option   | [PackingOption](#packingoption) | Yes  | Option for image packing.                    |
W
wusongqing 已提交
945
| callback | AsyncCallback\<ArrayBuffer>     | Yes  | Callback used to return the packed data.|
W
wusongqing 已提交
946 947 948 949 950

**Example**

```js
let packOpts = { format:["image/jpeg"], quality:98 }
W
wusongqing 已提交
951 952 953 954 955
imagePackerApi.packing(PixelMapApi, packOpts, data => { 
    console.log('packing succeeded.');
}).catch(error => {
	console.log('packing failed.');
})
W
wusongqing 已提交
956 957
```

W
wusongqing 已提交
958
### packing<sup>8+</sup>
W
wusongqing 已提交
959

W
wusongqing 已提交
960
packing(source: PixelMap, option: PackingOption): Promise\<ArrayBuffer>
W
wusongqing 已提交
961 962 963 964 965 966 967

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

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

**Parameters**

W
wusongqing 已提交
968 969
| Name| Type                           | Mandatory| Description              |
| ------ | ------------------------------- | ---- | ------------------ |
W
wusongqing 已提交
970
| source | [PixelMap](#pixelmap)           | Yes  | **PixelMap** object to pack.|
W
wusongqing 已提交
971
| option | [PackingOption](#packingoption) | Yes  | Option for image packing.    |
W
wusongqing 已提交
972 973 974

**Return value**

W
wusongqing 已提交
975 976
| Type                        | Description                                         |
| :--------------------------- | :-------------------------------------------- |
W
wusongqing 已提交
977
| Promise\<ArrayBuffer> | Promise used to return the packed data.|
W
wusongqing 已提交
978 979 980 981 982

**Example**

```js
let packOpts = { format:["image/jpeg"], quality:98 }
W
wusongqing 已提交
983 984 985 986 987 988
imagePackerApi.packing(PixelMapApi, packOpts)
    .then( data => {
	    console.log('packing succeeded.');
	}).catch(error => {
	    console.log('packing failed.');
	})
W
wusongqing 已提交
989 990 991 992 993 994
```

### release

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

995
Releases this **ImagePacker** instance. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007

**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 已提交
1008 1009 1010 1011 1012
imagePackerApi.release(()=>{ 
    console.log('release succeeded.');
}).catch(error => {
	console.log('release failed.');
})
W
wusongqing 已提交
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
```

### 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 已提交
1032 1033 1034 1035 1036
imagePackerApi.release().then(()=>{
    console.log('release succeeded.');
}).catch((error)=>{ 
    console.log('release failed.'); 
}) 
W
wusongqing 已提交
1037 1038
```

W
wusongqing 已提交
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
## 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 已提交
1065
var receiver = image.createImageReceiver(8192, 8, 4, 8);
W
wusongqing 已提交
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
```

## 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 已提交
1101 1102 1103 1104 1105 1106 1107
 receiver.getReceivingSurfaceId((err, id) => { 
    if(err) {
        console.log('getReceivingSurfaceId failed.');
    } else {
        console.log('getReceivingSurfaceId succeeded.');
    }
});
W
wusongqing 已提交
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
```

### 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 已提交
1128 1129 1130 1131
    console.log('getReceivingSurfaceId succeeded.');
}).catch(error => {
    console.log('getReceivingSurfaceId failed.');
})
W
wusongqing 已提交
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
```

### 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 已提交
1151 1152 1153 1154 1155 1156 1157
receiver.readLatestImage((err, img) => { 
    if(err) {
        console.log('readLatestImage failed.');
    } else {
        console.log('readLatestImage succeeded.');
    }
});
W
wusongqing 已提交
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
```

### 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 已提交
1177 1178 1179 1180 1181
receiver.readLatestImage().then(img => {
    console.log('readLatestImage succeeded.');
}).catch(error => {
    console.log('readLatestImage failed.');
})
W
wusongqing 已提交
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
```

### 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 已提交
1201 1202 1203 1204 1205 1206 1207
receiver.readNextImage((err, img) => { 
    if(err) {
        console.log('readNextImage failed.');
    } else {
        console.log('readNextImage succeeded.');
    }
});
W
wusongqing 已提交
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
```

### 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 已提交
1227 1228 1229 1230 1231
receiver.readNextImage().then(img => {
    console.log('readNextImage succeeded.');
}).catch(error => {
    console.log('readNextImage failed.');
})
W
wusongqing 已提交
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
```

### 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 已提交
1252
receiver.on('imageArrival', () => {})
W
wusongqing 已提交
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
```

### 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 已提交
1272
receiver.release(() => {})
W
wusongqing 已提交
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
```

### 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 已提交
1292 1293 1294 1295 1296
receiver.release().then(() => {
    console.log('release succeeded.');
}).catch(error => {
    console.log('release failed.');
})
W
wusongqing 已提交
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
```

## 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 已提交
1331 1332 1333 1334 1335 1336 1337
img.getComponent(4, (err, component) => {
    if(err) {
        console.log('getComponent failed.');
    } else {
        console.log('getComponent succeeded.');
    }
})
W
wusongqing 已提交
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
```

### 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 已提交
1385 1386 1387 1388 1389
img.release(() =>{ 
    console.log('release succeeded.');
}).catch(error => {
    console.log('release failed.');
}) 
W
wusongqing 已提交
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
```

### 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 已提交
1412 1413 1414 1415
    console.log('release succeeded.');
}).catch(error => {
    console.log('release failed.');
})
W
wusongqing 已提交
1416 1417
```

W
wusongqing 已提交
1418 1419 1420 1421 1422 1423
## PositionArea<sup>7+</sup>

Describes area information in an image.

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

W
wusongqing 已提交
1424 1425 1426 1427
| Name  | Type              | Readable| Writable| Description                                                        |
| ------ | ------------------ | ---- | ---- | ------------------------------------------------------------ |
| pixels | ArrayBuffer        | Yes  | No  | Pixels of the image.                                                      |
| offset | number             | Yes  | No  | Offset for data reading.                                                    |
W
wusongqing 已提交
1428 1429
| 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 已提交
1430

W
wusongqing 已提交
1431
## ImageInfo
W
wusongqing 已提交
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453

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 已提交
1454
Enumerates the pixel formats of images.
W
wusongqing 已提交
1455 1456 1457 1458 1459 1460 1461 1462 1463

**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 已提交
1464
## AlphaType<sup>9+</sup>
W
wusongqing 已提交
1465

W
wusongqing 已提交
1466
Enumerates the alpha types of images.
W
wusongqing 已提交
1467 1468 1469 1470 1471 1472

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

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

W
wusongqing 已提交
1477
## ScaleMode<sup>9+</sup>
W
wusongqing 已提交
1478

W
wusongqing 已提交
1479
Enumerates the scale modes of images.
W
wusongqing 已提交
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489

**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 已提交
1490 1491
Defines pixel map initialization options.

W
wusongqing 已提交
1492 1493
**System capability**: SystemCapability.Multimedia.Image

W
wusongqing 已提交
1494 1495 1496 1497 1498 1499 1500
| 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 已提交
1501 1502 1503

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

W
wusongqing 已提交
1504
Defines image decoding options.
W
wusongqing 已提交
1505 1506 1507 1508 1509 1510

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

| Name              | Type                              | Readable| Writable| Description            |
| ------------------ | ---------------------------------- | ---- | ---- | ---------------- |
| sampleSize         | number                             | Yes  | Yes  | Thumbnail sampling size.|
W
wusongqing 已提交
1511
| rotate             | number                             | Yes  | Yes  | Rotation angle.      |
W
wusongqing 已提交
1512 1513
| editable           | boolean                            | Yes  | Yes  | Whether the image is editable.    |
| desiredSize        | [Size](#size)                      | Yes  | Yes  | Expected output size.  |
W
wusongqing 已提交
1514
| desiredRegion      | [Region](#region7)                 | Yes  | Yes  | Region to decode.      |
W
wusongqing 已提交
1515
| desiredPixelFormat | [PixelMapFormat](#pixelmapformat7) | Yes  | Yes  | Pixel map format for decoding.|
N
Neil Chen 已提交
1516
| index              | number                              | Yes  | Yes  | Index of the image to decode.    |
W
wusongqing 已提交
1517

W
wusongqing 已提交
1518
## Region<sup>7+</sup>
W
wusongqing 已提交
1519 1520 1521 1522 1523

Describes region information.

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

W
wusongqing 已提交
1524 1525 1526
| Name| Type         | Readable| Writable| Description        |
| ---- | ------------- | ---- | ---- | ------------ |
| size | [Size](#size) | Yes  | Yes  | Region size.  |
W
wusongqing 已提交
1527 1528 1529 1530 1531
| x    | number        | Yes  | Yes  | X coordinate of the region.|
| y    | number        | Yes  | Yes  | Y coordinate of the region.|

## PackingOption

W
wusongqing 已提交
1532
Defines the option for image packing.
W
wusongqing 已提交
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555

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

| Name   | Type  | Readable| Writable| Description          |
| ------- | ------ | ---- | ---- | -------------- |
| format  | string | Yes  | Yes  | Format of the packed image.    |
| quality | number | Yes  | Yes  | Quality of the packed image.|

## 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 已提交
1556 1557
**System capability**: SystemCapability.Multimedia.Image

W
wusongqing 已提交
1558 1559
| Name             | Default Value           | Description                |
| ----------------- | ----------------- | -------------------- |
1560
| BITS_PER_SAMPLE   | "BitsPerSample"   | Number of bits per pixel.    |
W
wusongqing 已提交
1561 1562 1563 1564 1565 1566 1567
| 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 已提交
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604

## 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.|