使用 HTML5 控制摄像头摄像和拍照并保存图像文件到本地

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <meta name="generator" content="PSPad editor, www.pspad.com">
        <title></title>
    </head>
    <body>

    <video id="video" width="640" height="480" autoplay></video>
    <button id="snap">Snap Photo</button>
    <button id="save">Save Photo</button>
    <canvas id="canvas" width="640" height="480"></canvas>

    <script type="text/javascript">

        // Grab elements, create settings, etc.
        var video = document.getElementById('video');

        // Get access to the camera!
        if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
            // Not adding `{ audio: true }` since we only want video now
            navigator.mediaDevices.getUserMedia({video: true}).then(function (stream) {
                //video.src = window.URL.createObjectURL(stream);
                video.srcObject = stream;
                video.play();
            });
        }

        /* Legacy code below: getUserMedia 
         else if(navigator.getUserMedia) { // Standard
         navigator.getUserMedia({ video: true }, function(stream) {
         video.src = stream;
         video.play();
         }, errBack);
         } else if(navigator.webkitGetUserMedia) { // WebKit-prefixed
         navigator.webkitGetUserMedia({ video: true }, function(stream){
         video.src = window.webkitURL.createObjectURL(stream);
         video.play();
         }, errBack);
         } else if(navigator.mozGetUserMedia) { // Mozilla-prefixed
         navigator.mozGetUserMedia({ video: true }, function(stream){
         video.srcObject = stream;
         video.play();
         }, errBack);
         }
         */

        // Elements for taking the snapshot
        var canvas = document.getElementById('canvas');
        var context = canvas.getContext('2d');
        var video = document.getElementById('video');

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

        function saveAsLocalImage() {
            var myCanvas = document.getElementById("canvas");
            // here is the most important part because if you dont replace you will get a DOM 18 exception.  
            // var image = myCanvas.toDataURL("image/png").replace("image/png", "image/octet-stream;Content-Disposition: attachment;filename=foobar.png");  
            var image = myCanvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
            // window.location.href = image; // it will save locally  
            // create temporary link  
            var tmpLink = document.createElement('a');
            tmpLink.download = 'image.png'; // set the name of the download file 
            tmpLink.href = image;

            // temporarily add link to body and initiate the download  
            document.body.appendChild(tmpLink);
            tmpLink.click();
            document.body.removeChild(tmpLink);
        }

        // Trigger photo save
        document.getElementById("save").addEventListener("click", function () {
            saveAsLocalImage();
        });
    </script>
</body>
</html>

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值