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 | 是 | 表示截取图像区域的左边界,单位为px,该参数应为整数。 | 41| top | number | 是 | 表示截取图像区域的上边界,单位为px,该参数应为整数。 | 42| width | number | 是 | 表示截取图像区域的宽度,单位为px,该参数应为整数。 | 43| height | number | 是 | 表示截取图像区域的高度,单位为px,该参数应为整数。 | 44 45 46## Size 47 48表示截取图像的大小。 49 50**系统能力:** SystemCapability.WindowManager.WindowManager.Core 51 52| 名称 | 类型 | 必填 | 说明 | 53| ------ | ------ | ---- | ------------------------------------------------------------ | 54| width | number | 是 | 表示截取图像的宽度,单位为px,该参数应为整数。 | 55| height | number | 是 | 表示截取图像的高度,单位为px,该参数应为整数。 | 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'; 78import image from '@ohos.multimedia.image'; 79 80let screenshotOptions: screenshot.ScreenshotOptions = { 81 "screenRect": { 82 "left": 200, 83 "top": 100, 84 "width": 200, 85 "height": 200 }, 86 "imageSize": { 87 "width": 300, 88 "height": 300 }, 89 "rotation": 0, 90 "displayId": 0 91}; 92try { 93 screenshot.save(screenshotOptions, (err: BusinessError, pixelMap: image.PixelMap) => { 94 if (err) { 95 console.log('Failed to save screenshot. Code: ' + JSON.stringify(err)); 96 return; 97 } 98 console.log('Succeeded in saving screenshot. Pixel bytes number: ' + pixelMap.getPixelBytesNumber()); 99 pixelMap.release(); // PixelMap使用完后及时释放内存 100 }); 101} catch (exception) { 102 console.error('Failed to save screenshot. Code: ' + JSON.stringify(exception)); 103}; 104``` 105 106## screenshot.save 107 108save(callback: AsyncCallback<image.PixelMap>): void 109 110获取屏幕截图。 111 112**系统能力:** SystemCapability.WindowManager.WindowManager.Core 113 114**需要权限**:ohos.permission.CAPTURE_SCREEN,仅系统应用可用。 115 116**参数:** 117 118| 参数名 | 类型 | 必填 | 说明 | 119| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 120| callback | AsyncCallback<[image.PixelMap](js-apis-image.md#pixelmap7)> | 是 | 回调函数。返回一个PixelMap对象。 | 121 122**示例:** 123 124```ts 125import { BusinessError } from '@ohos.base'; 126import image from '@ohos.multimedia.image'; 127 128try { 129 screenshot.save((err: BusinessError, pixelMap: image.PixelMap) => { 130 if (err) { 131 console.log('Failed to save screenshot. Code: ' + JSON.stringify(err)); 132 return; 133 } 134 console.log('Succeeded in saving screenshot. Pixel bytes number: ' + pixelMap.getPixelBytesNumber()); 135 pixelMap.release(); // PixelMap使用完后及时释放内存 136 }); 137} catch (exception) { 138 console.error('Failed to save screenshot. Code: ' + JSON.stringify(exception)); 139}; 140``` 141 142## screenshot.save 143 144save(options?: ScreenshotOptions): Promise<image.PixelMap> 145 146获取屏幕截图。 147 148**系统能力:** SystemCapability.WindowManager.WindowManager.Core 149 150**需要权限**:ohos.permission.CAPTURE_SCREEN,仅系统应用可用。 151 152**参数:** 153 154| 参数名 | 类型 | 必填 | 说明 | 155| ------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 156| options | [ScreenshotOptions](#screenshotoptions) | 否 | 该类型的参数包含screenRect、imageSize、rotation、displayId四个参数,可以分别设置这四个参数。 | 157 158**返回值:** 159 160| 类型 | 说明 | 161| ----------------------------- | ----------------------------------------------- | 162| Promise<[image.PixelMap](js-apis-image.md#pixelmap7)> | Promise对象。返回一个PixelMap对象。 | 163 164**示例:** 165 166```ts 167import { BusinessError } from '@ohos.base'; 168import image from '@ohos.multimedia.image'; 169 170let screenshotOptions: screenshot.ScreenshotOptions = { 171 "screenRect": { 172 "left": 200, 173 "top": 100, 174 "width": 200, 175 "height": 200 }, 176 "imageSize": { 177 "width": 300, 178 "height": 300 }, 179 "rotation": 0, 180 "displayId": 0 181}; 182try { 183 let promise = screenshot.save(screenshotOptions); 184 promise.then((pixelMap: image.PixelMap) => { 185 console.log('Succeeded in saving screenshot. Pixel bytes number: ' + pixelMap.getPixelBytesNumber()); 186 pixelMap.release(); // PixelMap使用完后及时释放内存 187 }).catch((err: BusinessError) => { 188 console.log('Failed to save screenshot. Code: ' + JSON.stringify(err)); 189 }); 190} catch (exception) { 191 console.error('Failed to save screenshot. Code: ' + JSON.stringify(exception)); 192}; 193``` 194