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

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

W
wusongqing 已提交
6
## Modules to Import
W
wusongqing 已提交
7 8 9 10 11 12

```
import image from '@ohos.multimedia.image';
```

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

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

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

**Parameters**

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

**Return value**

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

**Example**

```js
image.createPixelMap(Color, opts)
            .then((pixelmap) => {
            })
```

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

W
wusongqing 已提交
42
createPixelMap(colors: ArrayBuffer, options: InitializetionOptions, callback: AsyncCallback\<PixelMap>): void
W
wusongqing 已提交
43 44 45 46 47 48 49

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

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

**Parameters**

W
wusongqing 已提交
50 51 52 53 54
| Name    | Type                                            | Mandatory| Description                      |
| -------- | ------------------------------------------------ | ---- | -------------------------- |
| colors   | ArrayBuffer                                      | Yes  | Color array.                |
| options  | [InitializetionOptions](#initializationoptions8) | Yes  | Pixel properties.                    |
| callback | AsyncCallback\<[PixelMap](#pixelmap7)>           | Yes  | Callback used to return the **PixelMap** object.|
W
wusongqing 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

**Example**

```js
image.createPixelMap(Color, opts, (pixelmap) => {
            })
```

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

| Name                   | Type   | Readable| Writable| Description                                                        |
| ----------------------- | ------- | ---- | ---- | ------------------------------------------------------------ |
| isEditable<sup>7+</sup> | boolean | Yes  | No  | Whether the image pixel map is editable.<br>**System capability**: SystemCapability.Multimedia.Image|

### 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
pixelmap.readPixelsToBuffer(readBuffer).then(() => {
W
wusongqing 已提交
97 98 99 100
                        // Called if the condition is met.
                }).catch(error => {
                // Called if no condition is met.
            })
W
wusongqing 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
```

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

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

Reads image pixel map data and writes the data to an **ArrayBuffer**. This API uses a callback 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.|
| callback | AsyncCallback\<void> | Yes  | Callback used to return the operation result. If the operation fails, an error message is returned.                              |

**Example**

```js
pixelmap.readPixelsToBuffer(readBuffer, () => {
            })
```

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

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

Reads pixel map data in an area. This API uses a promise to return the data read.

**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
pixelmap.readPixels(area).then((data) => {
W
wusongqing 已提交
149 150 151 152
                  // Called if the condition is met.     
                }).catch(error => {
                // Called if no condition is met.
            })
W
wusongqing 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
```

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

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

Reads image pixel map data in an area. This API uses a callback to return the data read.

**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 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts, (err, pixelmap) => {
     if(pixelmap == undefined){
          console.info('createPixelMap failed');
          expect(false).assertTrue();
          done();
      }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 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202
```

### 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                |
| ------ | ------------------------------ | ---- | -------------------- |
W
wusongqing 已提交
203
| area   | [PositionArea](#positionarea7) | Yes  | Area to which the pixel map data will be written.|
W
wusongqing 已提交
204 205 206 207 208 209 210 211 212 213

**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 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
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) {
            console.info('createPixelMap failed');
            expect(false).assertTrue()
            done();
        }
        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 }
            }        
        })
    })
    .catch(error => {
        console.log('error: ' + error);
        expect().assertFail();
        done();
    })
W
wusongqing 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260
```

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

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

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

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

**Parameters**

| Name   | Type                          | Mandatory| Description                          |
| --------- | ------------------------------ | ---- | ------------------------------ |
W
wusongqing 已提交
261
| area      | [PositionArea](#positionarea7) | Yes  | Area to which the pixel map data will be written.          |
W
wusongqing 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
| callback: | AsyncCallback\<void>           | Yes  | Callback used to return the operation result. If the operation fails, an error message is returned.|

**Example**

```js
pixelmap.writePixels(area, () => {
                const readArea = {
                    pixels: new ArrayBuffer(20),
                    offset: 0,
                    stride: 8,
                    region: { size: { height: 1, width: 2 }, x: 0, y: 0 },
                }
            })
```

### 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
pixelMap.writeBufferToPixels(colorBuffer).then(() => {
    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.");
});
```

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

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

Reads image data in an **ArrayBuffer** and writes the data to a **PixelMap** object. This API uses a callback 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.                |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the operation result. If the operation fails, an error message is returned.|

**Example**

```js
pixelMap.writeBufferToPixels(colorBuffer, function(err) {
    if (err) {
        console.error("Failed to write data from a buffer to a PixelMap.");
        return;
    }
    console.log("Succeeded in writing data from a buffer to a PixelMap.");
});
```

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

getImageInfo(): Promise\<ImageInfo>

Obtains pixel map information about this image. This API uses a promise to return the information.

**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
pixelMap.getImageInfo().then(function(info) {
    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

Obtains pixel map information about this image. This API uses a callback to return the information.

**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 已提交
375
pixelmap.getImageInfo((imageInfo) => {})
W
wusongqing 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
```

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

getBytesNumberPerRow(): number

Obtains the number of bytes in each line of the image pixel map.

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

**Return value**

| Type  | Description                |
| ------ | -------------------- |
| number | Number of bytes in each line.|

**Example**

```js
W
wusongqing 已提交
395
rowCount = pixelmap.getBytesNumberPerRow()
W
wusongqing 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
```

### 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 已提交
415
pixelBytesNumber = pixelmap.getPixelBytesNumber()
W
wusongqing 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
```

### 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 已提交
435 436
 pixelmap.release().then(() => { })
            .catch(error => {})
W
wusongqing 已提交
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
```

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

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

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

**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 已提交
456
pixelmap.release(()=>{ })   
W
wusongqing 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
```

## image.createImageSource

createImageSource(uri: string): ImageSource

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

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

**Parameters**

| Name| Type  | Mandatory| Description                              |
| ------ | ------ | ---- | ---------------------------------- |
| uri    | string | Yes  | Image source URI. Currently, only the local absolute path is supported.|

**Return value**

W
wusongqing 已提交
475 476 477
| Type                       | Description                                        |
| --------------------------- | -------------------------------------------- |
| [ImageSource](#imagesource) | Returns the **ImageSource** instance if the operation is successful; returns **undefined** otherwise.|
W
wusongqing 已提交
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500

**Example**

```js
const imageSourceApi = image.createImageSource('/data/local/tmp/test.jpg')
```

## 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 已提交
501 502 503
| Type                       | Description                                        |
| --------------------------- | -------------------------------------------- |
| [ImageSource](#imagesource) | Returns the **ImageSource** instance if the operation is successful; returns **undefined** otherwise.|
W
wusongqing 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538

**Example**

```js
const imageSourceApi = image.createImageSource(0)
```

## ImageSource

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

### Attributes

| Name            | Type          | Readable| Writable| Description                                                        |
| ---------------- | -------------- | ---- | ---- | ------------------------------------------------------------ |
| supportedFormats | Array\<string> | Yes  | No  | Supported image formats, including png, jpeg, wbmp, bmp, gif, webp, and heif.<br>**System capability**: SystemCapability.Multimedia.Image|

### getImageInfo

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

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

**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 已提交
539
imageSourceApi.getImageInfo(0,(error, imageInfo) => {})
W
wusongqing 已提交
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
```

### getImageInfo

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

Obtains information about this image. This API uses a callback to return the information.

**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 已提交
559
imageSourceApi.getImageInfo(imageInfo => {})
W
wusongqing 已提交
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
```

### 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 已提交
586 587
            .then(imageInfo => {})
			.catch(error => {})
W
wusongqing 已提交
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
```

### 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 已提交
614 615 616
imageSourceApi.getImageProperty("BitsPerSample")
            .then(data => {})
            .catch(error => {})
W
wusongqing 已提交
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
```

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

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

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

**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 已提交
637
imageSourceApi.getImageProperty("BitsPerSample",(error,data) => {})
W
wusongqing 已提交
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
```

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

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

Obtains the value of a property in this image. This API uses a callback to return the property value in a string.

**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 已提交
659
imageSourceApi.getImageProperty("BitsPerSample",property,(error,data) => {})
W
wusongqing 已提交
660 661 662 663
```

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

W
wusongqing 已提交
664
createPixelMap(options?: DecodingOptions): Promise\<PixelMap>
W
wusongqing 已提交
665 666 667 668 669 670 671

Creates a **PixelMap** object based on image decoding parameters. This API uses a callback to return the result.

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

**Parameters**

W
wusongqing 已提交
672 673 674 675 676 677 678 679 680
| 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 已提交
681 682 683 684

**Example**

```js
W
wusongqing 已提交
685 686
imageSourceApi.createPixelMap().then(pixelmap => {})
    						.catch(error => {})
W
wusongqing 已提交
687 688 689 690
```

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

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

W
wusongqing 已提交
693
Creates a **PixelMap** object based on the default parameters. This API uses a callback to return the result.
W
wusongqing 已提交
694 695 696 697 698

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

**Parameters**

W
wusongqing 已提交
699 700 701
| Name    | Type                                 | Mandatory| Description                      |
| -------- | ------------------------------------- | ---- | -------------------------- |
| callback | AsyncCallback<[PixelMap](#pixelmap7)> | Yes  | Callback used to return the **PixelMap** object.|
W
wusongqing 已提交
702 703 704 705

**Example**

```js
W
wusongqing 已提交
706
imageSourceApi.createPixelMap(pixelmap => {})
W
wusongqing 已提交
707 708 709 710
```

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

W
wusongqing 已提交
711
createPixelMap(options: DecodingOptions, callback: AsyncCallback\<PixelMap>): void
W
wusongqing 已提交
712 713 714 715 716 717 718

Creates a **PixelMap** object based on image decoding parameters. This API uses a callback to return the result.

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

**Parameters**

W
wusongqing 已提交
719 720 721 722
| 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 已提交
723 724 725 726

**Example**

```js
W
wusongqing 已提交
727
imageSourceApi.createPixelMap(decodingOptions, pixelmap => {})    
W
wusongqing 已提交
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
```

### release

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

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

**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 已提交
747
imageSourceApi.release(() => {})
W
wusongqing 已提交
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
```

### 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 已提交
767
imageSourceApi.release().then(()=>{ }).catch(error => {})
W
wusongqing 已提交
768 769 770 771 772 773 774 775 776 777 778 779
```

## image.createImagePacker

createImagePacker(): ImagePacker

Creates an **ImagePacker** instance.

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

**Return value**

W
wusongqing 已提交
780 781 782
| Type                       | Description                 |
| --------------------------- | --------------------- |
| [ImagePacker](#imagepacker) | **ImagePacker** instance created.|
W
wusongqing 已提交
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801

**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

| Name            | Type          | Readable| Writable| Description                                                        |
| ---------------- | -------------- | ---- | ---- | ------------------------------------------------------------ |
| supportedFormats | Array\<string> | Yes  | No  | Supported image format, which can be jpeg.<br>**System capability**: SystemCapability.Multimedia.Image|

### packing

W
wusongqing 已提交
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
packing(source: ImageSource, option: PackingOption, callback: AsyncCallback<Array\<ArrayBuffer>>): void

Packs an image. This API uses a callback 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.                    |
| callback | AsyncCallback<Array\<ArrayBuffer>> | Yes  | Callback used to return the packed data.|

**Example**

```js
let packOpts = { format:["image/jpeg"], quality:98 }
imagePackerApi.packing(imageSourceApi, packOpts, data => {})
```

### packing

packing(source: ImageSource, option: PackingOption): Promise<Array\<ArrayBuffer>>

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                                         |
| :--------------------------- | :-------------------------------------------- |
| Promise<Array\<ArrayBuffer>> | Promise used to return the packed data.|

**Example**

```js
let packOpts = { format:["image/jpeg"], quality:98 }
imagePackerApi.packing(imageSourceApi, packOpts)
    .then( data => { })
	.catch(error => {})
```

### packing

packing(source: PixelMap, option: PackingOption, callback: AsyncCallback\<ArrayBuffer>): void
W
wusongqing 已提交
856 857 858 859 860 861 862 863 864

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

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

**Parameters**

| Name  | Type                           | Mandatory| Description                              |
| -------- | ------------------------------- | ---- | ---------------------------------- |
W
wusongqing 已提交
865
| source   | [PixelMap](#pixelmap)           | Yes  | **PixelMap** object to pack.              |
W
wusongqing 已提交
866
| option   | [PackingOption](#packingoption) | Yes  | Option for image packing.                    |
W
wusongqing 已提交
867
| callback | AsyncCallback\<ArrayBuffer>     | Yes  | Callback used to return the packed data.|
W
wusongqing 已提交
868 869 870 871 872

**Example**

```js
let packOpts = { format:["image/jpeg"], quality:98 }
W
wusongqing 已提交
873
imagePackerApi.packing(pixelMapApi, packOpts, data => {})
W
wusongqing 已提交
874 875 876 877
```

### packing

W
wusongqing 已提交
878
packing(source: PixelMap, option: PackingOption): Promise<Array\<ArrayBuffer>>
W
wusongqing 已提交
879 880 881 882 883 884 885 886 887

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

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

**Parameters**

| Name| Type                           | Mandatory| Description          |
| ------ | ------------------------------- | ---- | -------------- |
W
wusongqing 已提交
888
| source | [PixelMap](#pixelmap)           | Yes  | **PixelMap** object to pack.|
W
wusongqing 已提交
889 890 891 892
| option | [PackingOption](#packingoption) | Yes  | Option for image packing.|

**Return value**

W
wusongqing 已提交
893 894 895
| Type                        | Description                                         |
| :--------------------------- | :-------------------------------------------- |
| Promise<Array\<ArrayBuffer>> | Promise used to return the packed data.|
W
wusongqing 已提交
896 897 898 899 900

**Example**

```js
let packOpts = { format:["image/jpeg"], quality:98 }
W
wusongqing 已提交
901 902 903
imagePackerApi.packing(pixelMapApi, packOpts)
    .then( data => { })
	.catch(error => {})
W
wusongqing 已提交
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
```

### release

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

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

**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 已提交
923
imagePackerApi.release(()=>{})
W
wusongqing 已提交
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
```

### 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 已提交
943 944
 imagePackerApi.release().then(()=>{
            }).catch((error)=>{}) 
W
wusongqing 已提交
945 946 947 948 949 950 951 952
```

## PositionArea<sup>7+</sup>

Describes area information in an image.

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

W
wusongqing 已提交
953 954 955 956 957 958
| Name  | Type              | Readable| Writable| Description                                                        |
| ------ | ------------------ | ---- | ---- | ------------------------------------------------------------ |
| pixels | ArrayBuffer        | Yes  | No  | Pixels of the image.                                                      |
| offset | number             | Yes  | No  | Offset for data reading.                                                    |
| 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](#region8) | 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 已提交
959

W
wusongqing 已提交
960
## ImageInfo
W
wusongqing 已提交
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992

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>

Enumerates pixel map formats.

**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 已提交
993
## AlphaType<sup>9+</sup>
W
wusongqing 已提交
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005

Enumerates alpha types.

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

| Name    | Default Value| Description                   |
| -------- | ------ | ----------------------- |
| UNKNOWN  | 0      | Unknown alpha type.           |
| OPAQUE   | 1      | There is no alpha or the image is fully transparent.|
| PREMUL   | 2      | Premultiplied alpha.         |
| UNPREMUL | 3      | Unpremultiplied alpha, that is, straight alpha.       |

W
wusongqing 已提交
1006
## ScaleMode<sup>9+</sup>
W
wusongqing 已提交
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022

Enumerates scale modes.

**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>

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

| Name       | Type                              | Readable| Writable| Description          |
| ----------- | ---------------------------------- | ---- | ---- | -------------- |
W
wusongqing 已提交
1023
| alphaType<sup>9+</sup>   | [AlphaType](#alphatype9)           | Yes  | Yes  | Alpha type.      |
W
wusongqing 已提交
1024 1025
| editable    | boolean                            | Yes  | Yes  | Whether the image is editable.  |
| pixelFormat | [PixelMapFormat](#pixelmapformat7) | Yes  | Yes  | Pixel map format.    |
W
wusongqing 已提交
1026
| scaleMode<sup>9+</sup>   | [ScaleMode](#scalemode9)           | Yes  | Yes  | Scale mode.      |
W
wusongqing 已提交
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
| size        | [Size](#size)                      | Yes  | Yes  | Image size.|

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

Describes the decoding options.

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

| Name              | Type                              | Readable| Writable| Description            |
| ------------------ | ---------------------------------- | ---- | ---- | ---------------- |
| sampleSize         | number                             | Yes  | Yes  | Thumbnail sampling size.|
W
wusongqing 已提交
1038
| rotate             | number                             | Yes  | Yes  | Rotation angle.      |
W
wusongqing 已提交
1039 1040
| editable           | boolean                            | Yes  | Yes  | Whether the image is editable.    |
| desiredSize        | [Size](#size)                      | Yes  | Yes  | Expected output size.  |
W
wusongqing 已提交
1041
| desiredRegion      | [Region](#region7)                 | Yes  | Yes  | Region to decode.      |
W
wusongqing 已提交
1042 1043 1044
| desiredPixelFormat | [PixelMapFormat](#pixelmapformat7) | Yes  | Yes  | Pixel map format for decoding.|
| index              | numer                              | Yes  | Yes  | Index of the image to decode.    |

W
wusongqing 已提交
1045
## Region<sup>7+</sup>
W
wusongqing 已提交
1046 1047 1048 1049 1050

Describes region information.

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

W
wusongqing 已提交
1051 1052 1053
| Name| Type         | Readable| Writable| Description        |
| ---- | ------------- | ---- | ---- | ------------ |
| size | [Size](#size) | Yes  | Yes  | Region size.  |
W
wusongqing 已提交
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
| x    | number        | Yes  | Yes  | X coordinate of the region.|
| y    | number        | Yes  | Yes  | Y coordinate of the region.|

## PackingOption

Describes the option for image packing.

**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 已提交
1083 1084
**System capability**: SystemCapability.Multimedia.Image

W
wusongqing 已提交
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
| Name             | Default Value           | Description                |
| ----------------- | ----------------- | -------------------- |
| BITS_PER_SAMPLE   | "BitsPerSample"   | Number of bytes in each pixel.    |
| 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.|