首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在HTML页面中使用摄像头拍摄快照?

如何在HTML页面中使用摄像头拍摄快照?
EN

Stack Overflow用户
提问于 2012-05-03 13:51:37
回答 1查看 16.3K关注 0票数 1

如果我保存一个包含绘图的<canvas>。一切都很正常。但是,如果我想保存一个包含图像的canvas (来自网络摄像头流的帧),它不工作,也不会向服务器发送任何东西。有人对此有什么想法吗?

HTML:

代码语言:javascript
复制
<video id="VideoCamera" autoplay></video>
<canvas id="testCanvas" width="300" height="300"></canvas>
<textarea id="debugConsole" rows="10" cols="60">Data</textarea>
<button onclick="saveViaAJAX();">Save Via AJAX</button>
<input id="button" type="button" value="photo" onclick="snapshot()" />
<input id="button1" type="button" value="bubble" onclick="bubble()" />

<script type="text/javascript">
    // This portion webcam setup
    var video = document.getElementsByTagName('video')[0];
    var localMediaStream = null;
    if (navigator.getUserMedia) {
        navigator.getUserMedia('video', successCallback, errorCallback);
        function successCallback(stream) {
            video.src = stream;
            localMediaStream = stream;
        }
        function errorCallback(error) { heading.textContent = "An error occurred: [CODE " + error.code + "]"; }
    }
    else {
        heading.textContent = "Native web camera streaming is not supported in this browser!";
    }

    //draw something in canvass
    var canvas = document.getElementById("testCanvas");
    if (canvas.getContext) {
        var canvasContext = canvas.getContext("2d");    
        canvasContext.fillStyle = "rgb(" + (parseInt(Math.random() * 255)) + "," + (parseInt(Math.random() * 255)) + "," + (parseInt(Math.random() * 255)) + ")";
        canvasContext.beginPath();
        canvasContext.arc(Math.random() * 350, Math.random() * 350, Math.random() * 20, 0, Math.PI * 2, true);
        canvasContext.fill();
    }

    // This portion of the code simply draws random circles into the canvas (it has nothing todo with saving the canvas).
    function bubble() {
        var canvas = document.getElementById("testCanvas");
        var canvasContext = canvas.getContext("2d");
        for (i = 0; i < 150; i++) {
            canvasContext.fillStyle = "rgb(" + (parseInt(Math.random() * 255)) + "," + (parseInt(Math.random() * 255)) + "," + (parseInt(Math.random() * 255)) + ")";
            canvasContext.beginPath();
            canvasContext.arc(Math.random() * 350, Math.random() * 350, Math.random() * 20, 0, Math.PI * 2, true);
            canvasContext.fill();
        }
    }

    // This portion of the code take snaphot from wecam
    function snapshot() {
        var canvas = document.getElementById("testCanvas");
        var canvasContext = canvas.getContext("2d");
        canvasContext.drawImage(video, 0, 0, 240, 320);
    }


    // This portion of the code save canvass to server
    function saveViaAJAX() {
        var canvas = document.getElementById("testCanvas");
        var canvasContext = canvas.toDataURL("image/png");
        var postData = "canvasData=" + canvasContext;
        var debugConsole = document.getElementById("debugConsole");
        debugConsole.value = canvasContext;

        var ajax = new XMLHttpRequest();
        ajax.open("POST", 'Save.php', true);
        ajax.setRequestHeader('Content-Type', 'canvas/upload');
        ajax.setRequestHeader('Content-TypeLength', postData.length);

        ajax.onreadystatechange = function () {
            if (ajax.readyState == 4) {
                debugConsole.value = canvasContext + " " + debugConsole.value;
            }
        }
        ajax.send(postData);
    }

PHP save.php 代码:

代码语言:javascript
复制
$png =$_POST['data'];
$filteredData=substr($png, strpos($png, ",")+1);
$unencodedData=base64_decode($filteredData);
$fp = fopen( 'image.png', 'wb' );
fwrite( $fp, $unencodedData);
fclose( $fp );

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2015-06-22 17:29:41

请查看picEdit project,它实现了从摄像头拍摄快照。在PC和Android上测试(使用Chrome浏览器,本地浏览器似乎不支持访问摄像头):

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10425820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档