Siddharth
Siddharth

Reputation: 7156

Trying to capture desktop image using getUserMedia and canvas

I am making an chrome extension through which I can get a screenshot of user's desktop. I am using chrome.desktopCapture API to get stream of selected window. I am creating video element dynamically. Once its source it set, I invoked the play() method. Then I created a canvas element and used it to draw an image of video. I am also able to get the dataURL using canvas.toDataURL() but when i open this url I see no image. And also this URL value remains same even during new capture.

Manifest.json -

{
  "name": "Desktop Capture Example",
  "description": "Show desktop media picker UI",
  "version": "1",
  "manifest_version": 2,
  "background": {
     "persistent": false,
      "scripts": ["background.js"]
  },
    "browser_action": {
    "default_title": "Take a screen shot of Desktop!"
  },
  "permissions": [
    "desktopCapture"
  ]
}

Background.js -

var pending_request_id = null;

chrome.browserAction.onClicked.addListener(function() {

   pending_request_id = chrome.desktopCapture.chooseDesktopMedia(["screen", "window"],onAccessApproved);

});

function gotStream(stream) {
  console.log("Received local stream");
  var video = document.createElement('video');
  video.src = URL.createObjectURL(stream);
  video.play();
  var canvas = document.createElement('canvas');
  var ctx = canvas.getContext("2d");
  ctx.drawImage(video, 0, 0, 640, 480);
   var url = canvas.toDataURL();
  console.log(url);
  //localstream = stream;
  //stream.onended = function() { console.log("Ended"); };
}

function getUserMediaError() {
  console.log("getUserMedia() failed.");
}

function onAccessApproved(id) {
  if (!id) {
    console.log("Access rejected.");
    return;
  }
  navigator.webkitGetUserMedia({
      audio:false,
      video: { mandatory: { chromeMediaSource: "desktop",
                            chromeMediaSourceId: id } }
  }, gotStream, getUserMediaError);
}

Upvotes: 3

Views: 5378

Answers (1)

Kaiido
Kaiido

Reputation: 136707

You have to wait for the video actually loads the stream : rewrite your gotStream like so :

function gotStream(stream) {
  console.log("Received local stream");
  var video = document.createElement('video');
  video.addEventListener('loadedmetadata',function(){
      var canvas = document.createElement('canvas');
      canvas.width = this.videoWidth;
      canvas.height = this.videoHeight;
      var ctx = canvas.getContext("2d");
      ctx.drawImage(this, 0, 0);
      var url = canvas.toDataURL();
      console.log(url);
      // will open the captured image in a new tab
      window.open(url);
    },false);
  video.src = URL.createObjectURL(stream);
  video.play();
  }

Also, in order to get a better quality image, you have to set the mandatory maxHeight and maxWidth of the getUserMedia() call :

navigator.webkitGetUserMedia({
      audio:false,
      video: { mandatory: { chromeMediaSource: "desktop",
                            chromeMediaSourceId: id,
                            maxWidth: 4000,
                            maxHeight: 4000} }
  }, gotStream, getUserMediaError);

Upvotes: 1

Related Questions