javascript - Zoom and crop video source - Stack Overflow

In my app, I wrote a code which allows a user to take a picture using their camera which is connected t

In my app, I wrote a code which allows a user to take a picture using their camera which is connected to the puter.

Implementation:

I have used a webcam, which show a video element and the source is the camera:

function onSuccess (stream) {
 this.video.srcObject = stream;
}
navigator.getUserMedia({video: true}, onSuccess, onFailure)

When the user clicks on a button I create a snapshot from the video:

function takeSnapshot (width, height, savePicture) {
      const canvas = document.createElement('canvas');
      canvas.width = width;
      canvas.height = height;
      const ctx = canvas.getContext('2d');
      ctx.drawImage(this.video, 0, 0, canvas.width, canvas.height);

      canvas.toBlob(function (blob) {
         savePicture(blob);
      }, "image/jpeg");
   }

Now, I need to zoom into the camera and crop the video around the center, since I want to remove the background around the photo. I don't want to implement this using CSS since I want to save the picture cropped and zoomed too. I want this zooming in the video view and also when taking the snapshot with zoom.

The camera has no zooming option of its own.

Is there an option to zoom and crop in the video stream?

In my app, I wrote a code which allows a user to take a picture using their camera which is connected to the puter.

Implementation:

I have used a webcam, which show a video element and the source is the camera:

function onSuccess (stream) {
 this.video.srcObject = stream;
}
navigator.getUserMedia({video: true}, onSuccess, onFailure)

When the user clicks on a button I create a snapshot from the video:

function takeSnapshot (width, height, savePicture) {
      const canvas = document.createElement('canvas');
      canvas.width = width;
      canvas.height = height;
      const ctx = canvas.getContext('2d');
      ctx.drawImage(this.video, 0, 0, canvas.width, canvas.height);

      canvas.toBlob(function (blob) {
         savePicture(blob);
      }, "image/jpeg");
   }

Now, I need to zoom into the camera and crop the video around the center, since I want to remove the background around the photo. I don't want to implement this using CSS since I want to save the picture cropped and zoomed too. I want this zooming in the video view and also when taking the snapshot with zoom.

The camera has no zooming option of its own.

Is there an option to zoom and crop in the video stream?

Share Improve this question edited Nov 5, 2018 at 13:09 Uwe Keim 40.8k61 gold badges190 silver badges304 bronze badges asked Nov 5, 2018 at 12:37 RivkaRivka 1711 silver badge6 bronze badges 1
  • Due the video output is in the canvas you can do anything canvas allows you, including zoom and crop. – Mosh Feu Commented Nov 5, 2018 at 13:17
Add a ment  | 

1 Answer 1

Reset to default 3

context.drawImage() has optional parameters to "scale" image (sWidth, sHeight). To move the image and "crop", you may set additional params (dx, dy).

https://developer.mozilla/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage

But this a direct way of zoom&crop by JS. If you need ready UI for such functionality, you may bine your code with something like this: http://deepliquid./projects/Jcrop/demos.php?demo=thumbnail (I didn't try it, just have it in bookmarks)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745595742a4635101.html

相关推荐

  • javascript - Zoom and crop video source - Stack Overflow

    In my app, I wrote a code which allows a user to take a picture using their camera which is connected t

    14小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信