从我的网站捕获照片并将其保存在服务器上

时间:2015-08-12 22:29:28

标签: javascript html5 getusermedia

我希望在我的网站上有一个按钮,当用户点击它时,它将打开计算机的相机并拍照并将其保存在我的服务器上。

来自html的代码

<button type="button" id="button1">You want to see something COOL? (Not working from phone)</button><br />
<button id='reverse'>Reverse direction</button>

 <video id='v'></video>

<style>

    video {

        position:absolute;
        border-radius: 50%;
        transform: rotateY(180deg);
        -webkit-transform:rotateY(180deg); 
        -moz-transform:rotateY(180deg); 
        -o-transform:rotateY(180deg);
        -ms-transform:rotateY(180deg);
      }

</style>

javascript文件

if (document.getElementById("button1").onclick == 'click' ){ //that is the problem,i want when the user click on the button to run the following code

var deltax = 10;
var deltay = 10;
var v;
var reverse;
var w;  //will try to get dimensions of window
var h;
var x;  //starting position
var y;
var vw = 300;  //default dimensions of video
var vh = 210;

console.log("hello");



window.addEventListener('DOMContentLoaded', function() {
        reverse = document.getElementById('reverse');
// When the reverse button is clicked, toggle the direction using deltax,deltay
        reverse.addEventListener('click', changedeltas, false);
        v = document.getElementById('v');


        w = window.innerWidth;
        h = window.innerHeight;
        x = w/5;
        y = h/5;

        v.style.left  = String(x)+"px";
        v.style.top = String(y)+"px";


    navigator.getUserMedia = (navigator.getUserMedia || 
                                  navigator.webkitGetUserMedia || 
                                  navigator.mozGetUserMedia || 
                                  navigator.msGetUserMedia);
        //alert("test "+navigator.getUserMedia);
        var isStreaming = false;
        // Cross browser

        if (navigator.getUserMedia) {

            // Request access to video and audio
            navigator.getUserMedia(
                {
                    video:true,
                    audio: false
                },
                function(stream) {
                    // Cross browser checks
                    var url = window.URL || window.webkitURL;
                    v.src = url ? url.createObjectURL(stream) : stream;
                    // Set the video to play
                    v.play();

                },
                function(error) {
                    alert('Something went wrong. (error code ' + error.code + ')');
                    return;
                }
            );
        }
        else {
            alert('Sorry, the browser you are using doesn\'t support getUserMedia');
            return;
        }

        // Wait until the video stream can play
        v.addEventListener('canplay', function(e) {

            if (!isStreaming) {  //only do once
                // videoWidth isn't always set correctly in all browsers
                if (v.videoWidth > 0) {
                    vw = v.videoWidth;
                    vh = v.videoHeight;

                  }
                ratio = vh/vw;
                vw = Math.min(vw,w/5);
                vh = vw*ratio;
                v.setAttribute('width',vw);
                v.setAttribute('height',vh);
                //alert("now vw is "+vw+" vh is "+vh+" w is "+w+" h is "+h);
                isStreaming = true;
            }
        }, false);



        // Wait for the video to start to play
        v.addEventListener('playing', function() {


            tid = setInterval(moveit,50);
        },false);
      },false);


    function moveit(){
            x  += deltax;
            y  += deltay;
            //5 is fudge factor for hitting bottom and right wall to stop scrolling
            if (((x+vw+5)>w) || (x<0)) {deltax = -deltax;}
            if (((y+vh+5)>h) || (y<0)) {deltay = -deltay;}
            v.style.top = String(y)+"px";
            v.style.left = String(x)+"px";
        }


 function changedeltas(){   
            deltax = -deltax; 
            deltay = -deltay; 
  }
}
谁能帮助我吗? 感谢

0 个答案:

没有答案