wechatide://minicode/fm4ZmNmu6zYO
h5界面源码:
<!DOCTYPE html>
<html lang=“en”>
<head>
<meta charset=“UTF-8”>
<meta name=“viewport” content=“width=device-width, initial-scale=1.0”>
<meta http-equiv=“X-UA-Compatible” content=“ie=edge”>
<title>摄像头拍照</title>
</head>
<body>
<video id=“video” width=“320” height=“320” style=“display: none”>
</video>
<!-- <div>
<button id=“capture”>拍照</button>
</div> -->
<canvas id=“canvas” width=“320” height=“320”></canvas>
<script>
let count = 0;
//访问用户媒体设备的兼容方法
function getUserMedia(constraints, success, error) {
if (navigator.mediaDevices.getUserMedia) {
//最新的标准API
navigator.mediaDevices.getUserMedia(constraints).then(success).catch(error);
} else if (navigator.webkitGetUserMedia) {
//webkit核心浏览器
navigator.webkitGetUserMedia(constraints, success, error)
} else if (navigator.mozGetUserMedia) {
//firfox浏览器
navigator.mozGetUserMedia(constraints, success, error);
} else if (navigator.getUserMedia) {
//旧版API
navigator.getUserMedia(constraints, success, error);
}
}
let video = document.getElementById(‘video’);
let canvas = document.getElementById(‘canvas’);
let context = canvas.getContext(‘2d’);
function success(stream) {
video.srcObject = stream;
video.play();
draw()
}
function error(error) {
alert(`访问用户媒体设备失败${error.name}, ${error.message}`);
}
window.addEventListener(“DOMContentLoaded”, function () {
if (navigator.mediaDevices.getUserMedia || navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia) {
//调用用户媒体设备, 访问摄像头
getUserMedia({
video: {
width: 480,
height: 320
}
}, success, error);
} else {
alert(‘不支持访问用户媒体’);
}
}, false);
function draw(){
context.drawImage(video, 0, 0, canvas.width, canvas.height);
context.font=“50px microsoft yahei”;
context.strokeText(“Hello”,20,100);
requestAnimationFrame(draw);
}
</script>
</body>
</html>