1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="author" content="GaryWang"> <meta name='renderer' content='webkit'> <meta http-equiv='X-UA-Compatible' content='IE=Edge,chrome=1' > <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <meta name="format-detection" content="telephone=no"> <meta name="format-detection" content="email=no"> <title></title> <link href="css/basic.css" rel="stylesheet" type="text/css" /> <link href="css/toast.css" rel="stylesheet" type="text/css" /> <script> function autoScale(){ var winW = document.documentElement.clientWidth; return Math.min (1,Math.min(winW / 750)); } document.documentElement.style.cssText = 'font-size:'+(100 * autoScale())+'px'; </script> </head> <body> <div class="wrap"> <div class="shadow"> <div class="btn"></div> </div> <video src="" id="videoElement" loop preload style="margin:0 auto;"></video> <canvas id="canvas"></canvas> </div> <script src="js/jquery-1.11.3.min.js"></script> <script> const [w,h] = [600,600] var video = document.getElementById("videoElement"); setTimeout(() => { if ( navigator.mediaDevices.getUserMedia || navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia ) { getUserMedia( { video: { width:w, height:h } }, function videoSuccess(stream) { mediaStreamTrack = stream; video.srcObject = stream; video.play(); }, function videoError(error) { alert("摄像头打开失败,请检查权限设置!"); } ); } else { alert("摄像头打开失败,请检查权限设置!"); } }, 500);
function getUserMedia(constraints, success, error) { if (navigator.mediaDevices.getUserMedia) { navigator.mediaDevices .getUserMedia(constraints) .then(success) .catch(error); } else if (navigator.webkitGetUserMedia) { navigator.webkitGetUserMedia(constraints, success, error); } else if (navigator.mozGetUserMedia) { navigator.mozGetUserMedia(constraints, success, error); } else if (navigator.getUserMedia) { navigator.getUserMedia(constraints, success, error); } } $('.btn').click(function(){ var canvas = document.createElement("canvas"); canvas.width = w; canvas.height = h; let context = canvas.getContext("2d"); context.drawImage(video, 0, 0,w, h); context.scale(-1, 1); var imgSrc = canvas.toDataURL("image/png"); $('.shadow').append( `<img src=${imgSrc} />` ) })
</script> </body> </html>
|