<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>示例</title>
<style>
html, body {
width: 100%;
height: 100%;
overflow: hidden;
display: flex;
justify-content: center;
align-items: center;
}
#canvas {
max-width: 100%;
max-height: 100%;
}
</style>
</head>
<body>
<video id="video" style="display: none;"></video>
<canvas id="canvas"></canvas>
<script>
window.onload = function () {
const video = document.getElementById('video');
const canvas = document.getElementById('canvas');
const constraints = {
video: {
width: { ideal: 1920 },
height: { ideal: 1080 },
facingMode: 'user'
}
};
navigator.mediaDevices.getUserMedia(constraints)
.then(stream => {
video.srcObject = stream;
video.onloadeddata = function () {
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
stream.getVideoTracks().forEach(track => track.stop());
};
video.play();
})
.catch(err => {
alert('遇到了一些问题,请刷新页面重新授予权限。');
});
};
</script>
</body>
</html>
暂无评论