js-apis-image.md 81.8 KB
Newer Older
Z
zengyawen 已提交
1
# 图片处理
X
xiaok 已提交
2

X
xu-rui-w 已提交
3 4
本模块提供图片处理效果,包括通过属性创建PixelMap、读取图像像素数据、读取区域内的图片数据等。

Z
zengyawen 已提交
5
> **说明:**
H
update  
HelloCrease 已提交
6
> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
Z
zengyawen 已提交
7

Z
zengyawen 已提交
8
## 导入模块
X
xiaok 已提交
9

Z
zengyawen 已提交
10
```js
Z
zengyawen 已提交
11
import image from '@ohos.multimedia.image';
X
xiaok 已提交
12 13
```

Z
zengyawen 已提交
14
## image.createPixelMap<sup>8+</sup>
X
xu-rui-w 已提交
15

16
createPixelMap(colors: ArrayBuffer, options: InitializationOptions): Promise\<PixelMap>
X
xiaok 已提交
17

X
xu-rui-w 已提交
18
通过属性创建PixelMap,默认采用BGRA_8888格式处理数据,通过Promise返回结果。
X
xiaok 已提交
19

X
xu-rui-w 已提交
20
**系统能力:** SystemCapability.Multimedia.Image.Core
X
xiaok 已提交
21 22 23

**参数:**

X
xu-rui-w 已提交
24 25 26
| 名称    | 类型                                             | 必填 | 说明                                                             |
| ------- | ------------------------------------------------ | ---- | ---------------------------------------------------------------- |
| colors  | ArrayBuffer                                      | 是   | BGRA_8888格式的颜色数组。                                        |
27
| options | [InitializationOptions](#initializationoptions8) | 是   | 创建像素的属性,包括透明度,尺寸,缩略值,像素格式和是否可编辑。 |
X
xiaok 已提交
28 29 30

**返回值:**

X
xu-rui-w 已提交
31 32 33 34
| 类型                             | 说明                                                                    |
| -------------------------------- | ----------------------------------------------------------------------- |
| Promise\<[PixelMap](#pixelmap7)> | 返回Pixelmap。<br>当创建的pixelmap大小超过原图大小时,返回原图pixelmap大小。|

X
xiaok 已提交
35 36 37 38

**示例:**

```js
X
xu-rui-w 已提交
39
const color = new ArrayBuffer(96);
X
xu-rui-w 已提交
40
let bufferArr = new Uint8Array(color);
X
xu-rui-w 已提交
41
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
F
fengzewu 已提交
42 43 44 45 46
image.createPixelMap(color, opts).then((pixelmap) => {
    console.log('Succeeded in creating pixelmap.');
}).catch(error => {
    console.log('Failed to create pixelmap.');
})
X
xiaok 已提交
47 48
```

Z
zengyawen 已提交
49
## image.createPixelMap<sup>8+</sup>
X
xiaok 已提交
50

51
createPixelMap(colors: ArrayBuffer, options: InitializationOptions, callback: AsyncCallback\<PixelMap>): void
X
xiaok 已提交
52

X
xu-rui-w 已提交
53
通过属性创建PixelMap,默认采用BGRA_8888格式处理数据,通过回调函数返回结果。
X
xiaok 已提交
54

X
xu-rui-w 已提交
55
**系统能力:** SystemCapability.Multimedia.Image.Core
X
xiaok 已提交
56 57 58

**参数:**

Z
zengyawen 已提交
59 60
| 名称     | 类型                                             | 必填 | 说明                       |
| -------- | ------------------------------------------------ | ---- | -------------------------- |
X
xu-rui-w 已提交
61
| colors   | ArrayBuffer                                      | 是   | BGRA_8888格式的颜色数组。  |
62
| options  | [InitializationOptions](#initializationoptions8) | 是   | 属性。                     |
Z
zengyawen 已提交
63
| callback | AsyncCallback\<[PixelMap](#pixelmap7)>           | 是   | 通过回调返回PixelMap对象。 |
X
xiaok 已提交
64 65 66 67

**示例:**

```js
X
xu-rui-w 已提交
68
const color = new ArrayBuffer(96);
X
xu-rui-w 已提交
69
let bufferArr = new Uint8Array(color);
X
xu-rui-w 已提交
70
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
X
xu-rui-w 已提交
71 72 73 74 75 76 77
image.createPixelMap(color, opts, (error, pixelmap) => {
    if(error) {
        console.log('Failed to create pixelmap.');
    } else {
        console.log('Succeeded in creating pixelmap.');
    }
})
X
xiaok 已提交
78 79
```

Z
zengyawen 已提交
80
## PixelMap<sup>7+</sup>
Z
zengyawen 已提交
81 82 83 84 85

图像像素类,用于读取或写入图像数据以及获取图像信息。在调用PixelMap的方法前,需要先通过createPixelMap创建一个PixelMap实例。

 ### 属性

X
xu-rui-w 已提交
86
**系统能力:** SystemCapability.Multimedia.Image.Core
Z
zhang-xiaobo1997 已提交
87

X
xu-rui-w 已提交
88 89 90
| 名称       | 类型    | 可读 | 可写 | 说明                       |
| ---------- | ------- | ---- | ---- | -------------------------- |
| isEditable | boolean | 是   | 否   | 设定是否图像像素可被编辑。 |
Z
zengyawen 已提交
91

Z
zengyawen 已提交
92
### readPixelsToBuffer<sup>7+</sup>
X
xiaok 已提交
93

Z
zengyawen 已提交
94
readPixelsToBuffer(dst: ArrayBuffer): Promise\<void>
X
xiaok 已提交
95

X
xu-rui-w 已提交
96
读取图像像素数据,结果写入ArrayBuffer里,使用Promise形式返回。指定BGRA_8888格式创建pixelmap,读取的像素数据与原数据保持一致。
X
xiaok 已提交
97

X
xu-rui-w 已提交
98
**系统能力:** SystemCapability.Multimedia.Image.Core
X
xiaok 已提交
99 100 101

**参数:**

X
xu-rui-w 已提交
102 103 104
| 参数名 | 类型        | 必填 | 说明                                                                                                  |
| ------ | ----------- | ---- | ----------------------------------------------------------------------------------------------------- |
| dst    | ArrayBuffer | 是   | 缓冲区,函数执行结束后获取的图像像素数据写入到该内存区域内。缓冲区大小由getPixelBytesNumber接口获取。 |
X
xiaok 已提交
105 106 107

**返回值:**

Z
zengyawen 已提交
108
| 类型           | 说明                                            |
X
xu-rui-w 已提交
109
| -------------- | ----------------------------------------------- |
Z
zengyawen 已提交
110
| Promise\<void> | Promise实例,用于获取结果,失败时返回错误信息。 |
X
xiaok 已提交
111 112 113 114

**示例:**

```js
X
xu-rui-w 已提交
115
const readBuffer = new ArrayBuffer(96);
X
xu-rui-w 已提交
116 117
pixelmap.readPixelsToBuffer(readBuffer).then(() => {
    console.log('Succeeded in reading image pixel data.');  //符合条件则进入 
X
xu-rui-w 已提交
118
}).catch(error => {
X
xu-rui-w 已提交
119
    console.log('Failed to read image pixel data.');  //不符合条件则进入
X
xu-rui-w 已提交
120
})
X
xiaok 已提交
121 122
```

Z
zengyawen 已提交
123
### readPixelsToBuffer<sup>7+</sup>
X
xiaok 已提交
124

Z
zengyawen 已提交
125
readPixelsToBuffer(dst: ArrayBuffer, callback: AsyncCallback\<void>): void
X
xiaok 已提交
126

X
xu-rui-w 已提交
127
读取图像像素数据,结果写入ArrayBuffer里,使用callback形式返回。指定BGRA_8888格式创建pixelmap,读取的像素数据与原数据保持一致。
X
xiaok 已提交
128

X
xu-rui-w 已提交
129
**系统能力:** SystemCapability.Multimedia.Image.Core
X
xiaok 已提交
130 131 132

**参数:**

X
xu-rui-w 已提交
133 134 135 136
| 参数名   | 类型                 | 必填 | 说明                                                                                                  |
| -------- | -------------------- | ---- | ----------------------------------------------------------------------------------------------------- |
| dst      | ArrayBuffer          | 是   | 缓冲区,函数执行结束后获取的图像像素数据写入到该内存区域内。缓冲区大小由getPixelBytesNumber接口获取。 |
| callback | AsyncCallback\<void> | 是   | 获取回调,失败时返回错误信息。                                                                        |
X
xiaok 已提交
137 138 139 140

**示例:**

```js
X
xu-rui-w 已提交
141
const readBuffer = new ArrayBuffer(96);
X
xu-rui-w 已提交
142
pixelmap.readPixelsToBuffer(readBuffer, (err, res) => {
X
xu-rui-w 已提交
143
    if(err) {
X
xu-rui-w 已提交
144
        console.log('Failed to read image pixel data.');  //不符合条件则进入
X
xu-rui-w 已提交
145
    } else {
X
xu-rui-w 已提交
146
        console.log('Succeeded in reading image pixel data.');  //符合条件则进入
X
xu-rui-w 已提交
147 148
    }
})
X
xiaok 已提交
149 150
```

Z
zengyawen 已提交
151
### readPixels<sup>7+</sup>
X
xiaok 已提交
152

Z
zengyawen 已提交
153
readPixels(area: PositionArea): Promise\<void>
X
xiaok 已提交
154 155 156

读取区域内的图片数据,使用Promise形式返回读取结果。

X
xu-rui-w 已提交
157
**系统能力:** SystemCapability.Multimedia.Image.Core
X
xiaok 已提交
158 159 160

**参数:**

Z
zengyawen 已提交
161 162 163
| 参数名 | 类型                           | 必填 | 说明                     |
| ------ | ------------------------------ | ---- | ------------------------ |
| area   | [PositionArea](#positionarea7) | 是   | 区域大小,根据区域读取。 |
X
xiaok 已提交
164 165 166

**返回值:**

Z
zengyawen 已提交
167 168 169
| 类型           | 说明                                                |
| :------------- | :-------------------------------------------------- |
| Promise\<void> | Promise实例,用于获取读取结果,失败时返回错误信息。 |
X
xiaok 已提交
170 171 172 173

**示例:**

```js
X
xu-rui-w 已提交
174 175 176 177 178 179
const area = {
    pixels: new ArrayBuffer(8),
    offset: 0,
    stride: 8,
    region: { size: { height: 1, width: 2 }, x: 0, y: 0 }
}
X
xu-rui-w 已提交
180 181
pixelmap.readPixels(area).then(() => {
    console.log('Succeeded in reading the image data in the area.'); //符合条件则进入
X
xu-rui-w 已提交
182
}).catch(error => {
X
xu-rui-w 已提交
183
    console.log('Failed to read the image data in the area.'); //不符合条件则进入
X
xu-rui-w 已提交
184
})
X
xiaok 已提交
185 186
```

Z
zengyawen 已提交
187
### readPixels<sup>7+</sup>
X
xiaok 已提交
188

Z
zengyawen 已提交
189
readPixels(area: PositionArea, callback: AsyncCallback\<void>): void
X
xiaok 已提交
190

Z
zengyawen 已提交
191
读取区域内的图片数据,使用callback形式返回读取结果。
X
xiaok 已提交
192

X
xu-rui-w 已提交
193
**系统能力:** SystemCapability.Multimedia.Image.Core
X
xiaok 已提交
194 195 196

**参数:**

Z
zengyawen 已提交
197 198 199 200
| 参数名   | 类型                           | 必填 | 说明                           |
| -------- | ------------------------------ | ---- | ------------------------------ |
| area     | [PositionArea](#positionarea7) | 是   | 区域大小,根据区域读取。       |
| callback | AsyncCallback\<void>           | 是   | 获取回调,失败时返回错误信息。 |
X
xiaok 已提交
201 202 203 204

**示例:**

```js
X
xu-rui-w 已提交
205
const color = new ArrayBuffer(96);
X
xu-rui-w 已提交
206
let bufferArr = new Uint8Array(color);
207 208
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts, (err, pixelmap) => {
X
xu-rui-w 已提交
209 210 211 212 213 214 215 216 217 218 219
    if(pixelmap == undefined){
        console.info('createPixelMap failed.');
    } else {
        const area = { pixels: new ArrayBuffer(8),
            offset: 0,
            stride: 8,
            region: { size: { height: 1, width: 2 }, x: 0, y: 0 }};
        pixelmap.readPixels(area, () => {
            console.info('readPixels success');
        })
    }
220
})
X
xiaok 已提交
221 222
```

Z
zengyawen 已提交
223
### writePixels<sup>7+</sup>
X
xiaok 已提交
224

Z
zengyawen 已提交
225
writePixels(area: PositionArea): Promise\<void>
X
xiaok 已提交
226

227
将PixelMap写入指定区域内,使用Promise形式返回写入结果。
X
xiaok 已提交
228

X
xu-rui-w 已提交
229
**系统能力:** SystemCapability.Multimedia.Image.Core
X
xiaok 已提交
230 231 232

**参数:** 

Z
zengyawen 已提交
233 234
| 参数名 | 类型                           | 必填 | 说明                 |
| ------ | ------------------------------ | ---- | -------------------- |
Z
zengyawen 已提交
235
| area   | [PositionArea](#positionarea7) | 是   | 区域,根据区域写入。 |
X
xiaok 已提交
236 237 238

**返回值:**

Z
zengyawen 已提交
239 240 241
| 类型           | 说明                                                |
| :------------- | :-------------------------------------------------- |
| Promise\<void> | Promise实例,用于获取写入结果,失败时返回错误信息。 |
X
xiaok 已提交
242 243 244 245

**示例:**

```js
246
const color = new ArrayBuffer(96);
X
xu-rui-w 已提交
247
let bufferArr = new Uint8Array(color);
248 249 250 251
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts)
    .then( pixelmap => {
        if (pixelmap == undefined) {
X
xu-rui-w 已提交
252
            console.info('createPixelMap failed.');
253 254 255 256 257 258
        }
        const area = { pixels: new ArrayBuffer(8),
            offset: 0,
            stride: 8,
            region: { size: { height: 1, width: 2 }, x: 0, y: 0 }
        }
X
xu-rui-w 已提交
259
        let bufferArr = new Uint8Array(area.pixels);
260 261 262 263 264
        for (var i = 0; i < bufferArr.length; i++) {
            bufferArr[i] = i + 1;
        }

        pixelmap.writePixels(area).then(() => {
F
fengzewu 已提交
265
		    console.info('Succeeded to write pixelmap into the specified area.');
266
        })
X
xu-rui-w 已提交
267
    }).catch(error => {
268 269
        console.log('error: ' + error);
    })
X
xiaok 已提交
270 271
```

Z
zengyawen 已提交
272
### writePixels<sup>7+</sup>
X
xiaok 已提交
273

Z
zengyawen 已提交
274
writePixels(area: PositionArea, callback: AsyncCallback\<void>): void
X
xiaok 已提交
275

276
将PixelMap写入指定区域内,使用callback形式返回写入结果。
X
xiaok 已提交
277

X
xu-rui-w 已提交
278
**系统能力:** SystemCapability.Multimedia.Image.Core
X
xiaok 已提交
279 280 281

**参数:** 

Z
zengyawen 已提交
282 283
| 参数名    | 类型                           | 必填 | 说明                           |
| --------- | ------------------------------ | ---- | ------------------------------ |
Z
zengyawen 已提交
284
| area      | [PositionArea](#positionarea7) | 是   | 区域,根据区域写入。           |
X
xu-rui-w 已提交
285
| callback  | AsyncCallback\<void>           | 是   | 获取回调,失败时返回错误信息。 |
X
xiaok 已提交
286 287 288 289

**示例:**

```js
F
fengzewu 已提交
290 291 292 293 294 295 296 297 298
const area = { pixels: new ArrayBuffer(8),
    offset: 0,
    stride: 8,
    region: { size: { height: 1, width: 2 }, x: 0, y: 0 }
}
let bufferArr = new Uint8Array(area.pixels);
for (var i = 0; i < bufferArr.length; i++) {
    bufferArr[i] = i + 1;
}
X
xu-rui-w 已提交
299
pixelmap.writePixels(area, (error) => {
X
xu-rui-w 已提交
300
    if (error != undefined) {
X
xu-rui-w 已提交
301
		console.info('Failed to write pixelmap into the specified area.');
X
xu-rui-w 已提交
302
	} else {
F
fengzewu 已提交
303
		console.info('Succeeded to write pixelmap into the specified area.');
X
xu-rui-w 已提交
304
	}
X
xu-rui-w 已提交
305
})
X
xiaok 已提交
306 307
```

Z
zengyawen 已提交
308
### writeBufferToPixels<sup>7+</sup>
X
xiaok 已提交
309

Z
zengyawen 已提交
310
writeBufferToPixels(src: ArrayBuffer): Promise\<void>
X
xiaok 已提交
311

312
读取缓冲区中的图片数据,结果写入PixelMap中,使用Promise形式返回。
X
xiaok 已提交
313

X
xu-rui-w 已提交
314
**系统能力:** SystemCapability.Multimedia.Image.Core
X
xiaok 已提交
315 316 317

**参数:**

Z
zengyawen 已提交
318 319 320
| 参数名 | 类型        | 必填 | 说明           |
| ------ | ----------- | ---- | -------------- |
| src    | ArrayBuffer | 是   | 图像像素数据。 |
X
xiaok 已提交
321 322 323

**返回值:**

Z
zengyawen 已提交
324 325 326
| 类型           | 说明                                            |
| -------------- | ----------------------------------------------- |
| Promise\<void> | Promise实例,用于获取结果,失败时返回错误信息。 |
X
xiaok 已提交
327 328 329 330

**示例:**

```js
X
xu-rui-w 已提交
331
const color = new ArrayBuffer(96);
X
xu-rui-w 已提交
332
let bufferArr = new Uint8Array(color);
F
fengzewu 已提交
333 334 335 336
for (var i = 0; i < bufferArr.length; i++) {
    bufferArr[i] = i + 1;
}
pixelmap.writeBufferToPixels(color).then(() => {
X
xiaok 已提交
337 338 339
    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.");
X
xu-rui-w 已提交
340
})
X
xiaok 已提交
341 342
```

Z
zengyawen 已提交
343
### writeBufferToPixels<sup>7+</sup>
X
xiaok 已提交
344

Z
zengyawen 已提交
345
writeBufferToPixels(src: ArrayBuffer, callback: AsyncCallback\<void>): void
X
xiaok 已提交
346

347
读取缓冲区中的图片数据,结果写入PixelMap中,使用callback形式返回。
X
xiaok 已提交
348

X
xu-rui-w 已提交
349
**系统能力:** SystemCapability.Multimedia.Image.Core
X
xiaok 已提交
350 351 352

**参数:**

Z
zengyawen 已提交
353 354 355 356
| 参数名   | 类型                 | 必填 | 说明                           |
| -------- | -------------------- | ---- | ------------------------------ |
| src      | ArrayBuffer          | 是   | 图像像素数据。                 |
| callback | AsyncCallback\<void> | 是   | 获取回调,失败时返回错误信息。 |
X
xiaok 已提交
357 358 359 360

**示例:**

```js
X
xu-rui-w 已提交
361
const color = new ArrayBuffer(96);
X
xu-rui-w 已提交
362
let bufferArr = new Uint8Array(color);
F
fengzewu 已提交
363 364 365 366
for (var i = 0; i < bufferArr.length; i++) {
    bufferArr[i] = i + 1;
}
pixelmap.writeBufferToPixels(color, function(err) {
X
xiaok 已提交
367 368 369
    if (err) {
        console.error("Failed to write data from a buffer to a PixelMap.");
        return;
X
xu-rui-w 已提交
370 371 372
    } else {
		console.log("Succeeded in writing data from a buffer to a PixelMap.");
	}
X
xiaok 已提交
373 374 375
});
```

Z
zengyawen 已提交
376
### getImageInfo<sup>7+</sup>
X
xiaok 已提交
377

Z
zengyawen 已提交
378
getImageInfo(): Promise\<ImageInfo>
X
xiaok 已提交
379 380 381

获取图像像素信息,使用Promise形式返回获取的图像像素信息。

X
xu-rui-w 已提交
382
**系统能力:** SystemCapability.Multimedia.Image.Core
X
xiaok 已提交
383 384 385

**返回值:**

Z
zengyawen 已提交
386 387 388
| 类型                              | 说明                                                        |
| --------------------------------- | ----------------------------------------------------------- |
| Promise\<[ImageInfo](#imageinfo)> | Promise实例,用于异步获取图像像素信息,失败时返回错误信息。 |
X
xiaok 已提交
389 390 391 392

**示例:**

```js
F
fengzewu 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
const color = new ArrayBuffer(96);
let opts = { editable: true, pixelFormat: 2, size: { height: 6, width: 8 } }
image.createPixelMap(color, opts).then(pixelmap => {
    if (pixelmap == undefined) {
        console.error("Failed to obtain the image pixel map information.");
    }
    pixelmap.getImageInfo().then(imageInfo => {
        if (imageInfo == undefined) {
            console.error("Failed to obtain the image pixel map information.");
        }
        if (imageInfo.size.height == 4 && imageInfo.size.width == 6) {
            console.log("Succeeded in obtaining the image pixel map information.");
        }
    })
})
X
xiaok 已提交
408 409
```

Z
zengyawen 已提交
410
### getImageInfo<sup>7+</sup>
X
xiaok 已提交
411

Z
zengyawen 已提交
412
getImageInfo(callback: AsyncCallback\<ImageInfo>): void
X
xiaok 已提交
413 414 415

获取图像像素信息,使用callback形式返回获取的图像像素信息。

X
xu-rui-w 已提交
416
**系统能力:** SystemCapability.Multimedia.Image.Core
X
xiaok 已提交
417 418 419

**参数:**

Z
zengyawen 已提交
420 421 422
| 参数名   | 类型                                    | 必填 | 说明                                                         |
| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
| callback | AsyncCallback\<[ImageInfo](#imageinfo)> | 是   | 获取图像像素信息回调,异步返回图像像素信息,失败时返回错误信息。 |
X
xiaok 已提交
423 424 425 426

**示例:**

```js
F
fengzewu 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440
const color = new ArrayBuffer(96);
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts, (err, pixelmap) => {
    if (pixelmap == undefined) {
        console.error("Failed to obtain the image pixel map information.");
    }
    pixelmap.getImageInfo((err, imageInfo) => {
        if (imageInfo == undefined) {
            console.error("Failed to obtain the image pixel map information.");
        }
        if (imageInfo.size.height == 4 && imageInfo.size.width == 6) {
            console.log("Succeeded in obtaining the image pixel map information.");
        }
    })
X
xu-rui-w 已提交
441
})
X
xiaok 已提交
442 443
```

Z
zengyawen 已提交
444
### getBytesNumberPerRow<sup>7+</sup>
X
xiaok 已提交
445

Z
zengyawen 已提交
446
getBytesNumberPerRow(): number
X
xiaok 已提交
447 448 449

获取图像像素每行字节数。

X
xu-rui-w 已提交
450
**系统能力:** SystemCapability.Multimedia.Image.Core
X
xiaok 已提交
451 452 453

**返回值:**

Z
zengyawen 已提交
454 455 456
| 类型   | 说明                 |
| ------ | -------------------- |
| number | 图像像素的行字节数。 |
X
xiaok 已提交
457 458 459 460

**示例:**

```js
X
xu-rui-w 已提交
461
const color = new ArrayBuffer(96);
X
xu-rui-w 已提交
462
let bufferArr = new Uint8Array(color);
X
xu-rui-w 已提交
463
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
X
xu-rui-w 已提交
464
image.createPixelMap(color, opts, (err,pixelmap) => {
X
xu-rui-w 已提交
465 466
    let rowCount = pixelmap.getBytesNumberPerRow();
})
X
xiaok 已提交
467 468
```

Z
zengyawen 已提交
469
### getPixelBytesNumber<sup>7+</sup>
X
xiaok 已提交
470

Z
zengyawen 已提交
471
getPixelBytesNumber(): number
X
xiaok 已提交
472 473 474

获取图像像素的总字节数。

X
xu-rui-w 已提交
475
**系统能力:** SystemCapability.Multimedia.Image.Core
X
xiaok 已提交
476 477 478

**返回值:**

Z
zengyawen 已提交
479 480 481
| 类型   | 说明                 |
| ------ | -------------------- |
| number | 图像像素的总字节数。 |
X
xiaok 已提交
482 483 484 485

**示例:**

```js
X
xu-rui-w 已提交
486
let pixelBytesNumber = pixelmap.getPixelBytesNumber();
X
xiaok 已提交
487 488
```

X
xu-rui-w 已提交
489 490 491 492
### getDensity<sup>9+</sup>

getDensity():number

X
xu-rui-w 已提交
493
获取当前图像像素的密度。
X
xu-rui-w 已提交
494 495 496 497 498

**系统能力:** SystemCapability.Multimedia.Image.Core

**返回值:**

X
xu-rui-w 已提交
499 500 501
| 类型   | 说明            |
| ------ | --------------- |
| number | 图像像素的密度。|
X
xu-rui-w 已提交
502 503 504 505 506 507 508 509 510 511 512

**示例:**

```js
let getDensity = pixelmap.getDensity();
```

### opacity<sup>9+</sup>

opacity(rate: number, callback: AsyncCallback\<void>): void

X
xu-rui-w 已提交
513
通过设置透明比率来让PixelMap达到对应的透明效果,使用callback形式返回。
X
xu-rui-w 已提交
514 515 516 517 518 519 520 521 522 523 524 525 526

**系统能力:** SystemCapability.Multimedia.Image.Core

**参数:**

| 参数名   | 类型                 | 必填 | 说明                           |
| -------- | -------------------- | ---- | ------------------------------ |
| rate     | number               | 是   | 透明比率的值,取值范围:0-1。   |
| callback | AsyncCallback\<void> | 是   | 获取回调,失败时返回错误信息。 |

**示例:**

```js
F
fengzewu 已提交
527 528 529 530 531 532 533 534 535
var rate = 0.5;
pixelmap.opacity(rate, (err) => {
	if (err) {
        console.error("Failed to set opacity.");
        return;
    } else {
        console.log("Succeeded in setting opacity.");
	}
})
X
xu-rui-w 已提交
536 537 538 539 540 541
```

### opacity<sup>9+</sup>

opacity(rate: number): Promise\<void>

X
xu-rui-w 已提交
542
通过设置透明比率来让PixelMap达到对应的透明效果,使用Promise形式返回。
X
xu-rui-w 已提交
543 544 545 546 547 548 549

**系统能力:** SystemCapability.Multimedia.Image.Core

**参数:**

| 参数名 | 类型   | 必填 | 说明                        |
| ------ | ------ | ---- | --------------------------- |
X
xu-rui-w 已提交
550
| rate   | number | 是   | 透明比率的值,取值范围:0-1。|
X
xu-rui-w 已提交
551 552 553 554 555 556 557 558 559 560

**返回值:**

| 类型           | 说明                                            |
| -------------- | ----------------------------------------------- |
| Promise\<void> | Promise实例,用于获取结果,失败时返回错误信息。 |

**示例:**

```js
F
fengzewu 已提交
561
async function Demo() {
F
fengzewu 已提交
562
    await pixelmap.opacity(0.5);
X
xu-rui-w 已提交
563
}
X
xu-rui-w 已提交
564 565 566 567 568 569
```

### createAlphaPixelmap<sup>9+</sup>

createAlphaPixelmap(): Promise\<PixelMap>

X
xu-rui-w 已提交
570
根据Alpha通道的信息,来生成一个仅包含Alpha通道信息的pixelmap,可用于阴影效果,使用Promise形式返回。
X
xu-rui-w 已提交
571 572 573 574 575 576 577 578 579 580 581 582

**系统能力:** SystemCapability.Multimedia.Image.Core

**返回值:**

| 类型                             | 说明                        |
| -------------------------------- | --------------------------- |
| Promise\<[PixelMap](#pixelmap7)> | Promise实例,返回pixelmap。 |

**示例:**

```js
F
fengzewu 已提交
583
async function Demo() {
F
fengzewu 已提交
584 585
    await pixelmap.createAlphaPixelmap();
}   
X
xu-rui-w 已提交
586 587 588 589 590 591
```

### createAlphaPixelmap<sup>9+</sup>

createAlphaPixelmap(callback: AsyncCallback\<PixelMap>): void

X
xu-rui-w 已提交
592
根据Alpha通道的信息,来生成一个仅包含Alpha通道信息的pixelmap,可用于阴影效果,使用callback形式返回。
X
xu-rui-w 已提交
593 594 595 596 597

**系统能力:** SystemCapability.Multimedia.Image.Core

**参数:**

X
xu-rui-w 已提交
598 599 600
| 参数名   | 类型                     | 必填 | 说明                     |
| -------- | ------------------------ | ---- | ------------------------ |
| callback | AsyncCallback\<PixelMap> | 是   | 获取回调,异步返回结果。 |
X
xu-rui-w 已提交
601 602 603 604

**示例:**

```js
F
fengzewu 已提交
605 606 607 608 609 610 611
pixelmap.createAlphaPixelmap((err, alphaPixelMap) => {
    if (alphaPixelMap == undefined) {
        console.info('Failed to obtain new pixel map.');
    } else {
        console.info('Succeed in obtaining new pixel map.');
    }
})
X
xu-rui-w 已提交
612 613 614 615 616 617
```

### scale<sup>9+</sup>

scale(x: number, y: number, callback: AsyncCallback\<void>): void

X
xu-rui-w 已提交
618
根据输入的宽高对图片进行缩放,使用callback形式返回。
X
xu-rui-w 已提交
619 620 621 622 623

**系统能力:** SystemCapability.Multimedia.Image.Core

**参数:**

X
xu-rui-w 已提交
624 625 626 627 628
| 参数名   | 类型                 | 必填 | 说明                            |
| -------- | -------------------- | ---- | ------------------------------- |
| x        | number               | 是   | 宽度的缩放值,其值为输入的倍数。|
| y        | number               | 是   | 高度的缩放值,其值为输入的倍数。|
| callback | AsyncCallback\<void> | 是   | 获取回调,失败时返回错误信息。  |
X
xu-rui-w 已提交
629 630 631 632

**示例:**

```js
F
fengzewu 已提交
633
async function Demo() {
F
fengzewu 已提交
634
	await pixelmap.scale(2.0, 1.0);
X
xu-rui-w 已提交
635
}
X
xu-rui-w 已提交
636 637 638 639 640 641
```

### scale<sup>9+</sup>

scale(x: number, y: number): Promise\<void>

X
xu-rui-w 已提交
642
根据输入的宽高对图片进行缩放,使用Promise形式返回。
X
xu-rui-w 已提交
643 644 645 646 647

**系统能力:** SystemCapability.Multimedia.Image.Core

**参数:**

X
xu-rui-w 已提交
648 649 650 651
| 参数名 | 类型   | 必填 | 说明                            |
| ------ | ------ | ---- | ------------------------------- |
| x      | number | 是   | 宽度的缩放值,其值为输入的倍数。|
| y      | number | 是   | 高度的缩放值,其值为输入的倍数。|
X
xu-rui-w 已提交
652 653 654

**返回值:**

X
xu-rui-w 已提交
655 656 657
| 类型           | 说明                        |
| -------------- | --------------------------- |
| Promise\<void> | Promise实例,异步返回结果。 |
X
xu-rui-w 已提交
658 659 660 661

**示例:**

```js
F
fengzewu 已提交
662
async function Demo() {
F
fengzewu 已提交
663
	await pixelmap.scale(2.0, 1.0);
X
xu-rui-w 已提交
664
}
X
xu-rui-w 已提交
665 666 667 668 669 670
```

### translate<sup>9+</sup>

translate(x: number, y: number, callback: AsyncCallback\<void>): void

X
xu-rui-w 已提交
671
根据输入的坐标对图片进行位置变换,使用callback形式返回。
X
xu-rui-w 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685

**系统能力:** SystemCapability.Multimedia.Image.Core

**参数:**

| 参数名   | 类型                 | 必填 | 说明                          |
| -------- | -------------------- | ---- | ----------------------------- |
| x        | number               | 是   | 区域横坐标。                  |
| y        | number               | 是   | 区域纵坐标。                  |
| callback | AsyncCallback\<void> | 是   | 获取回调,失败时返回错误信息。|

**示例:**

```js
F
fengzewu 已提交
686
async function Demo() {
F
fengzewu 已提交
687
	await pixelmap.translate(3.0, 1.0);
X
xu-rui-w 已提交
688
}
X
xu-rui-w 已提交
689 690 691 692 693 694
```

### translate<sup>9+</sup>

translate(x: number, y: number): Promise\<void>

X
xu-rui-w 已提交
695
根据输入的坐标对图片进行位置变换,使用Promise形式返回。
X
xu-rui-w 已提交
696 697 698 699 700 701 702 703 704 705 706 707

**系统能力:** SystemCapability.Multimedia.Image.Core

**参数:**

| 参数名 | 类型   | 必填 | 说明        |
| ------ | ------ | ---- | ----------- |
| x      | number | 是   | 区域横坐标。|
| y      | number | 是   | 区域纵坐标。|

**返回值:**

X
xu-rui-w 已提交
708 709 710
| 类型           | 说明                        |
| -------------- | --------------------------- |
| Promise\<void> | Promise实例,异步返回结果。 |
X
xu-rui-w 已提交
711 712 713 714

**示例:**

```js
F
fengzewu 已提交
715
async function Demo() {
F
fengzewu 已提交
716
	await pixelmap.translate(3.0, 1.0);
X
xu-rui-w 已提交
717
}
X
xu-rui-w 已提交
718 719 720 721 722 723
```

### rotate<sup>9+</sup>

rotate(angle: number, callback: AsyncCallback\<void>): void

X
xu-rui-w 已提交
724
根据输入的角度对图片进行旋转,使用callback形式返回。
X
xu-rui-w 已提交
725 726 727 728 729 730 731 732 733 734 735 736 737

**系统能力:** SystemCapability.Multimedia.Image.Core

**参数:**

| 参数名   | 类型                 | 必填 | 说明                          |
| -------- | -------------------- | ---- | ----------------------------- |
| angle    | number               | 是   | 图片旋转的角度。              |
| callback | AsyncCallback\<void> | 是   | 获取回调,失败时返回错误信息。|

**示例:**

```js
F
fengzewu 已提交
738
async function Demo() {
F
fengzewu 已提交
739
	await pixelmap.rotate(90.0);
X
xu-rui-w 已提交
740
}
X
xu-rui-w 已提交
741 742 743 744 745 746
```

### rotate<sup>9+</sup>

rotate(angle: number): Promise\<void>

X
xu-rui-w 已提交
747
根据输入的角度对图片进行旋转,使用Promise形式返回。
X
xu-rui-w 已提交
748 749 750 751 752 753 754 755 756 757 758

**系统能力:** SystemCapability.Multimedia.Image.Core

**参数:**

| 参数名 | 类型   | 必填 | 说明                          |
| ------ | ------ | ---- | ----------------------------- |
| angle  | number | 是   | 图片旋转的角度。              |

**返回值:**

X
xu-rui-w 已提交
759 760 761
| 类型           | 说明                        |
| -------------- | --------------------------- |
| Promise\<void> | Promise实例,异步返回结果。 |
X
xu-rui-w 已提交
762 763 764 765

**示例:**

```js
F
fengzewu 已提交
766
async function Demo() {
F
fengzewu 已提交
767
	await pixelmap.rotate(90.0);
X
xu-rui-w 已提交
768
}
X
xu-rui-w 已提交
769 770 771 772 773 774
```

### flip<sup>9+</sup>

flip(horizontal: boolean, vertical: boolean, callback: AsyncCallback\<void>): void

X
xu-rui-w 已提交
775
根据输入的条件对图片进行翻转,使用callback形式返回。
X
xu-rui-w 已提交
776 777 778 779 780 781 782 783 784 785 786 787 788 789

**系统能力:** SystemCapability.Multimedia.Image.Core

**参数:**

| 参数名     | 类型                 | 必填 | 说明                          |
| ---------- | -------------------- | ---- | ----------------------------- |
| horizontal | boolean              | 是   | 水平翻转。                    |
| vertical   | boolean              | 是   | 垂直翻转。                    |
| callback   | AsyncCallback\<void> | 是   | 获取回调,失败时返回错误信息。|

**示例:**

```js
F
fengzewu 已提交
790
async function Demo() {
F
fengzewu 已提交
791
	await pixelmap.flip(false, true);
X
xu-rui-w 已提交
792
}
X
xu-rui-w 已提交
793 794 795 796 797 798
```

### flip<sup>9+</sup>

flip(horizontal: boolean, vertical: boolean): Promise\<void>

X
xu-rui-w 已提交
799
根据输入的条件对图片进行翻转,使用Promise形式返回。
X
xu-rui-w 已提交
800 801 802 803 804 805 806 807 808 809 810 811

**系统能力:** SystemCapability.Multimedia.Image.Core

**参数:**

| 参数名     | 类型    | 必填 | 说明      |
| ---------- | ------- | ---- | --------- |
| horizontal | boolean | 是   | 水平翻转。|
| vertical   | boolean | 是   | 垂直翻转。|

**返回值:**

X
xu-rui-w 已提交
812 813 814
| 类型           | 说明                        |
| -------------- | --------------------------- |
| Promise\<void> | Promise实例,异步返回结果。 |
X
xu-rui-w 已提交
815 816 817 818

**示例:**

```js
F
fengzewu 已提交
819
async function Demo() {
F
fengzewu 已提交
820
	await pixelmap.flip(false, true);
X
xu-rui-w 已提交
821
}
X
xu-rui-w 已提交
822 823 824 825 826 827
```

### crop<sup>9+</sup>

crop(region: Region, callback: AsyncCallback\<void>): void

X
xu-rui-w 已提交
828
根据输入的尺寸对图片进行裁剪,使用callback形式返回。
X
xu-rui-w 已提交
829 830 831 832 833 834 835

**系统能力:** SystemCapability.Multimedia.Image.Core

**参数:**

| 参数名   | 类型                 | 必填 | 说明                          |
| -------- | -------------------- | ---- | ----------------------------- |
X
xu-rui-w 已提交
836
| region   | [Region](#region7)   | 是   | 裁剪的尺寸。                  |
X
xu-rui-w 已提交
837 838 839 840 841
| callback | AsyncCallback\<void> | 是   | 获取回调,失败时返回错误信息。|

**示例:**

```js
F
fengzewu 已提交
842
async function Demo() {
F
fengzewu 已提交
843
	await pixelmap.crop({ x: 0, y: 0, size: { height: 100, width: 100 } });
X
xu-rui-w 已提交
844
}
X
xu-rui-w 已提交
845 846 847 848 849 850
```

### crop<sup>9+</sup>

crop(region: Region): Promise\<void>

X
xu-rui-w 已提交
851
根据输入的尺寸对图片进行裁剪,使用Promise形式返回。
X
xu-rui-w 已提交
852 853 854 855 856

**系统能力:** SystemCapability.Multimedia.Image.Core

**参数:**

X
xu-rui-w 已提交
857 858 859
| 参数名 | 类型               | 必填 | 说明        |
| ------ | ------------------ | ---- | ----------- |
| region | [Region](#region7) | 是   | 裁剪的尺寸。|
X
xu-rui-w 已提交
860 861 862

**返回值:**

X
xu-rui-w 已提交
863 864 865
| 类型           | 说明                        |
| -------------- | --------------------------- |
| Promise\<void> | Promise实例,异步返回结果。 |
X
xu-rui-w 已提交
866 867 868 869

**示例:**

```js
F
fengzewu 已提交
870
async function Demo() {
F
fengzewu 已提交
871
	await pixelmap.crop({ x: 0, y: 0, size: { height: 100, width: 100 } });
X
xu-rui-w 已提交
872
}
X
xu-rui-w 已提交
873 874
```

Z
zengyawen 已提交
875
### release<sup>7+</sup>
X
xiaok 已提交
876

Z
zengyawen 已提交
877
release():Promise\<void>
X
xiaok 已提交
878

879
释放PixelMap对象,使用Promise形式返回释放结果。
X
xiaok 已提交
880

X
xu-rui-w 已提交
881
**系统能力:** SystemCapability.Multimedia.Image.Core
X
xiaok 已提交
882

Z
zengyawen 已提交
883
**返回值:**
X
xiaok 已提交
884

X
xu-rui-w 已提交
885 886 887
| 类型           | 说明                            |
| -------------- | ------------------------------- |
| Promise\<void> | Promise实例,异步返回释放结果。 |
X
xiaok 已提交
888 889 890 891

**示例:**

```js
F
fengzewu 已提交
892 893 894 895
pixelmap.release().then(() => {
	console.log('Succeeded in releasing pixelmap object.');
}).catch(error => {
	console.log('Failed to release pixelmap object.');
X
xu-rui-w 已提交
896
})
X
xiaok 已提交
897 898
```

Z
zengyawen 已提交
899
### release<sup>7+</sup>
X
xiaok 已提交
900

Z
zengyawen 已提交
901
release(callback: AsyncCallback\<void>): void
X
xiaok 已提交
902

903
释放PixelMap对象,使用callback形式返回释放结果。
X
xiaok 已提交
904

X
xu-rui-w 已提交
905
**系统能力:** SystemCapability.Multimedia.Image.Core
X
xiaok 已提交
906 907 908

**参数:**

Z
zengyawen 已提交
909 910 911
| 名称     | 类型                 | 必填 | 说明               |
| -------- | -------------------- | ---- | ------------------ |
| callback | AsyncCallback\<void> | 是   | 异步返回释放结果。 |
X
xiaok 已提交
912 913 914 915

**示例:**

```js
F
fengzewu 已提交
916 917
pixelmap.release(() => {
    console.log('Succeeded in releasing pixelmap object.');
X
xu-rui-w 已提交
918
})
X
xiaok 已提交
919 920
```

Z
zengyawen 已提交
921
## image.createImageSource
X
xiaok 已提交
922

Z
zengyawen 已提交
923
createImageSource(uri: string): ImageSource
X
xiaok 已提交
924

Z
zengyawen 已提交
925
通过传入的uri创建图片源实例。
X
xiaok 已提交
926

X
xu-rui-w 已提交
927
**系统能力:** SystemCapability.Multimedia.Image.ImageSource
X
xiaok 已提交
928 929 930

**参数:**

Z
zengyawen 已提交
931 932
| 参数名 | 类型   | 必填 | 说明                               |
| ------ | ------ | ---- | ---------------------------------- |
X
xu-rui-w 已提交
933
| uri    | string | 是   | 图片路径,当前仅支持应用沙箱路径。</br>当前支持格式有:.jpg .png .gif .bmp .webp RAW。 |
X
xiaok 已提交
934 935 936

**返回值:**

937 938 939
| 类型                        | 说明                                         |
| --------------------------- | -------------------------------------------- |
| [ImageSource](#imagesource) | 返回ImageSource类实例,失败时返回undefined。 |
X
xiaok 已提交
940 941 942 943

**示例:**

```js
Z
zhang-xiaobo1997 已提交
944 945
let path = this.context.getApplicationContext().fileDirs + "test.jpg";
const imageSourceApi = image.createImageSource(path);
X
xiaok 已提交
946 947
```

X
xu-rui-w 已提交
948 949 950 951 952 953 954 955 956 957 958 959
## image.createImageSource<sup>9+</sup>

createImageSource(uri: string, options: SourceOptions): ImageSource

通过传入的uri创建图片源实例。

**系统能力:** SystemCapability.Multimedia.Image.ImageSource

**参数:**

| 参数名  | 类型                            | 必填 | 说明                                |
| ------- | ------------------------------- | ---- | ----------------------------------- |
X
xu-rui-w 已提交
960
| uri     | string                          | 是   | 图片路径,当前仅支持应用沙箱路径。</br>当前支持格式有:.jpg .png .gif .bmp .webp RAW。 |
961
| options | [SourceOptions](#sourceoptions9) | 是   | 图片属性,包括图片序号与默认属性值。|
X
xu-rui-w 已提交
962 963 964 965 966 967 968 969 970 971

**返回值:**

| 类型                        | 说明                                         |
| --------------------------- | -------------------------------------------- |
| [ImageSource](#imagesource) | 返回ImageSource类实例,失败时返回undefined。 |

**示例:**

```js
X
xu-rui-w 已提交
972 973
var sourceOptions = { sourceDensity: 120 };
let imageSource = image.createImageSource('test.png', sourceOptions);
X
xu-rui-w 已提交
974 975
```

Z
zengyawen 已提交
976
## image.createImageSource<sup>7+</sup>
X
xiaok 已提交
977

Z
zengyawen 已提交
978
createImageSource(fd: number): ImageSource
X
xiaok 已提交
979

Z
zengyawen 已提交
980
通过传入文件描述符来创建图片源实例。
X
xiaok 已提交
981

X
xu-rui-w 已提交
982
**系统能力:** SystemCapability.Multimedia.Image.ImageSource
X
xiaok 已提交
983 984 985

**参数:**

X
xu-rui-w 已提交
986 987 988
| 参数名 | 类型   | 必填 | 说明          |
| ------ | ------ | ---- | ------------- |
| fd     | number | 是   | 文件描述符fd。|
X
xiaok 已提交
989 990 991

**返回值:**

992 993 994
| 类型                        | 说明                                         |
| --------------------------- | -------------------------------------------- |
| [ImageSource](#imagesource) | 返回ImageSource类实例,失败时返回undefined。 |
X
xiaok 已提交
995 996 997 998

**示例:**

```js
X
xu-rui-w 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
const imageSourceApi = image.createImageSource(0);
```

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

createImageSource(fd: number, options: SourceOptions): ImageSource

通过传入文件描述符来创建图片源实例。

**系统能力:** SystemCapability.Multimedia.Image.ImageSource

**参数:**

| 参数名  | 类型                            | 必填 | 说明                                |
| ------- | ------------------------------- | ---- | ----------------------------------- |
| fd      | number                          | 是   | 文件描述符fd。                      |
1015
| options | [SourceOptions](#sourceoptions9) | 是   | 图片属性,包括图片序号与默认属性值。|
X
xu-rui-w 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025

**返回值:**

| 类型                        | 说明                                         |
| --------------------------- | -------------------------------------------- |
| [ImageSource](#imagesource) | 返回ImageSource类实例,失败时返回undefined。 |

**示例:**

```js
F
fengzewu 已提交
1026 1027
var sourceOptions = { sourceDensity: 120 };
const imageSourceApi = image.createImageSource(0, sourceOptions);
X
xu-rui-w 已提交
1028 1029 1030
```

## image.createImageSource<sup>9+</sup>
X
xiaok 已提交
1031

X
xu-rui-w 已提交
1032
createImageSource(buf: ArrayBuffer): ImageSource
X
xiaok 已提交
1033

X
xu-rui-w 已提交
1034
通过缓冲区创建图片源实例。
X
xiaok 已提交
1035

X
xu-rui-w 已提交
1036
**系统能力:** SystemCapability.Multimedia.Image.ImageSource
X
xiaok 已提交
1037 1038 1039

**参数:**

X
xu-rui-w 已提交
1040 1041 1042
| 参数名 | 类型        | 必填 | 说明             |
| ------ | ----------- | ---- | ---------------- |
| buf    | ArrayBuffer | 是   | 图像缓冲区数组。 |
X
xiaok 已提交
1043 1044 1045 1046

**示例:**

```js
X
xu-rui-w 已提交
1047
const buf = new ArrayBuffer(96);
X
xu-rui-w 已提交
1048
const imageSourceApi = image.createImageSource(buf);
X
xiaok 已提交
1049 1050
```

X
xu-rui-w 已提交
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
## image.createImageSource<sup>9+</sup>

createImageSource(buf: ArrayBuffer, options: SourceOptions): ImageSource

通过缓冲区创建图片源实例。

**系统能力:** SystemCapability.Multimedia.Image.ImageSource

**参数:**

| 参数名 | 类型                             | 必填 | 说明                                 |
| ------ | -------------------------------- | ---- | ------------------------------------ |
| buf    | ArrayBuffer                      | 是   | 图像缓冲区数组。                     |
1064
| options | [SourceOptions](#sourceoptions9) | 是   | 图片属性,包括图片序号与默认属性值。 |
X
xu-rui-w 已提交
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074

**返回值:**

| 类型                        | 说明                                         |
| --------------------------- | -------------------------------------------- |
| [ImageSource](#imagesource) | 返回ImageSource类实例,失败时返回undefined。 |

**示例:**

```js
X
xu-rui-w 已提交
1075 1076
const data = new ArrayBuffer(112);
const imageSourceApi = image.createImageSource(data);
X
xu-rui-w 已提交
1077 1078
```

X
xu-rui-w 已提交
1079
## image.createIncrementalSource<sup>9+</sup>
X
xu-rui-w 已提交
1080

X
xu-rui-w 已提交
1081
createIncrementalSource(buf: ArrayBuffer): ImageSource
X
xu-rui-w 已提交
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101

通过缓冲区以增量的方式创建图片源实例。

**系统能力:** SystemCapability.Multimedia.Image.ImageSource

**参数:**

| 参数名  | 类型        | 必填 | 说明      |
| ------- | ------------| ---- | ----------|
| buf     | ArrayBuffer | 是   | 增量数据。|

**返回值:**

| 类型                        | 说明                              |
| --------------------------- | --------------------------------- |
| [ImageSource](#imagesource) | 返回图片源,失败时返回undefined。 |

**示例:**

```js
X
xu-rui-w 已提交
1102
const buf = new ArrayBuffer(96);
F
fengzewu 已提交
1103
const imageSourceIncrementalSApi = image.createIncrementalSource(buf);
X
xu-rui-w 已提交
1104 1105
```

X
xu-rui-w 已提交
1106
## image.createIncrementalSource<sup>9+</sup>
X
xu-rui-w 已提交
1107

X
xu-rui-w 已提交
1108
createIncrementalSource(buf: ArrayBuffer, options?: SourceOptions): ImageSource
X
xu-rui-w 已提交
1109

X
xu-rui-w 已提交
1110
通过缓冲区以增量的方式创建图片源实例。
X
xu-rui-w 已提交
1111

X
xu-rui-w 已提交
1112
**系统能力:** SystemCapability.Multimedia.Image.ImageSource
X
xu-rui-w 已提交
1113 1114 1115 1116 1117

**参数:**

| 参数名  | 类型                            | 必填 | 说明                                 |
| ------- | ------------------------------- | ---- | ------------------------------------ |
X
xu-rui-w 已提交
1118
| buf     | ArrayBuffer                     | 是   | 增量数据。                           |
1119
| options | [SourceOptions](#sourceoptions9) | 否   | 图片属性,包括图片序号与默认属性值。 |
X
xu-rui-w 已提交
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130

**返回值:**

| 类型                        | 说明                              |
| --------------------------- | --------------------------------- |
| [ImageSource](#imagesource) | 返回图片源,失败时返回undefined。 |

**示例:**

```js
const buf = new ArrayBuffer(96);
F
fengzewu 已提交
1131
const imageSourceIncrementalSApi = image.createIncrementalSource(buf);
X
xiaok 已提交
1132 1133
```

Z
zengyawen 已提交
1134
## ImageSource
X
xiaok 已提交
1135

Z
zengyawen 已提交
1136 1137 1138 1139
图片源类,用于获取图片相关信息。在调用ImageSource的方法前,需要先通过createImageSource构建一个ImageSource实例。

### 属性

X
xu-rui-w 已提交
1140
**系统能力:** SystemCapability.Multimedia.Image.ImageSource
Z
zhang-xiaobo1997 已提交
1141

Z
zengyawen 已提交
1142 1143
| 名称             | 类型           | 可读 | 可写 | 说明                                                         |
| ---------------- | -------------- | ---- | ---- | ------------------------------------------------------------ |
X
xu-rui-w 已提交
1144
| supportedFormats | Array\<string> | 是   | 否   | 支持的图片格式,包括:png,jpeg,bmp,gif,webp,RAW。 |
Z
zengyawen 已提交
1145 1146 1147 1148

### getImageInfo

getImageInfo(index: number, callback: AsyncCallback\<ImageInfo>): void
X
xiaok 已提交
1149 1150 1151

获取指定序号的图片信息,使用callback形式返回图片信息。

X
xu-rui-w 已提交
1152
**系统能力:** SystemCapability.Multimedia.Image.ImageSource
X
xiaok 已提交
1153 1154 1155

**参数:**

Z
zengyawen 已提交
1156 1157 1158 1159
| 参数名   | 类型                                   | 必填 | 说明                                     |
| -------- | -------------------------------------- | ---- | ---------------------------------------- |
| index    | number                                 | 是   | 创建图片源时的序号。                     |
| callback | AsyncCallback<[ImageInfo](#imageinfo)> | 是   | 获取图片信息回调,异步返回图片信息对象。 |
X
xiaok 已提交
1160 1161 1162 1163

**示例:**

```js
X
xu-rui-w 已提交
1164 1165 1166 1167 1168 1169 1170
imageSourceApi.getImageInfo(0,(error, imageInfo) => { 
    if(error) {
        console.log('getImageInfo failed.');
    } else {
        console.log('getImageInfo succeeded.');
    }
})
X
xiaok 已提交
1171 1172
```

Z
zengyawen 已提交
1173
### getImageInfo
X
xiaok 已提交
1174

Z
zengyawen 已提交
1175
getImageInfo(callback: AsyncCallback\<ImageInfo>): void
X
xiaok 已提交
1176

Z
zengyawen 已提交
1177
获取图片信息,使用callback形式返回图片信息。
X
xiaok 已提交
1178

X
xu-rui-w 已提交
1179
**系统能力:** SystemCapability.Multimedia.Image.ImageSource
X
xiaok 已提交
1180 1181 1182 1183 1184

**参数:**

| 名称     | 类型                                   | 必填 | 说明                                     |
| -------- | -------------------------------------- | ---- | ---------------------------------------- |
Z
zengyawen 已提交
1185
| callback | AsyncCallback<[ImageInfo](#imageinfo)> | 是   | 获取图片信息回调,异步返回图片信息对象。 |
X
xiaok 已提交
1186 1187 1188 1189

**示例:**

```js
X
xu-rui-w 已提交
1190
imageSourceApi.getImageInfo(imageInfo => { 
X
xu-rui-w 已提交
1191
    console.log('Succeeded in obtaining the image information.');
X
xu-rui-w 已提交
1192
})
X
xiaok 已提交
1193 1194
```

Z
zengyawen 已提交
1195
### getImageInfo
X
xiaok 已提交
1196

Z
zengyawen 已提交
1197
getImageInfo(index?: number): Promise\<ImageInfo>
X
xiaok 已提交
1198 1199 1200

获取图片信息,使用Promise形式返回图片信息。

X
xu-rui-w 已提交
1201
**系统能力:** SystemCapability.Multimedia.Image.ImageSource
X
xiaok 已提交
1202 1203 1204

**参数:**

Z
zengyawen 已提交
1205 1206 1207
| 名称  | 类型   | 必填 | 说明                                  |
| ----- | ------ | ---- | ------------------------------------- |
| index | number | 否   | 创建图片源时的序号,不选择时默认为0。 |
X
xiaok 已提交
1208 1209 1210

**返回值:**

Z
zengyawen 已提交
1211 1212 1213
| 类型                             | 说明                   |
| -------------------------------- | ---------------------- |
| Promise<[ImageInfo](#imageinfo)> | 返回获取到的图片信息。 |
X
xiaok 已提交
1214 1215 1216 1217 1218

**示例:**

```js
imageSourceApi.getImageInfo(0)
X
xu-rui-w 已提交
1219
    .then(imageInfo => {
X
xu-rui-w 已提交
1220
		console.log('Succeeded in obtaining the image information.');
X
xu-rui-w 已提交
1221
	}).catch(error => {
X
xu-rui-w 已提交
1222
		console.log('Failed to obtain the image information.');
X
xu-rui-w 已提交
1223
	})
X
xiaok 已提交
1224 1225
```

Z
zengyawen 已提交
1226
### getImageProperty<sup>7+</sup>
X
xiaok 已提交
1227

Z
zengyawen 已提交
1228
getImageProperty(key:string, options?: GetImagePropertyOptions): Promise\<string>
X
xiaok 已提交
1229

Z
zengyawen 已提交
1230
获取图片中给定索引处图像的指定属性键的值,用Promise形式返回结果。
X
xiaok 已提交
1231

X
xu-rui-w 已提交
1232
**系统能力:** SystemCapability.Multimedia.Image.ImageSource
X
xiaok 已提交
1233 1234 1235

 **参数:**

Z
zengyawen 已提交
1236 1237 1238 1239
| 名称    | 类型                                                 | 必填 | 说明                                 |
| ------- | ---------------------------------------------------- | ---- | ------------------------------------ |
| key     | string                                               | 是   | 图片属性名。                         |
| options | [GetImagePropertyOptions](#getimagepropertyoptions7) | 否   | 图片属性,包括图片序号与默认属性值。 |
X
xiaok 已提交
1240 1241 1242

**返回值:**

X
xu-rui-w 已提交
1243 1244
| 类型             | 说明                                                              |
| ---------------- | ----------------------------------------------------------------- |
Z
zengyawen 已提交
1245
| Promise\<string> | Promise实例,用于异步获取图片属性值,如获取失败则返回属性默认值。 |
X
xiaok 已提交
1246 1247 1248 1249

**示例:**

```js
1250
imageSourceApi.getImageProperty("BitsPerSample")
X
xu-rui-w 已提交
1251
    .then(data => {
X
xu-rui-w 已提交
1252
		console.log('Succeeded in getting the value of the specified attribute key of the image.');
X
xu-rui-w 已提交
1253
	})
X
xiaok 已提交
1254 1255
```

Z
zengyawen 已提交
1256
### getImageProperty<sup>7+</sup>
X
xiaok 已提交
1257

Z
zengyawen 已提交
1258
getImageProperty(key:string, callback: AsyncCallback\<string>): void
X
xiaok 已提交
1259

Z
zengyawen 已提交
1260
获取图片中给定索引处图像的指定属性键的值,用callback形式返回结果。
X
xiaok 已提交
1261

X
xu-rui-w 已提交
1262
**系统能力:** SystemCapability.Multimedia.Image.ImageSource
X
xiaok 已提交
1263 1264 1265

 **参数:**

Z
zengyawen 已提交
1266 1267 1268 1269
| 参数名   | 类型                   | 必填 | 说明                                                         |
| -------- | ---------------------- | ---- | ------------------------------------------------------------ |
| key      | string                 | 是   | 图片属性名。                                                 |
| callback | AsyncCallback\<string> | 是   | 获取图片属性回调,返回图片属性值,如获取失败则返回属性默认值。 |
X
xiaok 已提交
1270 1271 1272 1273

**示例:**

```js
X
xu-rui-w 已提交
1274 1275
imageSourceApi.getImageProperty("BitsPerSample",(error,data) => { 
    if(error) {
X
xu-rui-w 已提交
1276
        console.log('Failed to get the value of the specified attribute key of the image.');
X
xu-rui-w 已提交
1277
    } else {
X
xu-rui-w 已提交
1278
        console.log('Succeeded in getting the value of the specified attribute key of the image.');
X
xu-rui-w 已提交
1279 1280
    }
})
X
xiaok 已提交
1281 1282
```

Z
zengyawen 已提交
1283
### getImageProperty<sup>7+</sup>
X
xiaok 已提交
1284

Z
zengyawen 已提交
1285
getImageProperty(key:string, options: GetImagePropertyOptions, callback: AsyncCallback\<string>): void
X
xiaok 已提交
1286 1287 1288

获取图片指定属性键的值,callback形式返回结果。

X
xu-rui-w 已提交
1289
**系统能力:** SystemCapability.Multimedia.Image.ImageSource
X
xiaok 已提交
1290 1291 1292

**参数:**

X
xu-rui-w 已提交
1293 1294 1295 1296 1297
| 参数名   | 类型                                                 | 必填 | 说明                                                          |
| -------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------- |
| key      | string                                               | 是   | 图片属性名。                                                  |
| options  | [GetImagePropertyOptions](#getimagepropertyoptions7) | 是   | 图片属性,包括图片序号与默认属性值。                          |
| callback | AsyncCallback\<string>                               | 是   | 获取图片属性回调,返回图片属性值,如获取失败则返回属性默认值。|
X
xiaok 已提交
1298 1299 1300 1301

**示例:**

```js
F
fengzewu 已提交
1302
let property = { index: 0, defaultValue: '9999' }
X
xu-rui-w 已提交
1303
imageSourceApi.getImageProperty("BitsPerSample",property,(error,data) => { 
X
xu-rui-w 已提交
1304
    if(error) {
X
xu-rui-w 已提交
1305
        console.log('Failed to get the value of the specified attribute key of the image.');
X
xu-rui-w 已提交
1306
    } else {
X
xu-rui-w 已提交
1307
        console.log('Succeeded in getting the value of the specified attribute key of the image.');
X
xu-rui-w 已提交
1308 1309
    }
})
X
xiaok 已提交
1310 1311
```

X
xu-rui-w 已提交
1312
### modifyImageProperty<sup>9+</sup>
X
xu-rui-w 已提交
1313

X
xu-rui-w 已提交
1314
modifyImageProperty(key: string, value: string): Promise\<void>
X
xu-rui-w 已提交
1315

X
xu-rui-w 已提交
1316
通过指定的键修改图片属性的值,使用Promise形式返回结果。
X
xu-rui-w 已提交
1317

X
xu-rui-w 已提交
1318
**系统能力:** SystemCapability.Multimedia.Image.ImageSource
X
xu-rui-w 已提交
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328

**参数:**

| 参数名  | 类型   | 必填 | 说明         |
| ------- | ------ | ---- | ------------ |
| key     | string | 是   | 图片属性名。 |
| value   | string | 是   | 属性值。     |

**返回值:**

X
xu-rui-w 已提交
1329 1330 1331
| 类型           | 说明                        |
| -------------- | --------------------------- |
| Promise\<void> | Promise实例,异步返回结果。 |
X
xu-rui-w 已提交
1332 1333 1334 1335

**示例:**

```js
F
fengzewu 已提交
1336 1337 1338 1339
imageSourceApi.modifyImageProperty("ImageWidth", "120").then(() => {
    const w = imageSourceApi.getImageProperty("ImageWidth")
    console.info('w', w);
})
X
xu-rui-w 已提交
1340 1341
```

X
xu-rui-w 已提交
1342
### modifyImageProperty<sup>9+</sup>
X
xu-rui-w 已提交
1343

X
xu-rui-w 已提交
1344
modifyImageProperty(key: string, value: string, callback: AsyncCallback\<void>): void
X
xu-rui-w 已提交
1345

X
xu-rui-w 已提交
1346
通过指定的键修改图片属性的值,callback形式返回结果。
X
xu-rui-w 已提交
1347

X
xu-rui-w 已提交
1348
**系统能力:** SystemCapability.Multimedia.Image.ImageSource
X
xu-rui-w 已提交
1349 1350 1351 1352 1353 1354 1355

**参数:**

| 参数名   | 类型                | 必填 | 说明                           |
| -------- | ------------------- | ---- | ------------------------------ |
| key      | string              | 是   | 图片属性名。                   |
| value    | string              | 是   | 属性值。                       |
1356
| callback | AsyncCallback\<void> | 是   | 修改属性值,callback返回结果。 |
X
xu-rui-w 已提交
1357 1358 1359 1360

**示例:**

```js
X
xu-rui-w 已提交
1361
imageSourceApi.modifyImageProperty("ImageWidth", "120",() => {})
X
xu-rui-w 已提交
1362 1363
```

X
xu-rui-w 已提交
1364
### updateData<sup>9+</sup>
X
xu-rui-w 已提交
1365

X
xu-rui-w 已提交
1366
updateData(buf: ArrayBuffer, isFinished: boolean, value: number, length: number): Promise\<void>
X
xu-rui-w 已提交
1367

X
xu-rui-w 已提交
1368
更新增量数据,使用Promise形式返回结果。
X
xu-rui-w 已提交
1369

X
xu-rui-w 已提交
1370
**系统能力:** SystemCapability.Multimedia.Image.ImageSource
X
xu-rui-w 已提交
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382

**参数:**

| 名称       | 类型        | 必填 | 说明         |
| ---------- | ----------- | ---- | ------------ |
| buf        | ArrayBuffer | 是   | 增量数据。   |
| isFinished | boolean     | 是   | 是否更新完。 |
| value      | number      | 否   | 偏移量。     |
| length     | number      | 否   | 数组长。     |

**返回值:**

X
xu-rui-w 已提交
1383 1384 1385
| 类型           | 说明                       |
| -------------- | -------------------------- |
| Promise\<void> | Promise实例,异步返回结果。|
X
xu-rui-w 已提交
1386 1387 1388 1389 1390

**示例:**

```js
const array = new ArrayBuffer(100);
F
fengzewu 已提交
1391 1392 1393
imageSourceApi.updateData(array, false, 0, 10).then(data => {
    console.info('Succeeded in updating data.');
})
X
xu-rui-w 已提交
1394 1395 1396
```


X
xu-rui-w 已提交
1397
### updateData<sup>9+</sup>
X
xu-rui-w 已提交
1398

X
xu-rui-w 已提交
1399
updateData(buf: ArrayBuffer, isFinished: boolean, value: number, length: number, callback: AsyncCallback\<void>): void
X
xu-rui-w 已提交
1400

X
xu-rui-w 已提交
1401
更新增量数据,callback形式返回结果。
X
xu-rui-w 已提交
1402

X
xu-rui-w 已提交
1403
**系统能力:** SystemCapability.Multimedia.Image.ImageSource
X
xu-rui-w 已提交
1404 1405 1406 1407 1408 1409 1410 1411 1412

**参数:**

| 名称       | 类型                | 必填 | 说明                 |
| ---------- | ------------------- | ---- | -------------------- |
| buf        | ArrayBuffer         | 是   | 增量数据。           |
| isFinished | boolean             | 是   | 是否更新完。         |
| value      | number              | 否   | 偏移量。             |
| length     | number              | 否   | 数组长。             |
1413
| callback   | AsyncCallback\<void> | 是   | 回调表示成功或失败。 |
X
xu-rui-w 已提交
1414 1415 1416 1417 1418

**示例:**

```js
const array = new ArrayBuffer(100);
F
fengzewu 已提交
1419 1420 1421 1422 1423
imageSourceApi.updateData(array, false, 0, 10,(error,data )=> {
    if(data !== undefined){
        console.info('Succeeded in updating data.');     
    }
})
X
xu-rui-w 已提交
1424 1425
```

Z
zengyawen 已提交
1426
### createPixelMap<sup>7+</sup>
X
xiaok 已提交
1427

Z
zengyawen 已提交
1428
createPixelMap(options?: DecodingOptions): Promise\<PixelMap>
X
xiaok 已提交
1429

1430
通过图片解码参数创建PixelMap对象。
X
xiaok 已提交
1431

X
xu-rui-w 已提交
1432
**系统能力:** SystemCapability.Multimedia.Image.ImageSource
X
xiaok 已提交
1433 1434 1435

**参数:**

Z
zengyawen 已提交
1436 1437 1438 1439 1440 1441 1442 1443 1444
| 名称    | 类型                                 | 必填 | 说明       |
| ------- | ------------------------------------ | ---- | ---------- |
| options | [DecodingOptions](#decodingoptions7) | 否   | 解码参数。 |

**返回值:**

| 类型                             | 说明                  |
| -------------------------------- | --------------------- |
| Promise\<[PixelMap](#pixelmap7)> | 异步返回Promise对象。 |
X
xiaok 已提交
1445 1446 1447 1448

**示例:**

```js
X
xu-rui-w 已提交
1449
imageSourceApi.createPixelMap().then(pixelmap => {
X
xu-rui-w 已提交
1450
    console.log('Succeeded in creating pixelmap object through image decoding parameters.');
X
xu-rui-w 已提交
1451
}).catch(error => {
X
xu-rui-w 已提交
1452
    console.log('Failed to create pixelmap object through image decoding parameters.');
X
xu-rui-w 已提交
1453
})
X
xiaok 已提交
1454 1455
```

Z
zengyawen 已提交
1456
### createPixelMap<sup>7+</sup>
X
xiaok 已提交
1457

Z
zengyawen 已提交
1458
createPixelMap(callback: AsyncCallback\<PixelMap>): void
X
xiaok 已提交
1459

1460
通过默认参数创建PixelMap对象,使用callback形式返回结果。
X
xiaok 已提交
1461

X
xu-rui-w 已提交
1462
**系统能力:** SystemCapability.Multimedia.Image.ImageSource
X
xiaok 已提交
1463 1464 1465

**参数:**

Z
zengyawen 已提交
1466 1467 1468
| 名称     | 类型                                  | 必填 | 说明                       |
| -------- | ------------------------------------- | ---- | -------------------------- |
| callback | AsyncCallback<[PixelMap](#pixelmap7)> | 是   | 通过回调返回PixelMap对象。 |
X
xiaok 已提交
1469 1470 1471 1472

**示例:**

```js
X
xu-rui-w 已提交
1473
imageSourceApi.createPixelMap((err, pixelmap) => {
1474
                    console.info('Succeeded in creating pixelmap object.');
X
xu-rui-w 已提交
1475
                })
X
xiaok 已提交
1476 1477
```

Z
zengyawen 已提交
1478
### createPixelMap<sup>7+</sup>
X
xiaok 已提交
1479

Z
zengyawen 已提交
1480
createPixelMap(options: DecodingOptions, callback: AsyncCallback\<PixelMap>): void
X
xiaok 已提交
1481

Z
zengyawen 已提交
1482
通过图片解码参数创建PixelMap对象。
X
xiaok 已提交
1483

X
xu-rui-w 已提交
1484
**系统能力:** SystemCapability.Multimedia.Image.ImageSource
X
xiaok 已提交
1485 1486 1487

**参数:**

Z
zengyawen 已提交
1488 1489
| 名称     | 类型                                  | 必填 | 说明                       |
| -------- | ------------------------------------- | ---- | -------------------------- |
1490
| options  | [DecodingOptions](#decodingoptions7)  | 是   | 解码参数。                 |
Z
zengyawen 已提交
1491
| callback | AsyncCallback<[PixelMap](#pixelmap7)> | 是   | 通过回调返回PixelMap对象。 |
X
xiaok 已提交
1492 1493 1494 1495

**示例:**

```js
F
fengzewu 已提交
1496 1497 1498 1499 1500 1501 1502 1503 1504
let decodingOptions = {
    sampleSize: 1,
    editable: true,
    desiredSize: { width: 1, height: 2 },
    rotate: 10,
    desiredPixelFormat: 3,
    desiredRegion: { size: { height: 1, width: 2 }, x: 0, y: 0 },
    index: 0
};
X
xu-rui-w 已提交
1505
imageSourceApi.createPixelMap(decodingOptions, pixelmap => { 
X
xu-rui-w 已提交
1506 1507
    console.log('Succeeded in creating pixelmap object.');
})
X
xiaok 已提交
1508 1509
```

Z
zengyawen 已提交
1510
### release
X
xiaok 已提交
1511

Z
zengyawen 已提交
1512
release(callback: AsyncCallback\<void>): void
X
xiaok 已提交
1513

Z
zengyawen 已提交
1514
释放图片源实例,使用callback形式返回结果。
X
xiaok 已提交
1515

X
xu-rui-w 已提交
1516
**系统能力:** SystemCapability.Multimedia.Image.ImageSource
X
xiaok 已提交
1517 1518 1519

**参数:**

Z
zengyawen 已提交
1520 1521 1522
| 名称     | 类型                 | 必填 | 说明                               |
| -------- | -------------------- | ---- | ---------------------------------- |
| callback | AsyncCallback\<void> | 是   | 资源释放回调,失败时返回错误信息。 |
X
xiaok 已提交
1523 1524 1525 1526

**示例:**

```js
X
xu-rui-w 已提交
1527 1528 1529
imageSourceApi.release(() => { 
    console.log('release succeeded.');
})
X
xiaok 已提交
1530 1531
```

Z
zengyawen 已提交
1532
### release
X
xiaok 已提交
1533

Z
zengyawen 已提交
1534
release(): Promise\<void>
X
xiaok 已提交
1535

Z
zengyawen 已提交
1536
释放图片源实例,使用Promise形式返回结果。
X
xiaok 已提交
1537

X
xu-rui-w 已提交
1538
**系统能力:** SystemCapability.Multimedia.Image.ImageSource
X
xiaok 已提交
1539

Z
zengyawen 已提交
1540 1541 1542 1543 1544 1545
**返回值:**

| 类型           | 说明                        |
| -------------- | --------------------------- |
| Promise\<void> | Promise实例,异步返回结果。 |

X
xiaok 已提交
1546 1547 1548
**示例:**

```js
X
xu-rui-w 已提交
1549
imageSourceApi.release().then(()=>{
X
xu-rui-w 已提交
1550
    console.log('Succeeded in releasing the image source instance.');
X
xu-rui-w 已提交
1551
}).catch(error => {
X
xu-rui-w 已提交
1552
    console.log('Failed to release the image source instance.');
X
xu-rui-w 已提交
1553
})
X
xiaok 已提交
1554 1555
```

Z
zengyawen 已提交
1556
## image.createImagePacker
X
xiaok 已提交
1557 1558 1559

createImagePacker(): ImagePacker

Z
zengyawen 已提交
1560
创建ImagePacker实例。
X
xiaok 已提交
1561

X
xu-rui-w 已提交
1562
**系统能力:** SystemCapability.Multimedia.Image.ImagePacker
X
xiaok 已提交
1563 1564 1565

**返回值:**

Z
zengyawen 已提交
1566 1567 1568
| 类型                        | 说明                  |
| --------------------------- | --------------------- |
| [ImagePacker](#imagepacker) | 返回ImagePacker实例。 |
X
xiaok 已提交
1569 1570 1571 1572 1573 1574 1575

**示例:**

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

Z
zengyawen 已提交
1576 1577
## ImagePacker

X
xu-rui-w 已提交
1578
图片打包器类,用于图片压缩和打包。在调用ImagePacker的方法前,需要先通过createImagePacker构建一个ImagePacker实例,当前支持格式有:jpeg webp。
X
xiaok 已提交
1579

Z
zengyawen 已提交
1580 1581
### 属性

X
xu-rui-w 已提交
1582
**系统能力:** SystemCapability.Multimedia.Image.ImagePacker
Z
zhang-xiaobo1997 已提交
1583 1584 1585 1586

| 名称             | 类型           | 可读 | 可写 | 说明                       |
| ---------------- | -------------- | ---- | ---- | -------------------------- |
| supportedFormats | Array\<string> | 是   | 否   | 图片打包支持的格式,jpeg。 |
Z
zengyawen 已提交
1587 1588 1589

### packing

1590
packing(source: ImageSource, option: PackingOption, callback: AsyncCallback\<ArrayBuffer>): void
X
xiaok 已提交
1591 1592 1593

图片压缩或重新打包,使用callback形式返回结果。

X
xu-rui-w 已提交
1594
**系统能力:** SystemCapability.Multimedia.Image.ImagePacker
X
xiaok 已提交
1595 1596 1597

**参数:**

Z
zengyawen 已提交
1598 1599 1600
| 参数名   | 类型                               | 必填 | 说明                               |
| -------- | ---------------------------------- | ---- | ---------------------------------- |
| source   | [ImageSource](#imagesource)        | 是   | 打包的图片源。                     |
X
xu-rui-w 已提交
1601
| option   | [PackingOption](#packingoption)    | 是   | 设置打包参数。                      |
1602
| callback | AsyncCallback\<ArrayBuffer>        | 是   | 获取图片打包回调,返回打包后数据。 |
X
xiaok 已提交
1603 1604 1605 1606

**示例:**

```js
F
fengzewu 已提交
1607
const imageSourceApi = image.createImageSource(0);
X
xu-rui-w 已提交
1608
let packOpts = { format:"image/jpeg", quality:98 };
X
xu-rui-w 已提交
1609
imagePackerApi.packing(imageSourceApi, packOpts, data => {})
X
xiaok 已提交
1610 1611
```

Z
zengyawen 已提交
1612
### packing
X
xiaok 已提交
1613

1614
packing(source: ImageSource, option: PackingOption): Promise\<ArrayBuffer>
X
xiaok 已提交
1615

Z
zengyawen 已提交
1616
图片压缩或重新打包,使用Promise形式返回结果。
X
xiaok 已提交
1617

X
xu-rui-w 已提交
1618
**系统能力:** SystemCapability.Multimedia.Image.ImagePacker
X
xiaok 已提交
1619 1620 1621 1622 1623

**参数:**

| 参数名 | 类型                            | 必填 | 说明           |
| ------ | ------------------------------- | ---- | -------------- |
Z
zengyawen 已提交
1624 1625
| source | [ImageSource](#imagesource)     | 是   | 打包的图片源。 |
| option | [PackingOption](#packingoption) | 是   | 设置打包参数。 |
X
xiaok 已提交
1626 1627 1628

**返回值:**

Z
zengyawen 已提交
1629
| 类型                         | 说明                                          |
X
xu-rui-w 已提交
1630 1631
| ---------------------------- | --------------------------------------------- |
| Promise\<ArrayBuffer>        | Promise实例,用于异步获取压缩或打包后的数据。 |
X
xiaok 已提交
1632 1633 1634 1635

**示例:**

```js
F
fengzewu 已提交
1636
const imageSourceApi = image.createImageSource(0);
X
xu-rui-w 已提交
1637
let packOpts = { format:"image/jpeg", quality:98 }
X
xu-rui-w 已提交
1638
imagePackerApi.packing(imageSourceApi, packOpts)
X
xu-rui-w 已提交
1639 1640 1641 1642 1643
    .then( data => {
        console.log('packing succeeded.');
	}).catch(error => {
	    console.log('packing failed.');
	})
1644 1645
```

1646
### packing<sup>8+</sup>
1647

Z
zengyawen 已提交
1648
packing(source: PixelMap, option: PackingOption, callback: AsyncCallback\<ArrayBuffer>): void
1649 1650 1651

图片压缩或重新打包,使用callback形式返回结果。

X
xu-rui-w 已提交
1652
**系统能力:** SystemCapability.Multimedia.Image.ImagePacker
1653 1654 1655 1656 1657 1658 1659

**参数:**

| 参数名   | 类型                            | 必填 | 说明                               |
| -------- | ------------------------------- | ---- | ---------------------------------- |
| source   | [PixelMap](#pixelmap)           | 是   | 打包的PixelMap资源。               |
| option   | [PackingOption](#packingoption) | 是   | 设置打包参数。                     |
Z
zengyawen 已提交
1660
| callback | AsyncCallback\<ArrayBuffer>     | 是   | 获取图片打包回调,返回打包后数据。 |
1661 1662 1663 1664

**示例:**

```js
F
fengzewu 已提交
1665 1666 1667 1668 1669 1670 1671 1672
const color = new ArrayBuffer(96);
let bufferArr = new Uint8Array(color);
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts).then((pixelmap) => {
    let packOpts = { format:"image/jpeg", quality:98 }
    imagePackerApi.packing(pixelMapApi, packOpts, data => { 
        console.log('Succeeded in packing the image.');
    })
X
xu-rui-w 已提交
1673
})
1674 1675
```

1676
### packing<sup>8+</sup>
1677

1678
packing(source: PixelMap, option: PackingOption): Promise\<ArrayBuffer>
1679 1680 1681

图片压缩或重新打包,使用Promise形式返回结果。

X
xu-rui-w 已提交
1682
**系统能力:** SystemCapability.Multimedia.Image.ImagePacker
1683 1684 1685

**参数:**

Z
zhang-xiaobo1997 已提交
1686 1687
| 参数名 | 类型                            | 必填 | 说明               |
| ------ | ------------------------------- | ---- | ------------------ |
1688
| source | [PixelMap](#pixelmap)           | 是   | 打包的PixelMap源。 |
Z
zhang-xiaobo1997 已提交
1689
| option | [PackingOption](#packingoption) | 是   | 设置打包参数。     |
1690 1691 1692

**返回值:**

X
xu-rui-w 已提交
1693 1694 1695
| 类型                  | 说明                                         |
| --------------------- | -------------------------------------------- |
| Promise\<ArrayBuffer> | Promise实例,用于异步获取压缩或打包后的数据。|
1696 1697 1698 1699

**示例:**

```js
F
fengzewu 已提交
1700 1701 1702 1703 1704 1705 1706
const color = new ArrayBuffer(96);
let bufferArr = new Uint8Array(color);
let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
image.createPixelMap(color, opts).then((pixelmap) => {
    let packOpts = { format:"image/jpeg", quality:98 }
    imagePackerApi.packing(pixelMapApi, packOpts)
        .then( data => {
F
fengzewu 已提交
1707 1708 1709 1710
            console.log('Succeeded in packing the image.');
        }).catch(error => {
            console.log('Failed to pack the image..');
        })
F
fengzewu 已提交
1711
})
X
xiaok 已提交
1712 1713
```

Z
zengyawen 已提交
1714
### release
X
xiaok 已提交
1715

Z
zengyawen 已提交
1716
release(callback: AsyncCallback\<void>): void
X
xiaok 已提交
1717

Z
zengyawen 已提交
1718
释放图片打包实例,使用callback形式返回结果。
X
xiaok 已提交
1719

X
xu-rui-w 已提交
1720
**系统能力:** SystemCapability.Multimedia.Image.ImagePacker
X
xiaok 已提交
1721 1722 1723

**参数:**

Z
zengyawen 已提交
1724 1725 1726
| 参数名   | 类型                 | 必填 | 说明                           |
| -------- | -------------------- | ---- | ------------------------------ |
| callback | AsyncCallback\<void> | 是   | 释放回调,失败时返回错误信息。 |
X
xiaok 已提交
1727 1728 1729 1730

**示例:**

```js
X
xu-rui-w 已提交
1731
imagePackerApi.release(()=>{ 
X
xu-rui-w 已提交
1732
    console.log('Succeeded in releasing image packaging.');
X
xu-rui-w 已提交
1733
})
X
xiaok 已提交
1734 1735
```

Z
zengyawen 已提交
1736
### release
X
xiaok 已提交
1737

Z
zengyawen 已提交
1738
release(): Promise\<void>
X
xiaok 已提交
1739

Z
zengyawen 已提交
1740
释放图片打包实例,使用Promise形式返回释放结果。
X
xiaok 已提交
1741

X
xu-rui-w 已提交
1742
**系统能力:** SystemCapability.Multimedia.Image.ImagePacker
X
xiaok 已提交
1743

Z
zengyawen 已提交
1744 1745
**返回值:**

X
xu-rui-w 已提交
1746 1747 1748
| 类型           | 说明                                                   |
| -------------- | ------------------------------------------------------ |
| Promise\<void> | Promise实例,用于异步获取释放结果,失败时返回错误信息。|
Z
zengyawen 已提交
1749

X
xiaok 已提交
1750 1751 1752
**示例:**

```js
X
xu-rui-w 已提交
1753
imagePackerApi.release().then(()=>{
X
xu-rui-w 已提交
1754
    console.log('Succeeded in releasing image packaging.');
X
xu-rui-w 已提交
1755
}).catch((error)=>{ 
X
xu-rui-w 已提交
1756
    console.log('Failed to release image packaging.'); 
X
xu-rui-w 已提交
1757
}) 
X
xiaok 已提交
1758 1759
```

Z
zhang-xiaobo1997 已提交
1760 1761
## image.createImageReceiver<sup>9+</sup>

Z
zengyawen 已提交
1762
createImageReceiver(width: number, height: number, format: number, capacity: number): ImageReceiver
Z
zhang-xiaobo1997 已提交
1763

Z
zengyawen 已提交
1764
通过宽、高、图片格式、容量创建ImageReceiver实例。
Z
zhang-xiaobo1997 已提交
1765

Z
zengyawen 已提交
1766
**系统能力:** SystemCapability.Multimedia.Image.ImageReceiver
Z
zhang-xiaobo1997 已提交
1767 1768 1769

**参数:**

Z
zengyawen 已提交
1770 1771 1772 1773 1774 1775
| 名称     | 类型   | 必填 | 说明                   |
| -------- | ------ | ---- | ---------------------- |
| width    | number | 是   | 图像的默认宽度。       |
| height   | number | 是   | 图像的默认高度。       |
| format   | number | 是   | 图像格式。             |
| capacity | number | 是   | 同时访问的最大图像数。 |
Z
zhang-xiaobo1997 已提交
1776 1777 1778

**返回值:**

Z
zengyawen 已提交
1779 1780 1781
| 类型                             | 说明                                    |
| -------------------------------- | --------------------------------------- |
| [ImageReceiver](#imagereceiver9) | 如果操作成功,则返回ImageReceiver实例。 |
Z
zhang-xiaobo1997 已提交
1782 1783 1784 1785

**示例:**

```js
X
xu-rui-w 已提交
1786
var receiver = image.createImageReceiver(8192, 8, 4, 8);
Z
zhang-xiaobo1997 已提交
1787
```
Z
zhang-xiaobo1997 已提交
1788

Z
zhang-xiaobo1997 已提交
1789 1790
## ImageReceiver<sup>9+</sup>

Z
zengyawen 已提交
1791 1792 1793
图像接收类,用于获取组件surface id,接收最新的图片和读取下一张图片,以及释放ImageReceiver实例。

在调用以下方法前需要先创建ImageReceiver实例。
Z
zhang-xiaobo1997 已提交
1794 1795 1796

### 属性

X
xu-rui-w 已提交
1797
**系统能力:** SystemCapability.Multimedia.Image.ImageReceiver
Z
zhang-xiaobo1997 已提交
1798

X
xu-rui-w 已提交
1799 1800 1801 1802 1803
| 名称     | 类型                         | 可读 | 可写 | 说明               |
| -------- | ---------------------------- | ---- | ---- | ------------------ |
| size     | [Size](#size)                | 是   | 否   | 图片大小。         |
| capacity | number                       | 是   | 否   | 同时访问的图像数。 |
| format   | [ImageFormat](#imageformat9) | 是   | 否   | 图像格式。         |
Z
zhang-xiaobo1997 已提交
1804

Z
zhang-xiaobo1997 已提交
1805
### getReceivingSurfaceId<sup>9+</sup>
Z
zhang-xiaobo1997 已提交
1806

Z
zhang-xiaobo1997 已提交
1807
getReceivingSurfaceId(callback: AsyncCallback\<string>): void
Z
zhang-xiaobo1997 已提交
1808

Z
zengyawen 已提交
1809
用于获取一个surface id供Camera或其他组件使用。使用callback返回结果。
Z
zhang-xiaobo1997 已提交
1810

Z
zengyawen 已提交
1811
**系统能力:** SystemCapability.Multimedia.Image.ImageReceiver
Z
zhang-xiaobo1997 已提交
1812 1813 1814

**参数:**

Z
zengyawen 已提交
1815 1816 1817
| 名称     | 类型                   | 必填 | 说明                       |
| -------- | ---------------------- | ---- | -------------------------- |
| callback | AsyncCallback\<string> | 是   | 回调函数,返回surface id。 |
Z
zhang-xiaobo1997 已提交
1818 1819 1820 1821

**示例:**

```js
X
xu-rui-w 已提交
1822
receiver.getReceivingSurfaceId((err, id) => { 
X
xu-rui-w 已提交
1823 1824 1825 1826 1827 1828
    if(err) {
        console.log('getReceivingSurfaceId failed.');
    } else {
        console.log('getReceivingSurfaceId succeeded.');
    }
});
Z
zhang-xiaobo1997 已提交
1829 1830
```

Z
zhang-xiaobo1997 已提交
1831
### getReceivingSurfaceId<sup>9+</sup>
Z
zhang-xiaobo1997 已提交
1832

Z
zhang-xiaobo1997 已提交
1833
getReceivingSurfaceId(): Promise\<string>
Z
zhang-xiaobo1997 已提交
1834

Z
zengyawen 已提交
1835
用于获取一个surface id供Camera或其他组件使用。使用promise返回结果。
Z
zhang-xiaobo1997 已提交
1836

Z
zengyawen 已提交
1837
**系统能力:** SystemCapability.Multimedia.Image.ImageReceiver
Z
zhang-xiaobo1997 已提交
1838 1839 1840

**返回值:**

Z
zhang-xiaobo1997 已提交
1841 1842 1843
| 类型             | 说明                 |
| ---------------- | -------------------- |
| Promise\<string> | 异步返回surface id。 |
Z
zhang-xiaobo1997 已提交
1844 1845 1846 1847 1848

**示例:**

```js
receiver.getReceivingSurfaceId().then( id => { 
X
xu-rui-w 已提交
1849 1850 1851 1852
    console.log('getReceivingSurfaceId succeeded.');
}).catch(error => {
    console.log('getReceivingSurfaceId failed.');
})
Z
zhang-xiaobo1997 已提交
1853 1854
```

Z
zhang-xiaobo1997 已提交
1855
### readLatestImage<sup>9+</sup>
Z
zhang-xiaobo1997 已提交
1856

Z
zhang-xiaobo1997 已提交
1857
readLatestImage(callback: AsyncCallback\<Image>): void
Z
zhang-xiaobo1997 已提交
1858

Z
zengyawen 已提交
1859
从ImageReceiver读取最新的图片,并使用callback返回结果。
Z
zhang-xiaobo1997 已提交
1860

Z
zengyawen 已提交
1861
**系统能力:** SystemCapability.Multimedia.Image.ImageReceiver
Z
zhang-xiaobo1997 已提交
1862 1863 1864

**参数:**

Z
zengyawen 已提交
1865 1866 1867
| 名称     | 类型                            | 必填 | 说明                     |
| -------- | ------------------------------- | ---- | ------------------------ |
| callback | AsyncCallback<[Image](#image9)> | 是   | 回调函数,返回最新图像。 |
Z
zhang-xiaobo1997 已提交
1868 1869 1870 1871

**示例:**

```js
X
xu-rui-w 已提交
1872 1873 1874 1875 1876 1877 1878
receiver.readLatestImage((err, img) => { 
    if(err) {
        console.log('readLatestImage failed.');
    } else {
        console.log('readLatestImage succeeded.');
    }
});
Z
zhang-xiaobo1997 已提交
1879 1880
```

Z
zhang-xiaobo1997 已提交
1881
### readLatestImage<sup>9+</sup>
Z
zhang-xiaobo1997 已提交
1882

Z
zhang-xiaobo1997 已提交
1883
readLatestImage(): Promise\<Image>
Z
zhang-xiaobo1997 已提交
1884

Z
zengyawen 已提交
1885
从ImageReceiver读取最新的图片,并使用promise返回结果。
Z
zhang-xiaobo1997 已提交
1886

Z
zengyawen 已提交
1887
**系统能力:** SystemCapability.Multimedia.Image.ImageReceiver
Z
zhang-xiaobo1997 已提交
1888 1889 1890 1891 1892

**返回值:**

| 类型                      | 说明               |
| ------------------------- | ------------------ |
1893
| Promise<[Image](#image9)> | 异步返回最新图片。 |
Z
zhang-xiaobo1997 已提交
1894 1895 1896 1897

**示例:**

```js
X
xu-rui-w 已提交
1898 1899 1900 1901 1902
receiver.readLatestImage().then(img => {
    console.log('readLatestImage succeeded.');
}).catch(error => {
    console.log('readLatestImage failed.');
})
Z
zhang-xiaobo1997 已提交
1903 1904
```

Z
zhang-xiaobo1997 已提交
1905
### readNextImage<sup>9+</sup>
Z
zhang-xiaobo1997 已提交
1906

Z
zhang-xiaobo1997 已提交
1907
readNextImage(callback: AsyncCallback\<Image>): void
Z
zhang-xiaobo1997 已提交
1908

Z
zengyawen 已提交
1909
从ImageReceiver读取下一张图片,并使用callback返回结果。
Z
zhang-xiaobo1997 已提交
1910

Z
zengyawen 已提交
1911
**系统能力:** SystemCapability.Multimedia.Image.ImageReceiver
Z
zhang-xiaobo1997 已提交
1912 1913 1914

**参数:**

Z
zengyawen 已提交
1915 1916 1917
| 名称     | 类型                            | 必填 | 说明                       |
| -------- | ------------------------------- | ---- | -------------------------- |
| callback | AsyncCallback<[Image](#image9)> | 是   | 回调函数,返回下一张图片。 |
Z
zhang-xiaobo1997 已提交
1918 1919 1920 1921

**示例:**

```js
X
xu-rui-w 已提交
1922 1923 1924 1925 1926 1927 1928
receiver.readNextImage((err, img) => { 
    if(err) {
        console.log('readNextImage failed.');
    } else {
        console.log('readNextImage succeeded.');
    }
});
Z
zhang-xiaobo1997 已提交
1929 1930
```

Z
zhang-xiaobo1997 已提交
1931
### readNextImage<sup>9+</sup>
Z
zhang-xiaobo1997 已提交
1932

Z
zhang-xiaobo1997 已提交
1933
readNextImage(): Promise\<Image>
Z
zhang-xiaobo1997 已提交
1934

Z
zengyawen 已提交
1935
从ImageReceiver读取下一张图片,并使用promise返回结果。
Z
zhang-xiaobo1997 已提交
1936

Z
zengyawen 已提交
1937
**系统能力:** SystemCapability.Multimedia.Image.ImageReceiver
Z
zhang-xiaobo1997 已提交
1938 1939 1940 1941 1942

**返回值:**

| 类型                      | 说明                 |
| ------------------------- | -------------------- |
Z
zengyawen 已提交
1943
| Promise<[Image](#image9)> | 异步返回下一张图片。 |
Z
zhang-xiaobo1997 已提交
1944 1945 1946 1947

**示例:**

```js
X
xu-rui-w 已提交
1948 1949 1950 1951 1952
receiver.readNextImage().then(img => {
    console.log('readNextImage succeeded.');
}).catch(error => {
    console.log('readNextImage failed.');
})
Z
zhang-xiaobo1997 已提交
1953 1954
```

Z
zengyawen 已提交
1955
### on('imageArrival')<sup>9+</sup>
Z
zhang-xiaobo1997 已提交
1956

Z
zhang-xiaobo1997 已提交
1957
on(type: 'imageArrival', callback: AsyncCallback\<void>): void
Z
zhang-xiaobo1997 已提交
1958 1959 1960

接收图片时注册回调。

Z
zengyawen 已提交
1961
**系统能力:** SystemCapability.Multimedia.Image.ImageReceiver
Z
zhang-xiaobo1997 已提交
1962 1963 1964

**参数:**

Z
zhang-xiaobo1997 已提交
1965 1966
| 名称     | 类型                 | 必填 | 说明                                                   |
| -------- | -------------------- | ---- | ------------------------------------------------------ |
Z
zengyawen 已提交
1967
| type     | string               | 是   | 注册事件的类型,固定为'imageArrival',接收图片时触发。 |
Z
zhang-xiaobo1997 已提交
1968
| callback | AsyncCallback\<void> | 是   | 注册的事件回调。                                       |
Z
zhang-xiaobo1997 已提交
1969 1970 1971 1972

**示例:**

```js
X
xu-rui-w 已提交
1973
receiver.on('imageArrival', () => {})
Z
zhang-xiaobo1997 已提交
1974 1975
```

Z
zhang-xiaobo1997 已提交
1976
### release<sup>9+</sup>
Z
zhang-xiaobo1997 已提交
1977

Z
zengyawen 已提交
1978
release(callback: AsyncCallback\<void>): void
Z
zhang-xiaobo1997 已提交
1979 1980 1981

释放ImageReceiver实例并使用回调返回结果。

Z
zengyawen 已提交
1982
**系统能力:** SystemCapability.Multimedia.Image.ImageReceiver
Z
zhang-xiaobo1997 已提交
1983 1984 1985

**参数:**

Z
zengyawen 已提交
1986 1987 1988
| 名称     | 类型                 | 必填 | 说明                     |
| -------- | -------------------- | ---- | ------------------------ |
| callback | AsyncCallback\<void> | 是   | 回调函数,返回操作结果。 |
Z
zhang-xiaobo1997 已提交
1989 1990 1991 1992

**示例:**

```js
X
xu-rui-w 已提交
1993
receiver.release(() => {})
Z
zhang-xiaobo1997 已提交
1994 1995
```

Z
zhang-xiaobo1997 已提交
1996
### release<sup>9+</sup>
Z
zhang-xiaobo1997 已提交
1997

Z
zhang-xiaobo1997 已提交
1998
release(): Promise\<void>
Z
zhang-xiaobo1997 已提交
1999 2000 2001

释放ImageReceiver实例并使用promise返回结果。

Z
zengyawen 已提交
2002
**系统能力:** SystemCapability.Multimedia.Image.ImageReceiver
Z
zhang-xiaobo1997 已提交
2003 2004 2005

**返回值:**

Z
zengyawen 已提交
2006 2007 2008
| 类型           | 说明               |
| -------------- | ------------------ |
| Promise\<void> | 异步返回操作结果。 |
Z
zhang-xiaobo1997 已提交
2009 2010 2011 2012

**示例:**

```js
X
xu-rui-w 已提交
2013 2014 2015 2016 2017
receiver.release().then(() => {
    console.log('release succeeded.');
}).catch(error => {
    console.log('release failed.');
})
Z
zhang-xiaobo1997 已提交
2018 2019
```

Z
zhang-xiaobo1997 已提交
2020
## Image<sup>9+</sup>
Z
zhang-xiaobo1997 已提交
2021

Z
zengyawen 已提交
2022
提供基本的图像操作,包括获取图像信息、读写图像数据。调用[readNextImage](#readnextimage9)[readLatestImage](#readlatestimage9)接口时会返回image。
Z
zhang-xiaobo1997 已提交
2023 2024 2025

### 属性

X
xu-rui-w 已提交
2026
**系统能力:** SystemCapability.Multimedia.Image.Core
Z
zhang-xiaobo1997 已提交
2027

X
xu-rui-w 已提交
2028 2029 2030
| 名称     | 类型               | 可读 | 可写 | 说明                                               |
| -------- | ------------------ | ---- | ---- | -------------------------------------------------- |
| clipRect | [Region](#region7) | 是   | 是   | 要裁剪的图像区域。                                 |
X
xu-rui-w 已提交
2031
| size     | [Size](#size)      | 是   | 否   | 图像大小。                                         |
X
xu-rui-w 已提交
2032
| format   | number             | 是   | 否   | 图像格式,参考[PixelMapFormat](#pixelmapformat7)。 |
Z
zhang-xiaobo1997 已提交
2033

Z
zhang-xiaobo1997 已提交
2034
### getComponent<sup>9+</sup>
Z
zhang-xiaobo1997 已提交
2035

Z
zhang-xiaobo1997 已提交
2036
getComponent(componentType: ComponentType, callback: AsyncCallback\<Component>): void
Z
zhang-xiaobo1997 已提交
2037

Z
zhang-xiaobo1997 已提交
2038
根据图像的组件类型从图像中获取组件缓存并使用callback返回结果。
Z
zhang-xiaobo1997 已提交
2039

Z
zengyawen 已提交
2040
**系统能力:** SystemCapability.Multimedia.Image.Core
Z
zhang-xiaobo1997 已提交
2041 2042 2043 2044 2045

**参数:**

| 名称          | 类型                                    | 必填 | 说明                 |
| ------------- | --------------------------------------- | ---- | -------------------- |
Z
zengyawen 已提交
2046 2047
| componentType | [ComponentType](#componenttype9)        | 是   | 图像的组件类型。     |
| callback      | AsyncCallback<[Component](#component9)> | 是   | 用于返回组件缓冲区。 |
Z
zhang-xiaobo1997 已提交
2048 2049 2050 2051

**示例:**

```js
X
xu-rui-w 已提交
2052 2053 2054 2055 2056 2057 2058
img.getComponent(4, (err, component) => {
    if(err) {
        console.log('getComponent failed.');
    } else {
        console.log('getComponent succeeded.');
    }
})
Z
zhang-xiaobo1997 已提交
2059 2060
```

Z
zhang-xiaobo1997 已提交
2061
### getComponent<sup>9+</sup>
Z
zhang-xiaobo1997 已提交
2062

Z
zhang-xiaobo1997 已提交
2063
getComponent(componentType: ComponentType): Promise\<Component>
Z
zhang-xiaobo1997 已提交
2064

Z
zengyawen 已提交
2065
根据图像的组件类型从图像中获取组件缓存并使用Promise方式返回结果。
Z
zhang-xiaobo1997 已提交
2066

Z
zengyawen 已提交
2067
**系统能力:** SystemCapability.Multimedia.Image.Core
Z
zhang-xiaobo1997 已提交
2068 2069 2070 2071 2072

**参数:**

| 名称          | 类型                             | 必填 | 说明             |
| ------------- | -------------------------------- | ---- | ---------------- |
Z
zengyawen 已提交
2073
| componentType | [ComponentType](#componenttype9) | 是   | 图像的组件类型。 |
Z
zhang-xiaobo1997 已提交
2074 2075 2076 2077 2078

**返回值:**

| 类型                              | 说明                              |
| --------------------------------- | --------------------------------- |
Z
zengyawen 已提交
2079
| Promise<[Component](#component9)> | 用于返回组件缓冲区的promise实例。 |
Z
zhang-xiaobo1997 已提交
2080 2081 2082 2083 2084 2085 2086

**示例:**

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

Z
zhang-xiaobo1997 已提交
2087
### release<sup>9+</sup>
Z
zhang-xiaobo1997 已提交
2088

Z
zhang-xiaobo1997 已提交
2089
release(callback: AsyncCallback\<void>): void
Z
zhang-xiaobo1997 已提交
2090

Z
zengyawen 已提交
2091 2092 2093
释放当前图像并使用callback返回结果。

在接收另一个图像前必须先释放对应资源。
Z
zhang-xiaobo1997 已提交
2094

Z
zengyawen 已提交
2095
**系统能力:** SystemCapability.Multimedia.Image.Core
Z
zhang-xiaobo1997 已提交
2096 2097 2098

**参数:**

Z
zhang-xiaobo1997 已提交
2099 2100 2101
| 名称     | 类型                 | 必填 | 说明           |
| -------- | -------------------- | ---- | -------------- |
| callback | AsyncCallback\<void> | 是   | 返回操作结果。 |
Z
zhang-xiaobo1997 已提交
2102 2103 2104 2105

**示例:**

```js
X
xu-rui-w 已提交
2106 2107 2108
img.release(() =>{ 
    console.log('release succeeded.');
}) 
Z
zhang-xiaobo1997 已提交
2109 2110
```

Z
zhang-xiaobo1997 已提交
2111
### release<sup>9+</sup>
Z
zhang-xiaobo1997 已提交
2112

Z
zhang-xiaobo1997 已提交
2113
release(): Promise\<void>
Z
zhang-xiaobo1997 已提交
2114

Z
zengyawen 已提交
2115 2116 2117
释放当前图像并使用Promise方式返回结果。

在接收另一个图像前必须先释放对应资源。
Z
zhang-xiaobo1997 已提交
2118

Z
zengyawen 已提交
2119
**系统能力:** SystemCapability.Multimedia.Image.Core
Z
zhang-xiaobo1997 已提交
2120 2121 2122

**返回值:**

Z
zhang-xiaobo1997 已提交
2123 2124 2125
| 类型           | 说明                  |
| -------------- | --------------------- |
| Promise\<void> | promise返回操作结果。 |
Z
zhang-xiaobo1997 已提交
2126 2127 2128 2129 2130

**示例:**

```js
img.release().then(() =>{
X
xu-rui-w 已提交
2131 2132 2133 2134
    console.log('release succeeded.');
}).catch(error => {
    console.log('release failed.');
})
Z
zhang-xiaobo1997 已提交
2135 2136
```

Z
zengyawen 已提交
2137 2138 2139 2140
## PositionArea<sup>7+</sup>

表示图片指定区域内的数据。

X
xu-rui-w 已提交
2141
**系统能力:** SystemCapability.Multimedia.Image.Core
Z
zengyawen 已提交
2142

2143 2144 2145 2146
| 名称   | 类型               | 可读 | 可写 | 说明                                                         |
| ------ | ------------------ | ---- | ---- | ------------------------------------------------------------ |
| pixels | ArrayBuffer        | 是   | 否   | 像素。                                                       |
| offset | number             | 是   | 否   | 偏移量。                                                     |
X
xu-rui-w 已提交
2147
| stride | number             | 是   | 否   | 像素间距,stride >= region.size.width*4                      |
X
xu-rui-w 已提交
2148
| region | [Region](#region7) | 是   | 否   | 区域,按照区域读写。写入的区域宽度加X坐标不能大于原图的宽度,写入的区域高度加Y坐标不能大于原图的高度。 |
X
xiaok 已提交
2149

Z
zengyawen 已提交
2150
## ImageInfo
X
xiaok 已提交
2151

Z
zengyawen 已提交
2152
表示图片信息。
X
xiaok 已提交
2153

X
xu-rui-w 已提交
2154
**系统能力:** SystemCapability.Multimedia.Image.Core
X
xiaok 已提交
2155

Z
zengyawen 已提交
2156 2157 2158
| 名称 | 类型          | 可读 | 可写 | 说明       |
| ---- | ------------- | ---- | ---- | ---------- |
| size | [Size](#size) | 是   | 是   | 图片大小。 |
X
xiaok 已提交
2159

Z
zengyawen 已提交
2160
## Size
X
xiaok 已提交
2161

Z
zengyawen 已提交
2162 2163
表示图片尺寸。

X
xu-rui-w 已提交
2164
**系统能力:** SystemCapability.Multimedia.Image.Core
Z
zengyawen 已提交
2165 2166 2167 2168 2169 2170 2171

| 名称   | 类型   | 可读 | 可写 | 说明           |
| ------ | ------ | ---- | ---- | -------------- |
| height | number | 是   | 是   | 输出图片的高。 |
| width  | number | 是   | 是   | 输出图片的宽。 |

## PixelMapFormat<sup>7+</sup>
X
xiaok 已提交
2172

Z
zengyawen 已提交
2173
枚举,图片像素格式。
X
xiaok 已提交
2174

X
xu-rui-w 已提交
2175
**系统能力:** SystemCapability.Multimedia.Image.Core
Z
zengyawen 已提交
2176

X
xu-rui-w 已提交
2177 2178 2179 2180
| 名称                   | 默认值 | 描述              |
| ---------------------- | ------ | ----------------- |
| UNKNOWN                | 0      | 未知格式。        |
| RGB_565                | 2      | 格式为RGB_565     |
X
xu-rui-w 已提交
2181 2182
| RGBA_8888              | 3      | 格式为RGBA_8888   |
| BGRA_8888<sup>9+</sup> | 4      | 格式为BGRA_8888   |
Z
zengyawen 已提交
2183

2184
## AlphaType<sup>9+</sup>
X
xiaok 已提交
2185

Z
zengyawen 已提交
2186
枚举,图像的透明度类型。
X
xiaok 已提交
2187

X
xu-rui-w 已提交
2188
**系统能力:** SystemCapability.Multimedia.Image.Core
Z
zengyawen 已提交
2189 2190 2191 2192 2193

| 名称     | 默认值 | 描述                    |
| -------- | ------ | ----------------------- |
| UNKNOWN  | 0      | 未知透明度。            |
| OPAQUE   | 1      | 没有alpha或图片全透明。 |
X
xu-rui-w 已提交
2194 2195
| PREMUL   | 2      | RGB前乘alpha           |
| UNPREMUL | 3      | RGB不前乘alpha         |
X
xiaok 已提交
2196

2197
## ScaleMode<sup>9+</sup>
X
xiaok 已提交
2198

Z
zengyawen 已提交
2199
枚举,图像的缩放模式。
X
xiaok 已提交
2200

X
xu-rui-w 已提交
2201
**系统能力:** SystemCapability.Multimedia.Image.Core
X
xiaok 已提交
2202

Z
zengyawen 已提交
2203 2204 2205
| 名称            | 默认值 | 描述                                               |
| --------------- | ------ | -------------------------------------------------- |
| CENTER_CROP     | 1      | 缩放图像以填充目标图像区域并居中裁剪区域外的效果。 |
X
xu-rui-w 已提交
2206
| FIT_TARGET_SIZE | 0      | 图像适合目标尺寸的效果。                           |
X
xiaok 已提交
2207

X
xu-rui-w 已提交
2208 2209 2210 2211
## SourceOptions<sup>9+</sup>

ImageSource的初始化选项。

X
xu-rui-w 已提交
2212
**系统能力:** SystemCapability.Multimedia.Image.Core
X
xu-rui-w 已提交
2213

X
xu-rui-w 已提交
2214 2215 2216 2217 2218
| 名称              | 类型                               | 可读 | 可写 | 说明               |
| ----------------- | ---------------------------------- | ---- | ---- | ------------------ |
| sourceDensity     | number                             | 是   | 是   | ImageSource的密度。|
| sourcePixelFormat | [PixelMapFormat](#pixelmapformat7) | 是   | 是   | 图片像素格式。     |
| sourceSize        | [Size](#size)                      | 是   | 是   | 图像像素大小。     |
X
xu-rui-w 已提交
2219 2220


Z
zengyawen 已提交
2221
## InitializationOptions<sup>8+</sup>
X
xiaok 已提交
2222

Z
zengyawen 已提交
2223 2224
PixelMap的初始化选项。

X
xu-rui-w 已提交
2225
**系统能力:** SystemCapability.Multimedia.Image.Core
Z
zengyawen 已提交
2226

X
xu-rui-w 已提交
2227 2228 2229
| 名称                     | 类型                               | 可读 | 可写 | 说明           |
| ------------------------ | ---------------------------------- | ---- | ---- | -------------- |
| alphaType<sup>9+</sup>   | [AlphaType](#alphatype9)           | 是   | 是   | 透明度。       |
X
xu-rui-w 已提交
2230 2231
| editable                 | boolean                            | 是   | 是   | 是否可编辑。   |
| pixelFormat              | [PixelMapFormat](#pixelmapformat7) | 是   | 是   | 像素格式。     |
X
xu-rui-w 已提交
2232
| scaleMode<sup>9+</sup>   | [ScaleMode](#scalemode9)           | 是   | 是   | 缩略值。       |
X
xu-rui-w 已提交
2233
| size                     | [Size](#size)                      | 是   | 是   | 创建图片大小。 |
Z
zengyawen 已提交
2234 2235

## DecodingOptions<sup>7+</sup>
X
xiaok 已提交
2236

Z
zengyawen 已提交
2237
图像解码设置选项。
X
xiaok 已提交
2238

X
xu-rui-w 已提交
2239
**系统能力:** SystemCapability.Multimedia.Image.ImageSource
Z
zengyawen 已提交
2240 2241 2242 2243

| 名称               | 类型                               | 可读 | 可写 | 说明             |
| ------------------ | ---------------------------------- | ---- | ---- | ---------------- |
| sampleSize         | number                             | 是   | 是   | 缩略图采样大小。 |
Z
zengyawen 已提交
2244
| rotate             | number                             | 是   | 是   | 旋转角度。       |
Z
zengyawen 已提交
2245 2246
| editable           | boolean                            | 是   | 是   | 是否可编辑。     |
| desiredSize        | [Size](#size)                      | 是   | 是   | 期望输出大小。   |
2247
| desiredRegion      | [Region](#region7)                 | 是   | 是   | 解码区域。       |
Z
zengyawen 已提交
2248
| desiredPixelFormat | [PixelMapFormat](#pixelmapformat7) | 是   | 是   | 解码的像素格式。 |
X
xu-rui-w 已提交
2249
| index              | number                             | 是   | 是   | 解码图片序号。   |
Z
zengyawen 已提交
2250

2251
## Region<sup>7+</sup>
Z
zengyawen 已提交
2252 2253 2254

表示区域信息。

X
xu-rui-w 已提交
2255
**系统能力:** SystemCapability.Multimedia.Image.Core
Z
zengyawen 已提交
2256

2257 2258 2259 2260 2261
| 名称 | 类型          | 可读 | 可写 | 说明         |
| ---- | ------------- | ---- | ---- | ------------ |
| size | [Size](#size) | 是   | 是   | 区域大小。   |
| x    | number        | 是   | 是   | 区域横坐标。 |
| y    | number        | 是   | 是   | 区域纵坐标。 |
Z
zengyawen 已提交
2262 2263 2264 2265 2266

## PackingOption

表示图片打包选项。

X
xu-rui-w 已提交
2267
**系统能力:** SystemCapability.Multimedia.Image.ImagePacker
Z
zengyawen 已提交
2268

X
xu-rui-w 已提交
2269 2270
| 名称    | 类型   | 可读 | 可写 | 说明                                                |
| ------- | ------ | ---- | ---- | --------------------------------------------------- |
X
xu-rui-w 已提交
2271 2272
| format  | string | 是   | 是   | 目标格式。</br>当前支持格式有:jpeg webp  |
| quality | number | 是   | 是   | JPEG编码中设定输出图片质量的参数,取值范围为1-100 |
Z
zengyawen 已提交
2273 2274 2275 2276 2277

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

表示查询图片属性的索引。

X
xu-rui-w 已提交
2278
**系统能力:** SystemCapability.Multimedia.Image.ImageSource
Z
zengyawen 已提交
2279 2280 2281 2282 2283 2284 2285 2286 2287 2288

| 名称         | 类型   | 可读 | 可写 | 说明         |
| ------------ | ------ | ---- | ---- | ------------ |
| index        | number | 是   | 是   | 图片序号。   |
| defaultValue | string | 是   | 是   | 默认属性值。 |

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

枚举,Exif(Exchangeable image file format)图片信息。

X
xu-rui-w 已提交
2289
**系统能力:** SystemCapability.Multimedia.Image.Core
2290

X
xu-rui-w 已提交
2291 2292 2293 2294 2295 2296 2297 2298 2299 2300
| 名称              | 默认值                  | 说明                     |
| ----------------- | ----------------------- | ------------------------ |
| BITS_PER_SAMPLE   | "BitsPerSample"         | 每个像素比特数。         |
| ORIENTATION       | "Orientation"           | 图片方向。               |
| IMAGE_LENGTH      | "ImageLength"           | 图片长度。               |
| IMAGE_WIDTH       | "ImageWidth"            | 图片宽度。               |
| GPS_LATITUDE      | "GPSLatitude"           | 图片纬度。               |
| GPS_LONGITUDE     | "GPSLongitude"          | 图片经度。               |
| GPS_LATITUDE_REF  | "GPSLatitudeRef"        | 纬度引用,例如N或S。     |
| GPS_LONGITUDE_REF | "GPSLongitudeRef"       | 经度引用,例如W或E。     |
2301

Z
zhang-xiaobo1997 已提交
2302
## ImageFormat<sup>9+</sup>
Z
zhang-xiaobo1997 已提交
2303 2304 2305

枚举,图片格式。

X
xu-rui-w 已提交
2306
**系统能力:** SystemCapability.Multimedia.Image.Core
Z
zhang-xiaobo1997 已提交
2307

Z
zengyawen 已提交
2308 2309 2310 2311
| 名称         | 默认值 | 描述                 |
| ------------ | ------ | -------------------- |
| YCBCR_422_SP | 1000   | YCBCR422半平面格式。 |
| JPEG         | 2000   | JPEG编码格式。       |
Z
zhang-xiaobo1997 已提交
2312

Z
zengyawen 已提交
2313
## ComponentType<sup>9+</sup>
Z
zhang-xiaobo1997 已提交
2314 2315 2316

枚举,图像的组件类型。

X
xu-rui-w 已提交
2317
**系统能力:** SystemCapability.Multimedia.Image.ImageReceiver
Z
zhang-xiaobo1997 已提交
2318 2319 2320 2321 2322 2323

| 名称  | 默认值 | 描述        |
| ----- | ------ | ----------- |
| YUV_Y | 1      | 亮度信息。  |
| YUV_U | 2      | 色度信息。  |
| YUV_V | 3      | 色度信息。  |
X
xu-rui-w 已提交
2324
| JPEG  | 4      | JPEG 类型。 |
Z
zhang-xiaobo1997 已提交
2325

Z
zengyawen 已提交
2326
## Component<sup>9+</sup>
Z
zhang-xiaobo1997 已提交
2327 2328 2329

描述图像颜色分量。

X
xu-rui-w 已提交
2330
**系统能力:** SystemCapability.Multimedia.Image.Core
Z
zhang-xiaobo1997 已提交
2331 2332 2333

| 名称          | 类型                             | 可读 | 可写 | 说明         |
| ------------- | -------------------------------- | ---- | ---- | ------------ |
Z
zengyawen 已提交
2334
| componentType | [ComponentType](#componenttype9) | 是   | 否   | 组件类型。   |
Z
zhang-xiaobo1997 已提交
2335 2336 2337 2338
| rowStride     | number                           | 是   | 否   | 行距。       |
| pixelStride   | number                           | 是   | 否   | 像素间距。   |
| byteBuffer    | ArrayBuffer                      | 是   | 否   | 组件缓冲区。 |

X
xu-rui-w 已提交
2339
## ResponseCode
X
xu-rui-w 已提交
2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379

编译错误返回的响应码。

| 名称                                | 值       | 说明                                                |
| ----------------------------------- | -------- | --------------------------------------------------- |
| ERR_MEDIA_INVALID_VALUE             | -1       | 无效大小。                                          |
| SUCCESS                             | 0        | 操作成功。                                          |
| ERROR                               | 62980096 | 操作失败。                                          |
| ERR_IPC                             | 62980097 | ipc错误。                                           |
| ERR_SHAMEM_NOT_EXIST                | 62980098 | 共享内存错误。                                      |
| ERR_SHAMEM_DATA_ABNORMAL            | 62980099 | 共享内存错误。                                      |
| ERR_IMAGE_DECODE_ABNORMAL           | 62980100 | 图像解码错误。                                      |
| ERR_IMAGE_DATA_ABNORMAL             | 62980101 | 图像输入数据错误。                                  |
| ERR_IMAGE_MALLOC_ABNORMAL           | 62980102 | 图像malloc错误。                                    |
| ERR_IMAGE_DATA_UNSUPPORT            | 62980103 | 不支持图像类型。                                    |
| ERR_IMAGE_INIT_ABNORMAL             | 62980104 | 图像初始化错误。                                    |
| ERR_IMAGE_GET_DATA_ABNORMAL         | 62980105 | 图像获取数据错误。                                  |
| ERR_IMAGE_TOO_LARGE                 | 62980106 | 图像数据太大。                                      |
| ERR_IMAGE_TRANSFORM                 | 62980107 | 图像转换错误。                                      |
| ERR_IMAGE_COLOR_CONVERT             | 62980108 | 图像颜色转换错误。                                  |
| ERR_IMAGE_CROP                      | 62980109 | 裁剪错误。                                          |
| ERR_IMAGE_SOURCE_DATA               | 62980110 | 图像源数据错误。                                    |
| ERR_IMAGE_SOURCE_DATA_INCOMPLETE    | 62980111 | 图像源数据不完整。                                  |
| ERR_IMAGE_MISMATCHED_FORMAT         | 62980112 | 图像格式不匹配。                                    |
| ERR_IMAGE_UNKNOWN_FORMAT            | 62980113 | 图像未知格式。                                      |
| ERR_IMAGE_SOURCE_UNRESOLVED         | 62980114 | 图像源未解析。                                      |
| ERR_IMAGE_INVALID_PARAMETER         | 62980115 | 图像无效参数。                                      |
| ERR_IMAGE_DECODE_FAILED             | 62980116 | 解码失败。                                          |
| ERR_IMAGE_PLUGIN_REGISTER_FAILED    | 62980117 | 注册插件失败。                                      |
| ERR_IMAGE_PLUGIN_CREATE_FAILED      | 62980118 | 创建插件失败。                                      |
| ERR_IMAGE_ENCODE_FAILED             | 62980119 | 图像编码失败。                                      |
| ERR_IMAGE_ADD_PIXEL_MAP_FAILED      | 62980120 | 图像添加像素映射失败。                              |
| ERR_IMAGE_HW_DECODE_UNSUPPORT       | 62980121 | 不支持图像硬件解码。                                |
| ERR_IMAGE_DECODE_HEAD_ABNORMAL      | 62980122 | 图像解码头错误。                                    |
| ERR_IMAGE_DECODE_EXIF_UNSUPPORT     | 62980123 | 图像解码exif取消支持。                              |
| ERR_IMAGE_PROPERTY_NOT_EXIST        | 62980124 | 图像属性不存在;错误代码被媒体占用,图像从150开始。 |
| ERR_IMAGE_READ_PIXELMAP_FAILED      | 62980246 | 读取像素地图失败。                                  |
| ERR_IMAGE_WRITE_PIXELMAP_FAILED     | 62980247 | 写入像素映射失败。                                  |
| ERR_IMAGE_PIXELMAP_NOT_ALLOW_MODIFY | 62980248 | pixelmap不允许修改。                                |
| ERR_IMAGE_CONFIG_FAILED             | 62980259 | 配置错误。                                          |