1# @ohos.screenshot (屏幕截图) 2 3本模块提供屏幕截图的能力,截取屏幕时支持设置截取的区域、大小等图像信息。 4 5> **说明:** 6> 7> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 9> 该模块接口为系统接口。 10 11## 导入模块 12 13```ts 14import screenshot from '@ohos.screenshot'; 15``` 16 17## ScreenshotOptions 18 19设置截取图像的信息。 20 21**系统能力:** SystemCapability.WindowManager.WindowManager.Core 22 23 24| 名称 | 类型 | 必填 | 说明 | 25| ---------------------- | ------------- | ---- | ------------------------------------------------------------ | 26| screenRect | [Rect](#rect) | 否 | 表示截取图像的区域,不传值默认为全屏。 | 27| imageSize | [Size](#size) | 否 | 表示截取图像的大小,不传值默认为全屏。 | 28| rotation | number | 否 | 表示截取图像的旋转角度,当前仅支持输入值为0,默认值为0,该参数应为整数。 | 29| displayId<sup>8+</sup> | number | 否 | 表示截取图像的显示设备[Display](js-apis-display.md#display)的ID号,该参数应为整数。 | 30 31 32## Rect 33 34表示截取图像的区域。 35 36**系统能力:** SystemCapability.WindowManager.WindowManager.Core 37 38| 名称 | 类型 | 必填 | 说明 | 39| ------ | ------ | ---- | ------------------------------------------------------------ | 40| left | number | 是 | 表示截取图像区域的左边界,单位为像素,该参数应为整数。 | 41| top | number | 是 | 表示截取图像区域的上边界,单位为像素,该参数应为整数。 | 42| width | number | 是 | 表示截取图像区域的宽度,单位为像素,该参数应为整数。 | 43| height | number | 是 | 表示截取图像区域的高度,单位为像素,该参数应为整数。 | 44 45 46## Size 47 48表示截取图像的大小。 49 50**系统能力:** SystemCapability.WindowManager.WindowManager.Core 51 52| 名称 | 类型 | 必填 | 说明 | 53| ------ | ------ | ---- | ------------------------------------------------------------ | 54| width | number | 是 | 表示截取图像的宽度,单位为像素,该参数应为整数。 | 55| height | number | 是 | 表示截取图像的高度,单位为像素,该参数应为整数。 | 56 57## screenshot.save 58 59save(options: ScreenshotOptions, callback: AsyncCallback<image.PixelMap>): void 60 61获取屏幕截图。 62 63**系统能力:** SystemCapability.WindowManager.WindowManager.Core 64 65**需要权限**:ohos.permission.CAPTURE_SCREEN,仅系统应用可用。 66 67**参数:** 68 69| 参数名 | 类型 | 必填 | 说明 | 70| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 71| options | [ScreenshotOptions](#screenshotoptions) | 是 | 该类型的参数包含screenRect、imageSize、rotation、displayId四个参数,可以分别设置这四个参数。 | 72| callback | AsyncCallback<[image.PixelMap](js-apis-image.md#pixelmap7)> | 是 | 回调函数。返回一个PixelMap对象。 | 73 74**示例:** 75 76```ts 77import { BusinessError } from '@ohos.base'; 78 79let screenshotOptions: screenshot.ScreenshotOptions = { 80 "screenRect": { 81 "left": 200, 82 "top": 100, 83 "width": 200, 84 "height": 200 }, 85 "imageSize": { 86 "width": 300, 87 "height": 300 }, 88 "rotation": 0, 89 "displayId": 0 90}; 91try { 92 screenshot.save(screenshotOptions, (err: BusinessError, pixelMap) => { 93 if (err) { 94 console.log('Failed to save screenshot. Code: ' + JSON.stringify(err)); 95 return; 96 } 97 console.log('Succeeded in saving sreenshot. Pixel bytes number: ' + pixelMap.getPixelBytesNumber()); 98 pixelMap.release(); // PixelMap使用完后及时释放内存 99 }); 100} catch (exception) { 101 console.error('Failed to save screenshot. Code: ' + JSON.stringify(exception)); 102}; 103``` 104 105## screenshot.save 106 107save(callback: AsyncCallback<image.PixelMap>): void 108 109获取屏幕截图。 110 111**系统能力:** SystemCapability.WindowManager.WindowManager.Core 112 113**需要权限**:ohos.permission.CAPTURE_SCREEN,仅系统应用可用。 114 115**参数:** 116 117| 参数名 | 类型 | 必填 | 说明 | 118| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 119| callback | AsyncCallback<[image.PixelMap](js-apis-image.md#pixelmap7)> | 是 | 回调函数。返回一个PixelMap对象。 | 120 121**示例:** 122 123```ts 124import { BusinessError } from '@ohos.base'; 125 126try { 127 screenshot.save((err: BusinessError, pixelMap) => { 128 if (err) { 129 console.log('Failed to save screenshot. Code: ' + JSON.stringify(err)); 130 return; 131 } 132 console.log('Succeeded in saving sreenshot. Pixel bytes number: ' + pixelMap.getPixelBytesNumber()); 133 pixelMap.release(); // PixelMap使用完后及时释放内存 134 }); 135} catch (exception) { 136 console.error('Failed to save screenshot. Code: ' + JSON.stringify(exception)); 137}; 138``` 139 140## screenshot.save 141 142save(options?: ScreenshotOptions): Promise<image.PixelMap> 143 144获取屏幕截图。 145 146**系统能力:** SystemCapability.WindowManager.WindowManager.Core 147 148**需要权限**:ohos.permission.CAPTURE_SCREEN,仅系统应用可用。 149 150**参数:** 151 152| 参数名 | 类型 | 必填 | 说明 | 153| ------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 154| options | [ScreenshotOptions](#screenshotoptions) | 否 | 该类型的参数包含screenRect、imageSize、rotation、displayId四个参数,可以分别设置这四个参数。 | 155 156**返回值:** 157 158| 类型 | 说明 | 159| ----------------------------- | ----------------------------------------------- | 160| Promise<[image.PixelMap](js-apis-image.md#pixelmap7)> | Promise对象。返回一个PixelMap对象。 | 161 162**示例:** 163 164```ts 165import { BusinessError } from '@ohos.base'; 166 167let screenshotOptions: screenshot.ScreenshotOptions = { 168 "screenRect": { 169 "left": 200, 170 "top": 100, 171 "width": 200, 172 "height": 200 }, 173 "imageSize": { 174 "width": 300, 175 "height": 300 }, 176 "rotation": 0, 177 "displayId": 0 178}; 179try { 180 let promise = screenshot.save(screenshotOptions); 181 promise.then((pixelMap) => { 182 console.log('Succeeded in saving sreenshot. Pixel bytes number: ' + pixelMap.getPixelBytesNumber()); 183 pixelMap.release(); // PixelMap使用完后及时释放内存 184 }).catch((err: BusinessError) => { 185 console.log('Failed to save screenshot. Code: ' + JSON.stringify(err)); 186 }); 187} catch (exception) { 188 console.error('Failed to save screenshot. Code: ' + JSON.stringify(exception)); 189}; 190``` 191