您的位置:首页 > 新闻 > 资讯 > 【HarmonyOS】鸿蒙应用实现截屏

【HarmonyOS】鸿蒙应用实现截屏

2025/8/2 6:14:58 来源:https://blog.csdn.net/u010949451/article/details/140936296  浏览:    关键词:【HarmonyOS】鸿蒙应用实现截屏

【HarmonyOS】鸿蒙应用实现截屏

组件截屏

在这里插入图片描述
通过componentSnapshot的get函数,将需要截图的组件设置id传进去即可。

import { componentSnapshot } from '@kit.ArkUI';
import { image } from '@kit.ImageKit';/*** 截图*/



struct SnapShotPage { mImageSnapShot: image.PixelMap | null = null; ImgStyle(){.size({width: px2vp(350),height: px2vp(350)})}build() {Column(){Blank()// 参照图片Image($r("app.media.icon_img")).ImgStyle().id("target").autoResize(true)Blank()// 展示截图Image(this.mImageSnapShot).ImgStyle().backgroundColor(Color.Blue)Blank()Button("点击截图参照图片控件").onClick(()=>{componentSnapshot.get("target", (error: Error, pixmap: image.PixelMap)=>{if (error) {console.log("SnapShotDebug", "error: " + JSON.stringify(error));return;}console.log("SnapShotDebug", "pixmap: " + JSON.stringify(pixmap));this.mImageSnapShot = pixmap;});})Blank()}.size({width: "100%",height: "100%"})}
}

示例效果请以真机运行为准,当前 IDE 预览器不支持。

流媒体截屏

对于使用XComponent的场景,例如:Video或者相机流媒体展示类组件,不建议使用组件截图相关接口,建议从surface直接获取图片。效率更高些。

  private cutImage(surfaceId: string){let region: image.Region = { x: 0, y: 0, size: { height: 100, width: 100 } };image.createPixelMapFromSurface(surfaceId, region).then(() => {console.info("SnapShotDebug", 'Succeeded in creating pixelmap from Surface');}).catch((error: BusinessError) => {console.error("SnapShotDebug", `Failed to create pixelmap. code is ${error.code}, message is ${error.message}`);});}

关于如何屏蔽截屏行为,参见这篇文章。设置当前需要屏蔽截屏业务的容器窗口为,隐私窗口即可。【HarmonyOS】应用屏蔽截屏和录屏

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com