vue js-web-screen-shot浏览器截取其他非全屏窗口界面

news/2025/2/25 9:38:01

网页截屏 js-web-screen-shot 截取其他窗口 显示不全问题

npm 安装 js-web-screen-shot

javascript">npm install js-web-screen-shot --save

js-web-screen-shot默认截屏是从左下角开始的,修改成左上角开始,然后编辑cropBoxInfo参数宽高进行截取,目前截取适配的其他窗口宽高不能比浏览器页面宽高大

vue代码

javascript">import ScreenShot from 'js-web-screen-shot'

new ScreenShot({
                enableWebRtc: true,
                level: 99999,
                wrcWindowMode: true,
                clickCutFullScreen: true,
                imgAutoFit: true,
                showScreenData: true,
                completeCallback: this.callback, // 截图成功完成的回调
                closeCallback: this.cancelCallback, // 截图取消的回调
                hiddenScrollBar: {
                    state: true,
                    fillState: true,
                    color: "#FFFFFF"
                },
                // cropBoxInfo: {x: 0, y: 0, w: 1757, h: 1011},
                cropBoxInfo: {x: 0, y: 0, w: 1290, h: 640},
                // canvasWidth: window.innerWidth,
                // canvasHeight: window.innerHeight,
            })

修改js-web-screen-shot代码

main.ts文件

javascript">private loadScreenFlowData(triggerCallback: Function | undefined) {
    setTimeout(() => {
      // 获取截图区域canvas容器画布
      if (this.screenShotContainer == null) return;
      const canvasSize = this.plugInParameters.getCanvasSize();
      let containerWidth = this.screenShotImageController?.width;
      let containerHeight = this.screenShotImageController?.height;
      // 用户有传宽高时,则使用用户的
      if (canvasSize.canvasWidth !== 0 && canvasSize.canvasHeight !== 0) {
        containerWidth = canvasSize.canvasWidth;
        containerHeight = canvasSize.canvasHeight;
      }
      let imgContainerWidth = containerWidth;
      let imgContainerHeight = containerHeight;
      if (this.wrcWindowMode) {
        imgContainerWidth = containerWidth * this.dpr;
        imgContainerHeight = containerHeight * this.dpr;
      }
      const context = getCanvas2dCtx(
        this.screenShotContainer,
        containerWidth,
        containerHeight
      );
      const imgContext = getCanvas2dCtx(
        this.screenShotImageController,
        imgContainerWidth,
        imgContainerHeight
      );
      if (context == null || imgContext == null) return;
      // 赋值截图区域canvas画布
      this.screenShotCanvas = context;
      const { videoWidth, videoHeight } = this.videoController;
      if (this.wrcWindowMode) {
        // 从窗口视频流中获取body内容
        const bodyImgData = this.getWindowContentData(
          videoWidth,
          videoHeight,
          containerWidth * this.dpr,
          containerHeight * this.dpr
        );
        if (bodyImgData == null) return;
        // 将body内容绘制到图片容器里
        imgContext.putImageData(bodyImgData, 0, 0);
      } else {
        // 对webrtc源提供的图像宽高进行修复
        let fixWidth = containerWidth;
        let fixHeight = (videoHeight * containerWidth) / videoWidth;
        if (fixHeight > containerHeight) {
          fixWidth = (containerWidth * containerHeight) / fixHeight;
          fixHeight = containerHeight;
        }
        // 对视频容器的内容进行裁剪
        fixWidth = this.wrcImgPosition.w > 0 ? this.wrcImgPosition.w : fixWidth;
        fixHeight =
          this.wrcImgPosition.h > 0 ? this.wrcImgPosition.h : fixHeight;
        imgContext?.drawImage(
          this.videoController,
          this.wrcImgPosition.x,
          this.wrcImgPosition.y,
          fixWidth,
          fixHeight
        );
        // 隐藏滚动条会出现部分内容未截取到,需要进行修复
        const diffHeight = containerHeight - fixHeight;
        if (
          this.hiddenScrollBar.state &&
          diffHeight > 0 &&
          this.hiddenScrollBar.fillState
        ) {
          // 填充容器的剩余部分
          imgContext.beginPath();
          let fillWidth = containerWidth;
          let fillHeight = diffHeight;
          if (this.hiddenScrollBar.fillWidth > 0) {
            fillWidth = this.hiddenScrollBar.fillWidth;
          }
          if (this.hiddenScrollBar.fillHeight > 0) {
            fillHeight = this.hiddenScrollBar.fillHeight;
          }
          imgContext.rect(0, fixHeight, fillWidth, fillHeight);
          imgContext.fillStyle = this.hiddenScrollBar.color;
          imgContext.fill();
        }
      }

      // 初始化截图容器
      this.initScreenShot(undefined, context, this.screenShotImageController);
      let displaySurface = null;
      let displayLabel = null;
      if (this.captureStream) {
        // 获取当前选择的窗口类型
        displaySurface = this.captureStream.getVideoTracks()[0].getSettings()
          ?.displaySurface;
        // 获取当前选择的标签页标识
        displayLabel = this.captureStream.getVideoTracks()[0].label;
      }
      // 执行截图成功回调
      if (triggerCallback) {
        triggerCallback({
          code: 0,
          msg: "截图加载完成",
          displaySurface,
          displayLabel
        });
      }
      // 停止捕捉屏幕
      this.stopCapture();
      // 重置光标状态
      document.body.classList.remove("no-cursor");
    }, this.wrcReplyTime);
  }


http://www.niftyadmin.cn/n/5865330.html

相关文章

计算机网络与通讯知识总结

计算机网络与通讯知识总结 基础知识总结 1)FTP:文件传输 SSH:远程登录 HTTP:网址访问 2)‌交换机 定义‌:一种基于MAC地址实现局域网(LAN)内数据高速转发的网络设备,可为接入设备提供独享通信通道‌。 -‌ 核心功能‌: 1.数据链路层(OSI第二层)工作,通过MAC地址…

Spring高级篇-Spring IOC容器 Aware 接口

一、概述 在Spring框架中,IOC(Inversion of Control)容器负责管理应用程序中的对象(即Bean)的生命周期和依赖关系。Spring提供了一系列的Aware接口,允许Bean在初始化时获取Spring容器中的某些资源或信息。…

Starlink卫星动力学系统仿真建模第十讲-基于SMC和四元数的卫星姿态控制示例及Python实现

基于四元数与滑模控制的卫星姿态控制 一、基本原理 1. 四元数姿态表示 四元数运动学方程: 3. 滑模控制设计 二、代码实现(Python) 1. 四元数运算工具 import numpy as npdef quat_mult(q1, q2):"""四元数乘法""…

实现实时数据仓库开源项目

根据你的需求,以下是一些可以实现类似 ClickHouse 的实时数仓功能的项目,这些项目提供了高性能的数据处理和分析能力,适合实时数据仓库的场景: 1. Apache Doris Apache Doris 是一个开源的实时数据仓库,支持高吞吐量…

模版语法vscode

这里注意&#xff1a;<template></template>里面只能写一个根标签&#xff0c;其他在嵌套&#xff1a; <script > export default {data(){return{tthtml:"<a hrefhttps://itbaizhan.com>百战程序员</a>"}} } </script><tem…

Spark ,虚拟机(Linux)基本命令(2)

cp 命令&#xff0c;复制 格式&#xff1a;cp 源文件 目标文件 cp -r 命令 复制目录 格式&#xff1a;cp -r 源文件 目标文件夹 mv 命令&#xff0c;重命名和移动 格式&#xff1a;mv 源文件 目标文件 说明&#xff1a;若源文件和目标文件在同一个目录下&#xff0c;就是重…

本地开发用ASP.NET Core Web API项目创建及测试

1. 服务端代码&#xff08;C#&#xff09; 1.1 创建ASP.NET Core Web API项目 打开Visual Studio 2022。 选择“创建新项目”。 选择“ASP.NET Core Web API”模板&#xff0c;点击“下一步”。 输入项目名称&#xff08;如OracleApi&#xff09;&#xff0c;选择项目位置&…

AI赋能游戏前端:效率革命与沉浸式体验的未来

游戏行业正经历一场深刻的变革&#xff0c;玩家对游戏体验的要求越来越高&#xff0c;追求更逼真、更沉浸式的游戏世界。为了满足玩家日益增长的需求&#xff0c;游戏开发者们需要不断提升开发效率和品质。而AI写代码工具的出现&#xff0c;正为游戏前端开发带来一场效率革命&a…