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

W
wusongqing 已提交
3
> **NOTE**<br/>
Z
zengyawen 已提交
4
> 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 已提交
5 6
>
> API version 9 is a canary release for trial use. The APIs of this version may be unstable.
W
wusongqing 已提交
7

Z
zengyawen 已提交
8
## Modules to Import
W
wusongqing 已提交
9 10 11 12 13 14

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

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

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

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

**Parameters**

Z
zengyawen 已提交
23 24 25 26
| 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 已提交
27 28 29 30 31

**Return value**

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

**Example**

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

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

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

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

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

**Parameters**

Z
zengyawen 已提交
52 53 54 55 56
| 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 已提交
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 97 98

**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(() => {
Z
zengyawen 已提交
99 100 101 102
                        // Called if the condition is met.
                }).catch(error => {
                // Called if no condition is met.
            })
W
wusongqing 已提交
103 104 105 106 107 108
```

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

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

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

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

**Parameters**

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

**Example**

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

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

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

W
wusongqing 已提交
131
Reads image pixel map data in an area. This API uses a promise to return the data read.
W
wusongqing 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

**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) => {
Z
zengyawen 已提交
151 152 153 154
                  // Called if the condition is met.     
                }).catch(error => {
                // Called if no condition is met.
            })
W
wusongqing 已提交
155 156 157 158 159 160
```

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

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

W
wusongqing 已提交
161
Reads image pixel map data in an area. This API uses an asynchronous callback to return the data read.
W
wusongqing 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174

**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
Z
zengyawen 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
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 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204
```

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

**Return value**

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

**Example**

```js
Z
zengyawen 已提交
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 247 248
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 已提交
249 250 251 252 253 254
```

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

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

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

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

**Parameters**

| Name   | Type                          | Mandatory| Description                          |
| --------- | ------------------------------ | ---- | ------------------------------ |
W
wusongqing 已提交
263
| area      | [PositionArea](#positionarea7) | Yes  | Area to which the image pixel map data will be written.          |
W
wusongqing 已提交
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
| 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

W
wusongqing 已提交
313
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 已提交
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

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

W
wusongqing 已提交
340
Obtains pixel map information of this image. This API uses a promise to return the information.
W
wusongqing 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

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

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

**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
Z
zengyawen 已提交
377
pixelmap.getImageInfo((imageInfo) => {})
W
wusongqing 已提交
378 379 380 381 382 383
```

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

getBytesNumberPerRow(): number

W
wusongqing 已提交
384
Obtains the number of bytes per line of the image pixel map.
W
wusongqing 已提交
385 386 387 388 389 390 391

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

**Return value**

| Type  | Description                |
| ------ | -------------------- |
W
wusongqing 已提交
392
| number | Number of bytes per line.|
W
wusongqing 已提交
393 394 395 396

**Example**

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

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

### 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
Z
zengyawen 已提交
437 438
 pixelmap.release().then(() => { })
            .catch(error => {})
W
wusongqing 已提交
439 440 441 442 443 444
```

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

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

W
wusongqing 已提交
445
Releases this **PixelMap** object. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
446 447 448 449 450 451 452 453 454 455 456 457

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

**Parameters**

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

**Example**

```js
Z
zengyawen 已提交
458
pixelmap.release(()=>{ })   
W
wusongqing 已提交
459 460 461 462 463 464 465 466 467 468 469 470 471 472
```

## 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 已提交
473
| uri    | string | Yes  | Image path. Currently, only the application sandbox path is supported.|
W
wusongqing 已提交
474 475 476

**Return value**

Z
zengyawen 已提交
477 478 479
| Type                       | Description                                        |
| --------------------------- | -------------------------------------------- |
| [ImageSource](#imagesource) | Returns the **ImageSource** instance if the operation is successful; returns **undefined** otherwise.|
W
wusongqing 已提交
480 481 482 483

**Example**

```js
W
wusongqing 已提交
484 485
let path = this.context.getApplicationContext().fileDirs + "test.jpg";
const imageSourceApi = image.createImageSource(path);
W
wusongqing 已提交
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
```

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

Z
zengyawen 已提交
504 505 506
| Type                       | Description                                        |
| --------------------------- | -------------------------------------------- |
| [ImageSource](#imagesource) | Returns the **ImageSource** instance if the operation is successful; returns **undefined** otherwise.|
W
wusongqing 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527

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

W
wusongqing 已提交
528
Obtains information about an image with the specified index. This API uses an asynchronous callback to return the information.
W
wusongqing 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541

**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
Z
zengyawen 已提交
542
imageSourceApi.getImageInfo(0,(error, imageInfo) => {})
W
wusongqing 已提交
543 544 545 546 547 548
```

### getImageInfo

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

W
wusongqing 已提交
549
Obtains information about this image. This API uses an asynchronous callback to return the information.
W
wusongqing 已提交
550 551 552 553 554 555 556 557 558 559 560 561

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

**Parameters**

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

**Example**

```js
Z
zengyawen 已提交
562
imageSourceApi.getImageInfo(imageInfo => {})
W
wusongqing 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
```

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

### 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
Z
zengyawen 已提交
617 618 619
imageSourceApi.getImageProperty("BitsPerSample")
            .then(data => {})
            .catch(error => {})
W
wusongqing 已提交
620 621 622 623 624 625
```

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

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

W
wusongqing 已提交
626
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 已提交
627 628 629 630 631 632 633 634 635 636 637 638 639

**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
Z
zengyawen 已提交
640
imageSourceApi.getImageProperty("BitsPerSample",(error,data) => {})
W
wusongqing 已提交
641 642 643 644 645 646
```

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

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

W
wusongqing 已提交
647
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 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661

**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
Z
zengyawen 已提交
662
imageSourceApi.getImageProperty("BitsPerSample",property,(error,data) => {})
W
wusongqing 已提交
663 664 665 666
```

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

Z
zengyawen 已提交
667
createPixelMap(options?: DecodingOptions): Promise\<PixelMap>
W
wusongqing 已提交
668

W
wusongqing 已提交
669
Creates a **PixelMap** object based on image decoding parameters. This API uses a promise to return the result.
W
wusongqing 已提交
670 671 672 673 674

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

**Parameters**

Z
zengyawen 已提交
675 676 677 678 679 680 681 682 683
| 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 已提交
684 685 686 687

**Example**

```js
Z
zengyawen 已提交
688 689
imageSourceApi.createPixelMap().then(pixelmap => {})
    						.catch(error => {})
W
wusongqing 已提交
690 691 692 693
```

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

Z
zengyawen 已提交
694
createPixelMap(callback: AsyncCallback\<PixelMap>): void
W
wusongqing 已提交
695

W
wusongqing 已提交
696
Creates a **PixelMap** object based on the default parameters. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
697 698 699 700 701

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

**Parameters**

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

**Example**

```js
Z
zengyawen 已提交
709
imageSourceApi.createPixelMap(pixelmap => {})
W
wusongqing 已提交
710 711 712 713
```

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

Z
zengyawen 已提交
714
createPixelMap(options: DecodingOptions, callback: AsyncCallback\<PixelMap>): void
W
wusongqing 已提交
715

W
wusongqing 已提交
716
Creates a **PixelMap** object based on image decoding parameters. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
717 718 719 720 721

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

**Parameters**

Z
zengyawen 已提交
722 723 724 725
| 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 已提交
726 727 728 729

**Example**

```js
Z
zengyawen 已提交
730
imageSourceApi.createPixelMap(decodingOptions, pixelmap => {})    
W
wusongqing 已提交
731 732 733 734 735 736
```

### release

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

W
wusongqing 已提交
737
Releases this **ImageSource** instance. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
738 739 740 741 742 743 744 745 746 747 748 749

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

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

## image.createImagePacker

createImagePacker(): ImagePacker

Creates an **ImagePacker** instance.

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

**Return value**

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

**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 已提交
805
packing(source: ImageSource, option: PackingOption, callback: AsyncCallback\<ArrayBuffer>): void
Z
zengyawen 已提交
806

W
wusongqing 已提交
807
Packs an image. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
808 809 810 811 812 813 814 815 816

**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 已提交
817
| callback | AsyncCallback\<ArrayBuffer> | Yes  | Callback used to return the packed data.|
Z
zengyawen 已提交
818 819 820 821 822 823 824 825 826 827

**Example**

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

### packing

W
wusongqing 已提交
828
packing(source: ImageSource, option: PackingOption): Promise\<ArrayBuffer>
Z
zengyawen 已提交
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844

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 已提交
845
| Promise\<ArrayBuffer> | Promise used to return the packed data.|
Z
zengyawen 已提交
846 847 848 849 850 851 852 853 854 855

**Example**

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

W
wusongqing 已提交
856
### packing<sup>8+</sup>
Z
zengyawen 已提交
857 858

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

W
wusongqing 已提交
860
Packs an image. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
861 862 863 864 865 866 867

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

**Parameters**

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

**Example**

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

W
wusongqing 已提交
879
### packing<sup>8+</sup>
W
wusongqing 已提交
880

W
wusongqing 已提交
881
packing(source: PixelMap, option: PackingOption): Promise\<ArrayBuffer>
W
wusongqing 已提交
882 883 884 885 886 887 888 889 890

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

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

**Parameters**

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

**Return value**

Z
zengyawen 已提交
896 897
| Type                        | Description                                         |
| :--------------------------- | :-------------------------------------------- |
W
wusongqing 已提交
898
| Promise\<ArrayBuffer> | Promise used to return the packed data.|
W
wusongqing 已提交
899 900 901 902 903

**Example**

```js
let packOpts = { format:["image/jpeg"], quality:98 }
Z
zengyawen 已提交
904 905 906
imagePackerApi.packing(pixelMapApi, packOpts)
    .then( data => { })
	.catch(error => {})
W
wusongqing 已提交
907 908 909 910 911 912
```

### release

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

W
wusongqing 已提交
913
Releases this **ImagePacker** instance. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
914 915 916 917 918 919 920 921 922 923 924 925

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

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

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

Describes area information in an image.

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

Z
zengyawen 已提交
956 957 958 959 960
| 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.                    |
W
wusongqing 已提交
961
| 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 已提交
962

Z
zengyawen 已提交
963
## ImageInfo
W
wusongqing 已提交
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 993 994 995

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 已提交
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
## AlphaType<sup>9+</sup>

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 opaque.|
| PREMUL   | 2      | Premultiplied alpha.         |
| UNPREMUL | 3      | Unpremultiplied alpha, that is, straight alpha.       |

## ScaleMode<sup>9+</sup>

Enumerates scale modes.

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

| Name           | Default Value| Description                                              |
| --------------- | ------ | -------------------------------------------------- |
W
wusongqing 已提交
1017 1018
| CENTER_CROP     | 1      | The image is scaled in such a way that it fits the dimensions of the target and centered in the target.|
| FIT_TARGET_SIZE | 2      | The image size is reduced to fit the dimensions of the target.                          |
W
wusongqing 已提交
1019

W
wusongqing 已提交
1020 1021 1022 1023 1024 1025
## InitializationOptions<sup>8+</sup>

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

| Name       | Type                              | Readable| Writable| Description          |
| ----------- | ---------------------------------- | ---- | ---- | -------------- |
W
wusongqing 已提交
1026
| alphaType<sup>9+</sup>   | [AlphaType](#alphatype9)           | Yes  | Yes  | Alpha type.      |
W
wusongqing 已提交
1027 1028
| editable    | boolean                            | Yes  | Yes  | Whether the image is editable.  |
| pixelFormat | [PixelMapFormat](#pixelmapformat7) | Yes  | Yes  | Pixel map format.    |
W
wusongqing 已提交
1029
| scaleMode<sup>9+</sup>   | [ScaleMode](#scalemode9)           | Yes  | Yes  | Scale mode.      |
W
wusongqing 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
| 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.|
Z
zengyawen 已提交
1041
| rotate             | number                             | Yes  | Yes  | Rotation angle.      |
W
wusongqing 已提交
1042 1043
| editable           | boolean                            | Yes  | Yes  | Whether the image is editable.    |
| desiredSize        | [Size](#size)                      | Yes  | Yes  | Expected output size.  |
Z
zengyawen 已提交
1044
| desiredRegion      | [Region](#region7)                 | Yes  | Yes  | Region to decode.      |
W
wusongqing 已提交
1045 1046 1047
| desiredPixelFormat | [PixelMapFormat](#pixelmapformat7) | Yes  | Yes  | Pixel map format for decoding.|
| index              | numer                              | Yes  | Yes  | Index of the image to decode.    |

Z
zengyawen 已提交
1048
## Region<sup>7+</sup>
W
wusongqing 已提交
1049 1050 1051 1052 1053

Describes region information.

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

Z
zengyawen 已提交
1054 1055 1056
| Name| Type         | Readable| Writable| Description        |
| ---- | ------------- | ---- | ---- | ------------ |
| size | [Size](#size) | Yes  | Yes  | Region size.  |
W
wusongqing 已提交
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 1083 1084 1085
| 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.

Z
zengyawen 已提交
1086 1087
**System capability**: SystemCapability.Multimedia.Image

W
wusongqing 已提交
1088 1089
| Name             | Default Value           | Description                |
| ----------------- | ----------------- | -------------------- |
W
wusongqing 已提交
1090
| BITS_PER_SAMPLE   | "BitsPerSample"   | Number of bits per pixel.    |
W
wusongqing 已提交
1091 1092 1093 1094 1095 1096 1097
| 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.|