【本文正在参加 2023「盲盒」+码有奖征文活动】 https://ost.51cto.com/posts/25284
简介
分布式屏幕功能早在OpenHarmony-3.1-Release就已经发布,详情可参考官方仓库的介绍。
本示例基于OpenHarmony自带的分布式屏幕相关API,并简单实现了屏幕镜像(或扩展)投屏相关应用功能。示例主要分为ArkTS版本和C++版本,ArkTS版本目前主要基于API9版本分布式屏幕功能的API进行开发,API详细说明可参考:@ohos.screen。开发者可自行基于API10开发,其中API10版本增加了停止屏幕镜像(或扩展)的API。
源码下载
源码详细说明及下载可参考
效果预览
使用说明
应用界面中展示可以点击镜像投屏或扩展投屏,为了添加分布式设备,可以在菜单中进入设备管理页面进行添加。此外,开发者可创建虚拟屏幕,在本机预览投屏后的效果(开启虚拟屏幕会增加系统负荷)。
工程目录
具体实现
ArkTS实现
通过在IDE中创建工程,并调用@ohos.screen中相关API进行实现,主要实现如下:
主要使用到@ohos.screen中的相关API如下:
方法名称 |
描述 |
screen.getAllScreens |
获取所有的屏幕,可以是本地屏幕和其他设备的屏幕 |
screen.makeMirror |
将屏幕设置为镜像模式 |
screen.makeExpand |
将屏幕设置为扩展模式 |
C/C++实现
基于分布式屏幕的C/C++相关API实现一个简单的程序,并增加BUILD.gn用于开发者自行编译验证参考。
说明:源码及BUILD.gn在OpenHarmony-3.2-Release版本有验证,OpenHarmony-4.x版本,需要修改BUILD.gn
参考源码和BUILD.gn内容如下。
- dscreen_mirror.cpp
- dscreen_expand.cpp
-
BUILD.gn(仅供参考)
import("//build/ohos.gni")
import("//build/ohos_var.gni")
import("//foundation/distributedhardware/distributed_screen/distributedscreen.gni")
ohos_executable("dscreen_mirror") {
sources = ["src/dscreen_mirror.cpp"]
include_dirs = [
".",
"//foundation/distributedhardware/distributed_screen/interfaces/innerkits/native_cpp/screen_source/include",
"//foundation/distributedhardware/distributed_screen/interfaces/innerkits/native_cpp/screen_source/include/callback",
"//foundation/distributedhardware/distributed_hardware_fwk/common/utils/include",
"//foundation/distributedhardware/distributed_screen/interfaces/innerkits/native_cpp/screen_sink/include",
"//foundation/distributedhardware/distributed_screen/common/include/",
"//foundation/distributedhardware/distributed_screen/services/screenclient/include/",
]
cflags = [
"-Wall",
"-Werror",
"-Wno-cast-qual",
"-Wno-pointer-arith",
"-Wno-unused-parameter",
"-Wno-unused-variable",
"-Wno-delete-incomplete",
]
deps = [
"${common_path}:distributed_screen_utils",
"${interfaces_path}/innerkits/native_cpp/screen_sink:distributed_screen_sink_sdk",
"${interfaces_path}/innerkits/native_cpp/screen_source:distributed_screen_source_sdk",
"${services_path}/screenclient:distributed_screen_client",
"${windowmanager_path}/dm:libdm",
"${windowmanager_path}/wm:libwm",
"//base/security/access_token/interfaces/innerkits/nativetoken:libnativetoken",
"//base/security/access_token/interfaces/innerkits/token_setproc:libtoken_setproc",
"//foundation/communication/dsoftbus/adapter:softbus_adapter",
"//foundation/communication/dsoftbus/sdk:softbus_client",
"//foundation/graphic/graphic_2d:libsurface",
"//foundation/graphic/graphic_2d/rosen/modules/2d_graphics:2d_graphics",
"//foundation/graphic/graphic_2d/rosen/modules/render_service_base/src/platform:platform",
"//foundation/graphic/graphic_2d/rosen/modules/render_service_client:librender_service_client",
"//foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy",
]
external_deps = [
"hiviewdfx_hilog_native:libhilog",
"multimedia_player_framework:media_client",
]
install_enable = true
install_images = [ "vendor" ]
part_name = "rockchip_products" # 根据实际编译的产品调整part_name
}
ohos_executable("dscreen_expand") {
sources = ["src/dscreen_expand.cpp"]
include_dirs = [
".",
"//foundation/distributedhardware/distributed_screen/interfaces/innerkits/native_cpp/screen_source/include",
"//foundation/distributedhardware/distributed_screen/interfaces/innerkits/native_cpp/screen_source/include/callback",
"//foundation/distributedhardware/distributed_hardware_fwk/common/utils/include",
"//foundation/distributedhardware/distributed_screen/interfaces/innerkits/native_cpp/screen_sink/include",
"//foundation/distributedhardware/distributed_screen/common/include/",
"//foundation/distributedhardware/distributed_screen/services/screenclient/include/",
]
cflags = [
"-Wall",
"-Werror",
"-Wno-cast-qual",
"-Wno-pointer-arith",
"-Wno-unused-parameter",
"-Wno-unused-variable",
"-Wno-delete-incomplete",
]
deps = [
"${common_path}:distributed_screen_utils",
"${interfaces_path}/innerkits/native_cpp/screen_sink:distributed_screen_sink_sdk",
"${interfaces_path}/innerkits/native_cpp/screen_source:distributed_screen_source_sdk",
"${services_path}/screenclient:distributed_screen_client",
"${windowmanager_path}/dm:libdm",
"${windowmanager_path}/wm:libwm",
"//base/security/access_token/interfaces/innerkits/nativetoken:libnativetoken",
"//base/security/access_token/interfaces/innerkits/token_setproc:libtoken_setproc",
"//foundation/communication/dsoftbus/adapter:softbus_adapter",
"//foundation/communication/dsoftbus/sdk:softbus_client",
"//foundation/graphic/graphic_2d:libsurface",
"//foundation/graphic/graphic_2d/rosen/modules/2d_graphics:2d_graphics",
"//foundation/graphic/graphic_2d/rosen/modules/render_service_base/src/platform:platform",
"//foundation/graphic/graphic_2d/rosen/modules/render_service_client:librender_service_client",
"//foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy",
]
external_deps = [
"hiviewdfx_hilog_native:libhilog",
"multimedia_player_framework:media_client",
]
install_enable = true
install_images = [ "vendor" ]
part_name = "rockchip_products" # 根据实际编译的产品调整part_name
}
group("distributed_screen") {
deps = [
":dscreen_mirror",
":dscreen_expand",
]
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 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.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
相关权限
默认添加以下权限
权限名称 |
描述 |
ohos.permission.CAPTURE_SCREEN |
允许应用截取屏幕图像 |
ohos.permission.DISTRIBUTED_DATASYNC |
允许不同设备间的数据交换 |
ohos.permission.GET_BUNDLE_INFO |
允许应用查询其他应用的信息。该权限仅适用于三方应用。 |
ohos.permission.INTERNET |
允许使用Internet网络 |
说明:应用需采用系统签名,部分权限可根据是否使用相关模块进行删除
视频演示
【OpenHarmony分布式屏幕,屏幕镜像功能体验】 https://www.bilibili.com/video/BV1dr4y1R74p/?share_source=copy_web&vd_source=5130b770dd48ecf758ca620cb4b9e34b