gpt4 book ai didi

javascript - HTML5 : working with the camera: sizing issue

转载 作者:行者123 更新时间:2023-11-28 01:30:40 25 4
gpt4 key购买 nike

我正在编写这个 HTML5 相机演示,我想通过单击按钮来捕获视频。
问题是,我想以视频的原始尺寸或任何其他“大”尺寸保存图像。但是,我还想用图像的小版本向用户呈现图像。

如何做到这一点?

我使用这些 HTML 标签:

<video width="500" style="margin:0 auto; border:1px;" height="auto" autoplay></video>
<canvas width="auto" height="auto" style="display:none;"></canvas>

最佳答案

Source: MDN

HTML 标记

<video id="video"></video>
<button id="startbutton">Take photo</button>
<canvas id="canvas"></canvas>
<img src="http://placekitten.com/g/320/261" id="photo" alt="photo">

完整脚本

(function() {

var streaming = false,
video = document.querySelector('#video'),
canvas = document.querySelector('#canvas'),
photo = document.querySelector('#photo'),
startbutton = document.querySelector('#startbutton'),
width = 320,
height = 0;

navigator.getMedia = ( navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);

navigator.getMedia(
{
video: true,
audio: false
},
function(stream) {
if (navigator.mozGetUserMedia) {
video.mozSrcObject = stream;
} else {
var vendorURL = window.URL || window.webkitURL;
video.src = vendorURL.createObjectURL(stream);
}
video.play();
},
function(err) {
console.log("An error occured! " + err);
}
);

video.addEventListener('canplay', function(ev){
if (!streaming) {
height = video.videoHeight / (video.videoWidth/width);
video.setAttribute('width', width);
video.setAttribute('height', height);
canvas.setAttribute('width', width);
canvas.setAttribute('height', height);
streaming = true;
}
}, false);

function takepicture() {
canvas.width = width;
canvas.height = height;
canvas.getContext('2d').drawImage(video, 0, 0, width, height);
var data = canvas.toDataURL('image/png');
photo.setAttribute('src', data);
}

startbutton.addEventListener('click', function(ev){
takepicture();
ev.preventDefault();
}, false);

})();

关于javascript - HTML5 : working with the camera: sizing issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22149768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com