js-apis-screenshot.md 4.6 KB
Newer Older
1 2
# 屏幕截图

G
ge-yafang 已提交
3
>  **说明:**
4
>
5 6 7 8
> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

## 导入模块

9
```js
10 11 12 13 14 15 16
import screenshot from '@ohos.screenshot';
```

## ScreenshotOptions

设置截取图像的信息。

17
**系统能力:** SystemCapability.WindowManager.WindowManager.Core
18 19


20 21
| 参数名     | 类型          | 必填 | 说明                                                         |
| ---------- | ------------- | ---- | ------------------------------------------------------------ |
22 23
| screenRect | [Rect](#rect) | 否   | 表示截取图像的区域,不传值默认为全屏。|
| imageSize  | [Size](#size) | 否   | 表示截取图像的大小,不传值默认为全屏。|
24
| rotation   | number        | 否   | 表示截取图像的旋转角度,当前仅支持输入值为0,默认值为0。|
25 26 27 28 29 30


## Rect

表示截取图像的区域。

31
**系统能力:** SystemCapability.WindowManager.WindowManager.Core
32

33 34
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
35 36 37 38
| left   | number | 是   | 表示截取图像区域的左边界。|
| top    | number | 是   | 表示截取图像区域的上边界。|
| width  | number | 是   | 表示截取图像区域的宽度。|
| height | number | 是   | 表示截取图像区域的高度。|
39 40 41 42 43 44


## Size

表示截取图像的大小。

45
**系统能力:** SystemCapability.WindowManager.WindowManager.Core
46

47 48
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
49 50
| width  | number | 是   | 表示截取图像的宽度。|
| height | number | 是   | 表示截取图像的高度。|
51 52 53 54 55 56 57

## screenshot.save

save(options?: ScreenshotOptions, callback: AsyncCallback<image.PixelMap>): void

获取屏幕截图。

G
ge-yafang 已提交
58
**系统能力:** SystemCapability.WindowManager.WindowManager.Core
59

60
**需要权限**:ohos.permission.CAPTURE_SCREEN,仅系统应用可用。
61

G
ge-yafang 已提交
62
**参数:**
63 64 65

  | 参数名   | 类型                                    | 必填 | 说明                                                         |
  | -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
66
  | options  | [ScreenshotOptions](#screenshotoptions) | 否   | 该类型的参数包含screenRect,imageSize,rotation三个参数,需要分别设置这三个参数。 |
67 68
  | callback | AsyncCallback<image.PixelMap>     | 是   | 回调返回一个PixelMap对象。                                   |

G
ge-yafang 已提交
69
**示例:**
70

71
  ```js
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  var ScreenshotOptions = {
  	"screenRect": {
  		"left": 200,
  		"top": 100,
  		"width": 200,
  		"height": 200},
  	"imageSize": {
  		"width": 300,
  		"height": 300},
  	"rotation": 0
  };
  screenshot.save(ScreenshotOptions, (err, data) => {
  	if (err) {
  		console.error('Failed to save the screenshot. Error: ' + JSON.stringify(err));
  		return;
  	}
  	console.info('Screenshot saved. Data: ' + JSON.stringify(data));
  });
  ```

## screenshot.save

save(options?: ScreenshotOptions): Promise<image.PixelMap>

获取屏幕截图。

G
ge-yafang 已提交
98
**系统能力:** SystemCapability.WindowManager.WindowManager.Core
99

100
**需要权限**:ohos.permission.CAPTURE_SCREEN,仅系统应用可用。
101

G
ge-yafang 已提交
102
**参数:**
103

G
ge-yafang 已提交
104 105 106
| 参数名  | 类型                                    | 必填 | 说明                                                         |
| ------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
| options | [ScreenshotOptions](#screenshotoptions) | 否   | 该类型的参数包含screenRect、imageSize、rotation三个参数,需要分别设置这三个参数。 |
107

G
ge-yafang 已提交
108
**返回值:**
109 110 111 112 113

  | 类型                          | 说明                                            |
  | ----------------------------- | ----------------------------------------------- |
  | Promise<image.PixelMap> | 以Promise形式返回结果,返回image.PixelMap对象。 |

G
ge-yafang 已提交
114
**示例:**
115

116
  ```js
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  var ScreenshotOptions = {
  	"screenRect": {
  		"left": 200,
  		"top": 100,
  		"width": 200,
  		"height": 200},
  	"imageSize": {
  		"width": 300,
  		"height": 300},
  	"rotation": 0
  };
  let promise = screenshot.save(ScreenshotOptions);
  promise.then(() => {
      console.log('screenshot save success');
  }).catch((err) => {
      console.log('screenshot save fail: ' + JSON.stringify(err));
  });
  ```