js-apis-screenshot.md 5.5 KB
Newer Older
W
wusongqing 已提交
1
# Screenshot
W
wusongqing 已提交
2
The **Screenshot** module provides APIs for you to set information such as the region to capture and the size of the screen region when capturing a screen.
W
wusongqing 已提交
3

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

## Modules to Import

12
```js
W
wusongqing 已提交
13 14 15 16 17 18 19
import screenshot from '@ohos.screenshot';
```

## ScreenshotOptions

Describes screenshot options.

W
wusongqing 已提交
20 21 22
**System capability**: SystemCapability.WindowManager.WindowManager.Core


23 24 25 26 27 28
| Name                | Type         | Mandatory| Description                                                        |
| ---------------------- | ------------- | ---- | ------------------------------------------------------------ |
| screenRect             | [Rect](#rect) | No  | Region of the screen to capture. If this parameter is null, the full screen will be captured.                      |
| imageSize              | [Size](#size) | No  | Size of the screen region to capture. If this parameter is null, the full screen will be captured.                      |
| rotation               | number        | No  | Rotation angle of the screenshot. Currently, the value can be **0** only. The default value is **0**.    |
| displayId<sup>8+</sup> | number        | No  | ID of the [display](js-apis-display.md#display) device on which the screen region is to be captured.|
W
wusongqing 已提交
29 30 31 32 33 34


## Rect

Describes the region of the screen to capture.

W
wusongqing 已提交
35 36 37
**System capability**: SystemCapability.WindowManager.WindowManager.Core

| Name| Type  | Mandatory| Description                                                        |
W
wusongqing 已提交
38
| ------ | ------ | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
39 40 41 42
| left   | number | Yes  | Left boundary of the screen region to capture.|
| top    | number | Yes  | Top boundary of the screen region to capture.|
| width  | number | Yes  | Width of the screen region to capture.|
| height | number | Yes  | Height of the screen region to capture.|
W
wusongqing 已提交
43 44 45 46 47 48


## Size

Describes the size of the screen region to capture.

W
wusongqing 已提交
49 50 51
**System capability**: SystemCapability.WindowManager.WindowManager.Core

| Name| Type  | Mandatory| Description                                                        |
W
wusongqing 已提交
52
| ------ | ------ | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
53 54
| width  | number | Yes  | Width of the screen region to capture.|
| height | number | Yes  | Height of the screen region to capture.|
W
wusongqing 已提交
55 56 57 58 59

## screenshot.save

save(options?: ScreenshotOptions, callback: AsyncCallback&lt;image.PixelMap&gt;): void

W
wusongqing 已提交
60
Takes a screenshot and saves it as a **PixelMap** object. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
61

W
wusongqing 已提交
62
**System capability**: SystemCapability.WindowManager.WindowManager.Core
W
wusongqing 已提交
63

W
wusongqing 已提交
64
**Required permissions**: ohos.permission.CAPTURE_SCREEN (available only to system applications)
W
wusongqing 已提交
65

W
wusongqing 已提交
66
**Parameters**
W
wusongqing 已提交
67

W
wusongqing 已提交
68 69
| Name  | Type                                   | Mandatory| Description                                                        |
| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
70
| options  | [ScreenshotOptions](#screenshotoptions) | No  | Screenshot settings consist of **screenRect**, **imageSize**, **rotation**, and **displayId**. You can set the parameters separately.|
W
wusongqing 已提交
71
| callback | AsyncCallback&lt;image.PixelMap&gt;     | Yes  | Callback used to return a **PixelMap** object.                                  |
W
wusongqing 已提交
72

W
wusongqing 已提交
73
**Example**
W
wusongqing 已提交
74

75
  ```js
W
wusongqing 已提交
76 77 78 79 80 81 82 83 84
  var ScreenshotOptions = {
  	"screenRect": {
  		"left": 200,
  		"top": 100,
  		"width": 200,
  		"height": 200},
  	"imageSize": {
  		"width": 300,
  		"height": 300},
85 86
  	"rotation": 0,
  	"displayId": 0
W
wusongqing 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100
  };
  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&lt;image.PixelMap&gt;

W
wusongqing 已提交
101
Takes a screenshot and saves it as a **PixelMap** object. This API uses a promise to return the result.
W
wusongqing 已提交
102

W
wusongqing 已提交
103
**System capability**: SystemCapability.WindowManager.WindowManager.Core
W
wusongqing 已提交
104

W
wusongqing 已提交
105
**Required permissions**: ohos.permission.CAPTURE_SCREEN (available only to system applications)
W
wusongqing 已提交
106

W
wusongqing 已提交
107
**Parameters**
W
wusongqing 已提交
108

W
wusongqing 已提交
109 110
| Name | Type                                   | Mandatory| Description                                                        |
| ------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
111
| options | [ScreenshotOptions](#screenshotoptions) | No  | Screenshot settings consist of **screenRect**, **imageSize**, **rotation**, and **displayId**. You can set the parameters separately.|
W
wusongqing 已提交
112

W
wusongqing 已提交
113
**Return value**
W
wusongqing 已提交
114

W
wusongqing 已提交
115 116
| Type                         | Description                                           |
| ----------------------------- | ----------------------------------------------- |
117
| Promise&lt;image.PixelMap&gt; | Promise used to return a **PixelMap** object.|
W
wusongqing 已提交
118

W
wusongqing 已提交
119
**Example**
W
wusongqing 已提交
120

121
  ```js
W
wusongqing 已提交
122 123 124 125 126 127 128 129 130
  var ScreenshotOptions = {
  	"screenRect": {
  		"left": 200,
  		"top": 100,
  		"width": 200,
  		"height": 200},
  	"imageSize": {
  		"width": 300,
  		"height": 300},
131 132
  	"rotation": 0,
  	"displayId": 0
W
wusongqing 已提交
133 134 135 136 137 138 139 140
  };
  let promise = screenshot.save(ScreenshotOptions);
  promise.then(() => {
      console.log('screenshot save success');
  }).catch((err) => {
      console.log('screenshot save fail: ' + JSON.stringify(err));
  });
  ```