harmony 鸿蒙@ohos.screenshot (屏幕截图)

2022-08-09 浏览 (1404)

@ohos.screenshot (屏幕截图)

本模块提供屏幕截图的能力,截取屏幕时支持设置截取的区域、大小等图像信息。

说明:

本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

该模块接口为系统接口。

导入模块

import screenshot from '@ohos.screenshot';

ScreenshotOptions

设置截取图像的信息。

系统能力: SystemCapability.WindowManager.WindowManager.Core

名称类型必填说明
screenRectRect表示截取图像的区域,不传值默认为全屏。
imageSizeSize表示截取图像的大小,不传值默认为全屏。
rotationnumber表示截取图像的旋转角度,当前仅支持输入值为0,默认值为0,该参数应为整数。
displayId8+number表示截取图像的显示设备Display的ID号,该参数应为整数。

Rect

表示截取图像的区域。

系统能力: SystemCapability.WindowManager.WindowManager.Core

名称类型必填说明
leftnumber表示截取图像区域的左边界,单位为像素,该参数应为整数。
topnumber表示截取图像区域的上边界,单位为像素,该参数应为整数。
widthnumber表示截取图像区域的宽度,单位为像素,该参数应为整数。
heightnumber表示截取图像区域的高度,单位为像素,该参数应为整数。

Size

表示截取图像的大小。

系统能力: SystemCapability.WindowManager.WindowManager.Core

名称类型必填说明
widthnumber表示截取图像的宽度,单位为像素,该参数应为整数。
heightnumber表示截取图像的高度,单位为像素,该参数应为整数。

screenshot.save

save(options: ScreenshotOptions, callback: AsyncCallback<image.PixelMap>): void

获取屏幕截图。

系统能力: SystemCapability.WindowManager.WindowManager.Core

需要权限:ohos.permission.CAPTURE_SCREEN,仅系统应用可用。

参数:

参数名类型必填说明
optionsScreenshotOptions该类型的参数包含screenRect、imageSize、rotation、displayId四个参数,可以分别设置这四个参数。
callbackAsyncCallback<image.PixelMap>回调函数。返回一个PixelMap对象。

示例:

import { BusinessError } from '@ohos.base';

let screenshotOptions: screenshot.ScreenshotOptions = {
  "screenRect": {
    "left": 200,
    "top": 100,
    "width": 200,
    "height": 200 },
  "imageSize": {
    "width": 300,
    "height": 300 },
  "rotation": 0,
  "displayId": 0
};
try {
  screenshot.save(screenshotOptions, (err: BusinessError, pixelMap) => {
    if (err) {
      console.log('Failed to save screenshot. Code: ' + JSON.stringify(err));
      return;
    }
    console.log('Succeeded in saving sreenshot. Pixel bytes number: ' + pixelMap.getPixelBytesNumber());
    pixelMap.release(); // PixelMap使用完后及时释放内存
  });
} catch (exception) {
  console.error('Failed to save screenshot. Code: ' + JSON.stringify(exception));
};

screenshot.save

save(callback: AsyncCallback<image.PixelMap>): void

获取屏幕截图。

系统能力: SystemCapability.WindowManager.WindowManager.Core

需要权限:ohos.permission.CAPTURE_SCREEN,仅系统应用可用。

参数:

参数名类型必填说明
callbackAsyncCallback<image.PixelMap>回调函数。返回一个PixelMap对象。

示例:

import { BusinessError } from '@ohos.base';

try {
  screenshot.save((err: BusinessError, pixelMap) => {
    if (err) {
      console.log('Failed to save screenshot. Code: ' + JSON.stringify(err));
      return;
    }
    console.log('Succeeded in saving sreenshot. Pixel bytes number: ' + pixelMap.getPixelBytesNumber());
    pixelMap.release(); // PixelMap使用完后及时释放内存
  });
} catch (exception) {
  console.error('Failed to save screenshot. Code: ' + JSON.stringify(exception));
};

screenshot.save

save(options?: ScreenshotOptions): Promise<image.PixelMap>

获取屏幕截图。

系统能力: SystemCapability.WindowManager.WindowManager.Core

需要权限:ohos.permission.CAPTURE_SCREEN,仅系统应用可用。

参数:

参数名类型必填说明
optionsScreenshotOptions该类型的参数包含screenRect、imageSize、rotation、displayId四个参数,可以分别设置这四个参数。

返回值:

类型说明
Promise<image.PixelMap>Promise对象。返回一个PixelMap对象。

示例:

import { BusinessError } from '@ohos.base';

let screenshotOptions: screenshot.ScreenshotOptions = {
  "screenRect": {
    "left": 200,
    "top": 100,
    "width": 200,
    "height": 200 },
  "imageSize": {
    "width": 300,
    "height": 300 },
  "rotation": 0,
  "displayId": 0
};
try {
  let promise = screenshot.save(screenshotOptions);
  promise.then((pixelMap) => {
    console.log('Succeeded in saving sreenshot. Pixel bytes number: ' + pixelMap.getPixelBytesNumber());
    pixelMap.release(); // PixelMap使用完后及时释放内存
  }).catch((err: BusinessError) => {
    console.log('Failed to save screenshot. Code: ' + JSON.stringify(err));
  });
} catch (exception) {
  console.error('Failed to save screenshot. Code: ' + JSON.stringify(exception));
};

你可能感兴趣的鸿蒙文章

harmony 鸿蒙接口

harmony 鸿蒙系统公共事件定义(待停用)

harmony 鸿蒙系统公共事件定义

harmony 鸿蒙开发说明

harmony 鸿蒙企业设备管理概述(仅对系统应用开放)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager模块)

harmony 鸿蒙@ohos.distributedBundle (分布式包管理)

harmony 鸿蒙@ohos.bundle (Bundle模块)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (企业设备管理扩展能力)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/bf0b4889e33c423c96d51012827d1545