nashaofu / screenshots

A screenshot plugin for electron and react
https://nashaofu.github.io/screenshots/
MIT License
389 stars 98 forks source link

截图不清楚,建议用 getUserMedia 而不是 thumbnail 获取截图 #42

Closed WangYuLue closed 2 years ago

WangYuLue commented 4 years ago

packages/react-screenshots/src/electron/app.js

getSource = (e, display) => {
    getSource(display).then(({ thumbnail }) => {
      // 捕捉完桌面后通知主进程
      ipcRenderer.send('SCREENSHOTS::CAPTURED')
      this.setState({ image: thumbnail.toDataURL() })
    })
}

改造成:


  handleStream = (stream, callback) => {
    document.body.style.opacity = '1'
    // 创建隐藏 video 元素
    const video = document.createElement('video')
    video.autoplay = true
    video.style.cssText = 'position:absolute;top:-10000px;left:-10000px;'
    let loaded = false
    video.onplaying = () => {
      if (loaded) return
      loaded = true
      video.style.height = video.videoHeight + 'px'
      video.style.width = video.videoWidth + 'px'

      const canvas = document.createElement('canvas')
      canvas.width = video.videoWidth
      canvas.height = video.videoHeight
      const ctx = canvas.getContext('2d')
      if (!ctx) return
      ctx.drawImage(video, 0, 0, canvas.width, canvas.height)

      if (callback) {
        callback(canvas.toDataURL('image/png'))
      }

      video.remove()
      try {
        stream.getTracks()[0].stop()
      } catch (e) {
        console.error('getTracks error')
      }
    }
    // 将流传给 video
    video.srcObject = stream
    document.body.appendChild(video)
  };

  getSource = (e, display) => {
    getSource(display).then((source) => {
      if (!source) {
        console.error('未找到屏幕源')
        return
      }
      const config = {
        audio: false,
        video: {
          mandatory: {
            chromeMediaSource: 'desktop',
            chromeMediaSourceId: source.id,
            minWidth: 1280,
            minHeight: 720,
            maxWidth: 8000,
            maxHeight: 8000
          }
        }
      }
      navigator.mediaDevices.getUserMedia(config).then((e) => {
        this.handleStream(e, (dataurl) => {
          ipcRenderer.send('SCREENSHOTS::CAPTURED')
          this.setState({ image: dataurl })
        })
      }).catch(e => {
        console.error('屏幕捕获异常', e)
      })
    })
  }

上面的代码没有运行验证,但是思路大概是这样

nashaofu commented 4 years ago

有时间我看一下

bruceauyeung commented 3 years ago

@nashaofu 当前的代码在windows 7上截图清晰,但是在linux上就很模糊,这个问题有进展吗? 谢谢! ps:electron版本

banyueyang commented 3 years ago

我这个在mac上打开截图也很模糊,怎么才能加入你们呢,想尝试着改一下

nashaofu commented 3 years ago

你直接fork项目,然后开发完成后提pr申请合并到我仓库的master分支,我这边审核过了就可以的。

2020年12月8日 11:18,banyueyang notifications@github.com<mailto:notifications@github.com> 写道:

我这个在mac上打开截图也很模糊,怎么才能加入你们呢,想尝试着改一下

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/nashaofu/screenshots/issues/42#issuecomment-740347492, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AETIVETDZPYRWZIB6I5QS4TSTWLIZANCNFSM4OGQVSJQ.

lilong7676 commented 3 years ago

首先感谢作者,有个问题,在mac上截图也不是很清晰。

nashaofu commented 2 years ago

截图不清晰使用getUserMedia也存在,并且使用getUserMedia会导致获取截图时间变长很多