调用摄像头 拍照

来源:互联网 发布:怎么看淘宝店铺买家秀 编辑:程序博客网 时间:2024/05/19 13:58
相关例子:  http://www.qdfuns.com/notes/15998/b5ad46a155bf4cec6587cd4871f044c8.html

手机端,电脑端  都支持拍照。 微信端不可以,调用微信的拍照接口。




<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>take photo</title>
    <style>
        body{backgroundurl("http://img13.360buyimg.com/cms/jfs/t868/250/648027290/292491/5c670a52/5549b7f9N7bc3afed.jpg");}
        /*拍照相关组件的样式*/
        video,canvas{background-colorrgba(255,255,255,0.6); border2px solid #333border-radius10px;}
        h1,h2{margin20px autocolor#20cbc9text-aligncenter;}
        *:focus{
            outlinenone;
        }

        #video,#canvas{
            margin20px auto;
            border1px solid #000000;
        }

        #snap{
            width200px;
            height80px;
            border-radius20px;
            background-colorgreenyellow;
            bordernone;
            marginauto;
        }

        #snap,#video,#canvas{
            displayblock;
        }


        .filter0{
            filtergrayscale(1);
            -webkit-filtergrayscale(1);
            -moz-filtergrayscale(1);
            -o-filtergrayscale(1);
            -ms-filtergrayscale(1);
        }
        .filter1{filterblur(2px);
            -webkit-filterblur(2px);
            -moz-filterblur(2px);
            -o-filterblur(2px);
            -ms-filterblur(2px);}
        .filter2{filtersaturate(3);
            -webkit-filtersaturate(3);
            -moz-filtersaturate(3);
            -o-filtersaturate(3);
            -ms-filtersaturate(3);}
        .filter3filtersepia(0.5);
            -webkit-filtersepia(0.5);
            -moz-filtersepia(0.5);
            -o-filtersepia(0.5);
            -ms-filtersepia(0.5);}
        .filter4{filterhue-rotate(180deg);
            -webkit-filterhue-rotate(180deg);
            -moz-filterhue-rotate(180deg);
            -o-filterhue-rotate(180deg);
            -ms-filterhue-rotate(180deg);}
        .filter5filterinvert(1);
            -webkit-filterinvert(1);
            -moz-filterinvert(1);
            -o-filterinvert(1);
            -ms-filterinvert(1);}
        .filter6filterbrightness(50%);
            -webkit-filterbrightness(150%);
            -moz-filterbrightness(150%);
            -o-filterbrightness(150%);
            -ms-filterbrightness(150%);}
        .filter7{filtercontrast(0.8);
            -webkit-filtercontrast(0.8);
            -moz-filtercontrast(0.8);
            -o-filtercontrast(0.8);
            -ms-filtercontrast(0.8);}
        .filterall{list-stylenone;}
        .filterlist{width100%height200pxborder1px solid #333;}
        .filterall{width990pxheight200pxlist-stylenonemarginauto;}
        .filterall li{floatleftmargin-right10pxcursorpointer;}
        .filterall li p{text-aligncentercolor#ffffont-size20px;}
    </style>
</head>
<body>
<h1>拍照上传</h1>
<video id="video" width="480" height="320" autoplay></video>
<button id="snap">截图</button>
<canvas id="canvas" width="480" height="320"></canvas>
<h2>滤镜列表</h2>
<div class="filterlist">
    <ul class="filterall" id="currentlist">
        <li><div><img src="http://img10.360buyimg.com/cms/jfs/t1285/221/79857272/6043/163bd730/5549b83fN5b6f14f0.jpg" class="filter0" style="width:100pxheight:100px;"></div><p>黑白照</p></li>
        <li><div><img src="http://img10.360buyimg.com/cms/jfs/t1285/221/79857272/6043/163bd730/5549b83fN5b6f14f0.jpg" class="filter1" style="width:100pxheight:100px;"></div><p>朦胧</p></li>
        <li><div><img src="http://img10.360buyimg.com/cms/jfs/t1285/221/79857272/6043/163bd730/5549b83fN5b6f14f0.jpg" class="filter2" style="width:100pxheight:100px;"></div><p>饱和</p></li>
        <li><div><img src="http://img10.360buyimg.com/cms/jfs/t1285/221/79857272/6043/163bd730/5549b83fN5b6f14f0.jpg" class="filter3" style="width:100pxheight:100px;"></div><p>老照片</p></li>
        <li><div><img src="http://img10.360buyimg.com/cms/jfs/t1285/221/79857272/6043/163bd730/5549b83fN5b6f14f0.jpg" class="filter4" style="width:100pxheight:100px;"></div><p>色相翻转</p></li>
        <li><div><img src="http://img10.360buyimg.com/cms/jfs/t1285/221/79857272/6043/163bd730/5549b83fN5b6f14f0.jpg" class="filter5" style="width:100pxheight:100px;"></div><p>X</p></li>
        <li><div><img src="http://img10.360buyimg.com/cms/jfs/t1285/221/79857272/6043/163bd730/5549b83fN5b6f14f0.jpg" class="filter6" style="width:100pxheight:100px;"></div><p>高亮</p></li>
        <li><div><img src="http://img10.360buyimg.com/cms/jfs/t1285/221/79857272/6043/163bd730/5549b83fN5b6f14f0.jpg" class="filter7" style="width:100pxheight:100px;"></div><p>均衡感</p></li>
    </ul>

</div>
<img src="" width="200" height="200" id="test">
<!--拍照功能需要配合javascript使用-->
<script type="text/javascript">
    /**
     * Created by Rodger on 2015/2/26.
     */

    /*
     **这是第一种写法,下面有一种更好的写法
     window.addEventListener('DOMContentLoaded',function(){
     var canvas=document.getElementById('canvas'),
     video=document.getElementById('video'),
     context=canvas.getContext('2d'),
     videoObj={'video':true},
     errBack= function (error) {
     console.log('Video capture error:',error.code);
     };

     if(navigator.getUserMedia){
     navigator.getUserMedia(videoObj,function(stream){
     video.src=stream;
     video.play();
     },errBack)
     }else if(navigator.webkitGetUserMedia){
     navigator.webkitGetUserMedia(videoObj,function(stream){
     video.src=window.webkitURL.createObjectURL(stream);
     video.play();
     },errBack)
     }else if(navigator.mozGetUserMedia){
     navigator.mozGetUserMedia(videoObj,function(stream){
     video.src=window.URL.createObjectURL(stream);
     video.play();
     },errBack)
     };

     document.getElementById('snap').addEventListener('click',function(){
     context.drawImage(video,0,0,640,480);
     })
     },false)
     */

    //下面的代码比上面的更加简洁和统一
   window.addEventListener('DOMContentLoaded',function(){
        //获取要控制的DOM对象
       var canvas=document.getElementById('canvas'),
                context=canvas.getContext('2d'),
                video=document.getElementById('video'),
        //这是getUserMedia的第一个参数constraints的值
               videoObj={'video':true},
                errBack=function(error){
                    console.log('video capture error:',error.code);
                }
        /*
         **这种直接把navigator的各种getUserMedia函数直接赋值给getUserMedia是不行的
         *
         var getUserMedia=navigator.getUserMedia||navigator.webkitGetUserMedia||navigator.mozGetUserMedia||navigator.msGetUserMedia;

         getUserMedia(videoObj,function(localMediaStream){
         video.src=window.URL.createObjectURL(localMediaStream);
         video.play();
         },errBack);

         */

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

        navigator.myGetUserMedia(videoObj,function(localMediaStream){
            /*并不是每个window都有URL对象,要加上浏览器前缀
             **video.src=window.URL.createObjectURL(localMediaStream);
             * */
            window.URL=window.URL||window.webkitURL||window.mozURL||window.msURL;
            video.src=window.URL.createObjectURL(localMediaStream);
            video.play();
        },errBack);

        video.onloadedmetadata=function(e){
            var snapBtn=document.getElementById('snap');
            snapBtn.addEventListener('click',function(){
                context.drawImage(video,0,0,480,320);
                var image = new Image();
                image.src canvas.toDataURL("image/png");
                var testimg=document.getElementById('test');
                testimg.src=image.src;
            })
        };
    })

    var EventUtil={
        addHandler:function(element,type,handler){
            if(element.addEventListener){element.addEventListener(type,handler,false);}
            else if(element.attachEvent){element.attachEvent("on"+type,handler);}else{element["on"+type]=handler;}
        },
        removeHandler:function(element,type,handler){
            if(element.removeEventListener){element.removeEventListener(type,handler,false);}
            else if(element.detachEvent){element.detachEvent("on"+type,handler);}else{element["on"+type]=null;}
        },
        getEvent:function(event){
            return eventevent:window.event/*获取事件对象*/
        },
        getTarget:function(event){
            return event.target||event.srcElement/*获取目标元素*/
        },
        preventDefault:function(event){     /*取消事件的默认设置*/
            if(event.preventDefault){
                event.preventDefault();
            }else{
                event.returnValue=false;
            }
        },
        stopPropagation:function(event){     /*取消事件冒泡*/
            if(event.stopPropagation){
                event.stopPropagation();
            }else{
                event.cancelBubble=true;
            }
        }
    };

    var currentlist=document.getElementById('currentlist');
    EventUtil.addHandler(currentlist,"click",function(e){
        var classnames=EventUtil.getTarget(e).className;
        var videos=document.getElementById('video');
        var can=document.getElementById('canvas');
        videos.className=classnames;
        can.className=classnames;
    });
</script>
</body>
</html>
原创粉丝点击