admin管理员组文章数量:1432566
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 anythingcanvas
allows you, including zoom and crop. – Mosh Feu Commented Nov 5, 2018 at 13:17
1 Answer
Reset to default 3context.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)
本文标签: javascriptZoom and crop video sourceStack Overflow
版权声明:本文标题:javascript - Zoom and crop video source - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745595742a2665467.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论