js-apis-screenshot.md 4.9 KB
Newer Older
W
wusongqing 已提交
1 2
# Screenshot

W
wusongqing 已提交
3
>  **NOTE**<br/>
W
wusongqing 已提交
4 5 6 7
> 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.

## Modules to Import

8
```js
W
wusongqing 已提交
9 10 11 12 13 14 15
import screenshot from '@ohos.screenshot';
```

## ScreenshotOptions

Describes screenshot options.

W
wusongqing 已提交
16 17 18 19
**System capability**: SystemCapability.WindowManager.WindowManager.Core


| Name    | Type         | Mandatory| Description                                                        |
W
wusongqing 已提交
20
| ---------- | ------------- | ---- | ------------------------------------------------------------ |
21 22
| 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.|
W
wusongqing 已提交
23
| rotation   | number        | No  | Rotation angle of the screenshot. Currently, the value can be **0** only. The default value is **0**.|
W
wusongqing 已提交
24 25 26 27 28 29


## Rect

Describes the region of the screen to capture.

W
wusongqing 已提交
30 31 32
**System capability**: SystemCapability.WindowManager.WindowManager.Core

| Name| Type  | Mandatory| Description                                                        |
W
wusongqing 已提交
33
| ------ | ------ | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
34 35 36 37
| 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 已提交
38 39 40 41 42 43


## Size

Describes the size of the screen region to capture.

W
wusongqing 已提交
44 45 46
**System capability**: SystemCapability.WindowManager.WindowManager.Core

| Name| Type  | Mandatory| Description                                                        |
W
wusongqing 已提交
47
| ------ | ------ | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
48 49
| width  | number | Yes  | Width of the screen region to capture.|
| height | number | Yes  | Height of the screen region to capture.|
W
wusongqing 已提交
50 51 52 53 54 55 56

## screenshot.save

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

Takes a screenshot and saves it as a **PixelMap** object. This method uses a callback to return the result.

W
wusongqing 已提交
57
**System capability**: SystemCapability.WindowManager.WindowManager.Core
W
wusongqing 已提交
58 59 60

**Required permissions**: ohos.permission.CAPTURE_SCREEN

W
wusongqing 已提交
61
**Parameters**
W
wusongqing 已提交
62

W
wusongqing 已提交
63
  | Name  | Type                                   | Mandatory| Description                                                        |
W
wusongqing 已提交
64
  | -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
65
  | options  | [ScreenshotOptions](#screenshotoptions) | No  | Screenshot options, which consist of **screenRect**, **imageSize**, and **rotation**. You need to set these parameters.|
W
wusongqing 已提交
66
  | callback | AsyncCallback&lt;image.PixelMap&gt;     | Yes  | Callback used to return a **PixelMap** object.                                  |
W
wusongqing 已提交
67

W
wusongqing 已提交
68
**Example**
W
wusongqing 已提交
69

70
  ```js
W
wusongqing 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
  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&lt;image.PixelMap&gt;

Takes a screenshot and saves it as a **PixelMap** object. This method uses a promise to return the result.

W
wusongqing 已提交
97
**System capability**: SystemCapability.WindowManager.WindowManager.Core
W
wusongqing 已提交
98 99 100

**Required permissions**: ohos.permission.CAPTURE_SCREEN

W
wusongqing 已提交
101
**Parameters**
W
wusongqing 已提交
102

W
wusongqing 已提交
103 104 105
| Name | Type                                   | Mandatory| Description                                                        |
| ------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
| options | [ScreenshotOptions](#screenshotoptions) | No  | Screenshot options, which consist of **screenRect**, **imageSize**, and **rotation**. You need to set these parameters.|
W
wusongqing 已提交
106

W
wusongqing 已提交
107
**Return value**
W
wusongqing 已提交
108

W
wusongqing 已提交
109
  | Type                         | Description                                           |
W
wusongqing 已提交
110 111 112
  | ----------------------------- | ----------------------------------------------- |
  | Promise&lt;image.PixelMap&gt; | Promise used to return an **image.PixelMap** object.|

W
wusongqing 已提交
113
**Example**
W
wusongqing 已提交
114

115
  ```js
W
wusongqing 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  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));
  });
  ```