Capturing an image from a webcam

白昼怎懂夜的黑 提交于 2019-12-31 21:12:05

问题


I'm attempting to randomly capture an image from a webcam and then save it to my server, right now i'm using getUserMedia to stream the webcam to video element then I can capture an image to a canvas using a button but i'm not sure how to save that image from the canvas to my server.

Another issue is getUserMedia only works in Chrome, Opera and Firefox, is there any alternative for IE?

This is what i'm currently working with:

    <video id="video" width="640" height="480" autoplay></video>
<button id="snap">Snap Photo</button>
<canvas id="canvas" width="640" height="480"></canvas>
<button id="getBase" onclick="getBase()">Get Base64</button>
<textarea id="textArea"></textarea>

<script>

    // Put event listeners into place
    window.addEventListener("DOMContentLoaded", function() {
        // Grab elements, create settings, etc.
        var canvas = document.getElementById("canvas"),         
        context = canvas.getContext("2d"),
        video = document.getElementById("video"),
        videoObj = { "video": true },
        errBack = function(error) {
        console.log("Video capture error: ", error.code); 
        };

        // Put video listeners into place
        if(navigator.getUserMedia) { // Standard
            navigator.getUserMedia(videoObj, function(stream) {
                video.src = stream;
                video.play();
            }, errBack);
        } else if(navigator.webkitGetUserMedia) { // WebKit-prefixed
            navigator.webkitGetUserMedia(videoObj, function(stream){
                video.src = window.webkitURL.createObjectURL(stream);
                video.play();
            }, errBack);
        }

        // Trigger photo take
        document.getElementById("snap").addEventListener("click", function() {
            context.drawImage(video, 0, 0, 640, 480);
        });

        document.getElementByID("getBase").addEventListener("click", getBase());

    }, false);                  

    function getBase(){
        var imgBase = canvas.toDataURL("image/png");

        document.getElementByID("textArea").value=imgBase;
    }
</script>`

Thanks Heaps


回答1:


You can use canvas.toDataUrl to get a base64 encoded string that you can post as a parameter to a script on your server that will read it in and store as an image file.

Here is a shortened version of an actual implementation of this approach:

<script>
function saveImage(){
    var xmlhttp;
    xmlhttp=((window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP"));
    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4){
            if(xmlhttp.status==200){
                //handle success                
            }
        }
    }
    xmlhttp.open("POST",baseURL,true);
    var oldCanvas = under.toDataURL("image/png");
    var params=oldCanvas;
    xmlhttp.setRequestHeader("Content-type", "application/upload");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(params);
}
</script>

and here is an example PHP script that accepts the base64 string, converts to a .png, gives it a random filename, and saves:

<?php
if((isset($GLOBALS["HTTP_RAW_POST_DATA"]))){
    //some validation is necessary, this is just a proof of concept
    if(isset($GLOBALS["HTTP_RAW_POST_DATA"])){
        $params=explode('&',$GLOBALS["HTTP_RAW_POST_DATA"]);
        $imgsrc=str_replace(' ','+',$params[0]);
        $im = imagecreatefrompng($imgsrc);
    }
    if($im){
        $background = imagecolorallocatealpha($im, 255,255,255,127);
        imagecolortransparent($im, $background);
        //random file name
        $m = rand(10e16, 10e24);
        $filename=base_convert($m, 10, 36);
        //save image
        imagepng($im, 'images/'.$filename.'.png');
        imagedestroy($im);
        $size = getimagesize('images/'.$filename.'.png');
        $iH=$size[1];
        $iW=$size[0];
        echo "{\"filename\":\"".$filename."\",\"width\":\"".$iW."\",\"height\":\"".$iH."\"}";
    }
}
?>



回答2:


You can use getUserMedia.js or other similar libraries that include a Flash fallback to cover IE. Though Flash has become wildly unpopular recently.

CanIUse.com's stream page has information regarding support for this feature.

As was said in Robot Woods' answer, canvas.toDataUrl will grab the image for you.



来源:https://stackoverflow.com/questions/15056647/capturing-an-image-from-a-webcam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!