简体   繁体   English

HTML5 相机拉伸图片

[英]HTML5 camera stretches the picture

I'm using the HTML5 camera for taking picture.我正在使用 HTML5 相机拍照。 I have noticed that after the picture is captured, the final image width and height are stretched though I'm applying the same width and height for both video as well as canvas.我注意到在捕获图片后,虽然我为视频和画布应用了相同的宽度和高度,但最终图像的宽度和高度会被拉伸。

Please refer this JSFIDDLE请参考这个JSFIDDLE

var video = document.querySelector("#videoElement");

navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia || navigator.oGetUserMedia;

if (navigator.getUserMedia) {       
    navigator.getUserMedia({video: true}, handleVideo, videoError);
}

function handleVideo(stream) {
    video.src = window.URL.createObjectURL(stream);
}

function videoError(e) {
    // do something
}
var v,canvas,context,w,h;
var imgtag = document.getElementById('imgtag');
var sel = document.getElementById('fileselect');

//document.addEventListener('DOMContentLoaded', function(){
    v = document.getElementById('videoElement');
    canvas = document.getElementById('canvas');
    context = canvas.getContext('2d');
    w = canvas.width;
    h = canvas.height;

//},false);

function draw(v,c,w,h) {
    if(v.paused || v.ended) return false;
    context.drawImage(v,0,0,w,h);

   var uri = canvas.toDataURL("image/png");

   imgtag.src = uri;
}

document.getElementById('save').addEventListener('click',function(e){

    draw(v,context,w,h);


});
var fr;

sel.addEventListener('change',function(e){
    var f = sel.files[0];

    fr = new FileReader();
    fr.onload = receivedText;
    fr.readAsDataURL(f);
})

function receivedText() {        
    imgtag.src = fr.result;
}

Your code has hard-coded values for width and height, both for video and canvas elements. 您的代码具有针对视频和画布元素的宽度和高度的硬编码值。

Avoid setting absolute sizes in both directions as the dimension of the video element can vary. 避免在两个方向上都设置绝对大小,因为视频元素的尺寸可能会发生变化。 You can set the size by using just the width or just the height, or by using CSS style/rule. 您可以仅使用宽度或高度,或使用CSS样式/规则来设置大小。

For canvas you need to set the size based on the actual size of the video element. 对于画布,您需要根据视频元素的实际大小设置大小。 For this use the video element properties: 为此,请使用视频元素属性:

 video.videoWidth;
 video.videoHeight;

You can apply a scale to those as you want, for example: 您可以将刻度应用于所需的刻度,例如:

scale = 300 / v.videoWidth;

w = v.videoWidth * scale;
h = v.videoHeight * scale;

canvas.width = w;
canvas.height = h;
context.drawImage(v, 0, 0, w, h);

Updated fiddle 更新的小提琴

Should someone still be facing this problem in 2021...如果有人在 2021 年仍然面临这个问题......

// init media navigator
navigator.mediaDevices.getUserMedia(constraints).then(stream => {

    // get the actual settings of video element, 
    // which includes real video width and height
    const streamSettings = stream.getVideoTracks()[0].getSettings();
    // set the constrains to canvas element
    canvasElement.width = streamSettings.width;
    canvasElement.height = streamSettings.height;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM