1# 屏幕截图 2本模块提供屏幕截图的能力,截取屏幕时支持设置截取的区域、大小等图像信息。 3 4> **说明:** 5> 6> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 7> 8> 该模块接口为系统接口。 9 10## 导入模块 11 12```js 13import screenshot from '@ohos.screenshot'; 14``` 15 16## ScreenshotOptions 17 18设置截取图像的信息。 19 20**系统能力:** SystemCapability.WindowManager.WindowManager.Core 21 22 23| 参数名 | 类型 | 必填 | 说明 | 24| ---------------------- | ------------- | ---- | ------------------------------------------------------------ | 25| screenRect | [Rect](#rect) | 否 | 表示截取图像的区域,不传值默认为全屏。 | 26| imageSize | [Size](#size) | 否 | 表示截取图像的大小,不传值默认为全屏。 | 27| rotation | number | 否 | 表示截取图像的旋转角度,当前仅支持输入值为0,默认值为0。 | 28| displayId<sup>8+</sup> | number | 否 | 表示截取图像的显示设备[Display](js-apis-display.md#display)的ID号。 | 29 30 31## Rect 32 33表示截取图像的区域。 34 35**系统能力:** SystemCapability.WindowManager.WindowManager.Core 36 37| 参数名 | 类型 | 必填 | 说明 | 38| ------ | ------ | ---- | ------------------------------------------------------------ | 39| left | number | 是 | 表示截取图像区域的左边界,单位为像素。| 40| top | number | 是 | 表示截取图像区域的上边界,单位为像素。| 41| width | number | 是 | 表示截取图像区域的宽度,单位为像素。| 42| height | number | 是 | 表示截取图像区域的高度,单位为像素。| 43 44 45## Size 46 47表示截取图像的大小。 48 49**系统能力:** SystemCapability.WindowManager.WindowManager.Core 50 51| 参数名 | 类型 | 必填 | 说明 | 52| ------ | ------ | ---- | ------------------------------------------------------------ | 53| width | number | 是 | 表示截取图像的宽度,单位为像素。| 54| height | number | 是 | 表示截取图像的高度,单位为像素。| 55 56## screenshot.save 57 58save(options?: ScreenshotOptions, callback: AsyncCallback<image.PixelMap>): void 59 60获取屏幕截图。 61 62**系统能力:** SystemCapability.WindowManager.WindowManager.Core 63 64**需要权限**:ohos.permission.CAPTURE_SCREEN,仅系统应用可用。 65 66**参数:** 67 68| 参数名 | 类型 | 必填 | 说明 | 69| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 70| options | [ScreenshotOptions](#screenshotoptions) | 否 | 该类型的参数包含screenRect,imageSize,rotation, displayId四个参数,可以分别设置这四个参数。 | 71| callback | AsyncCallback<image.PixelMap> | 是 | 回调函数。返回一个PixelMap对象。 | 72 73**示例:** 74 75 ```js 76 var ScreenshotOptions = { 77 "screenRect": { 78 "left": 200, 79 "top": 100, 80 "width": 200, 81 "height": 200}, 82 "imageSize": { 83 "width": 300, 84 "height": 300}, 85 "rotation": 0, 86 "displayId": 0 87 }; 88 screenshot.save(ScreenshotOptions, (err, data) => { 89 if (err) { 90 console.error('Failed to save the screenshot. Error: ' + JSON.stringify(err)); 91 return; 92 } 93 console.info('Screenshot saved. Data: ' + JSON.stringify(data)); 94 }); 95 ``` 96 97## screenshot.save 98 99save(options?: ScreenshotOptions): Promise<image.PixelMap> 100 101获取屏幕截图。 102 103**系统能力:** SystemCapability.WindowManager.WindowManager.Core 104 105**需要权限**:ohos.permission.CAPTURE_SCREEN,仅系统应用可用。 106 107**参数:** 108 109| 参数名 | 类型 | 必填 | 说明 | 110| ------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 111| options | [ScreenshotOptions](#screenshotoptions) | 否 | 该类型的参数包含screenRect、imageSize、rotation、displayId四个参数,可以分别设置这四个参数。 | 112 113**返回值:** 114 115| 类型 | 说明 | 116| ----------------------------- | ----------------------------------------------- | 117| Promise<image.PixelMap> | Promise对象。返回一个PixelMap对象。 | 118 119**示例:** 120 121 ```js 122 var ScreenshotOptions = { 123 "screenRect": { 124 "left": 200, 125 "top": 100, 126 "width": 200, 127 "height": 200}, 128 "imageSize": { 129 "width": 300, 130 "height": 300}, 131 "rotation": 0, 132 "displayId": 0 133 }; 134 let promise = screenshot.save(ScreenshotOptions); 135 promise.then(() => { 136 console.log('screenshot save success'); 137 }).catch((err) => { 138 console.log('screenshot save fail: ' + JSON.stringify(err)); 139 }); 140 ``` 141