小工具知识网

当前位置: 首页 > 行业知识 > html +JavaScript 实现camera调用摄像头系列

行业知识

html +JavaScript 实现camera调用摄像头系列

2026-10-25 19:50:17 来源:互联网转载

 # 纯启动摄像头:

        使用摄像头            function accessCamera() {            const video = document.getElementById("video");            // 创建一个媒体流请求对象            const constraints = {                video: true,                audio: false            };            // 使用getUserMedia方法访问摄像头            navigator.mediaDevices.getUserMedia(constraints)                .then(function(stream) {                    // 将视频流赋值给video元素的src属性,以显示摄像头的视频                    video.srcObject = stream;                    video.onloadedmetadata = function(e) {                        video.play();                    };                })                .catch(function(error) {                    console.error("访问摄像头失败:", error);                });        }    

使用摄像头

访问摄像头

启动摄像头并拍照保存本地:

        使用摄像头            function accessCamera() {            const video = document.getElementById("video");            // 创建一个媒体流请求对象            const constraints = { video: true, audio: false };            // 使用getUserMedia方法访问摄像头            navigator.mediaDevices.getUserMedia(constraints)                .then(function(stream) {                    // 将视频流赋值给video元素的src属性,以显示摄像头的视频                    video.srcObject = stream;                    video.onloadedmetadata = function(e) {                        video.play();                    };                })                .catch(function(error) {                    console.error("访问摄像头失败:", error);                });        }        function takePhoto() {            const video = document.getElementById("video");            // 使用canvas捕获图像数据            const canvas = document.createElement("canvas");            canvas.width = video.videoWidth;            canvas.height = video.videoHeight;            const ctx = canvas.getContext("2d");            ctx.drawImage(video, 0, 0);            // 将图像数据保存为图片文件            const dataUrl = canvas.toDataURL();            saveAs(canvas.toDataURL(), "photo.jpg");        }        function saveAs(data, filename) {            const link = document.createElement("a");            link.href = data;            link.download = filename;            document.body.appendChild(link);            link.click();            document.body.removeChild(link);        }    

使用摄像头

访问摄像头拍照保存

 录制视频并保存:

      Video Recorder

Video Recorder

Start Recording Stop Recording let mediaRecorder; let recordedChunks = []; const startButton = document.getElementById('startRecord'); const stopButton = document.getElementById('stopRecord'); const videoPlayer = document.getElementById('videoPlayer'); startButton.addEventListener('click', startRecording); stopButton.addEventListener('click', stopRecording); async function startRecording() { const stream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true }); mediaRecorder = new MediaRecorder(stream); mediaRecorder.ondataavailable = function(event) { if (event.data.size > 0) { recordedChunks.push(event.data); } }; mediaRecorder.onstop = function() { const blob = new Blob(recordedChunks, { type: 'video/webm' }); const url = URL.createObjectURL(blob); videoPlayer.src = url; }; mediaRecorder.start(); startButton.disabled = true; stopButton.disabled = false; } function stopRecording() { mediaRecorder.stop(); startButton.disabled = false; stopButton.disabled = true; }

共享屏幕和录频并保存在本地:

      Screen Recorder

Screen Recorder

Start Recording Stop Recording Save Video let mediaRecorder; let recordedChunks = []; const startButton = document.getElementById('startRecord'); const stopButton = document.getElementById('stopRecord'); const videoPlayer = document.getElementById('videoPlayer'); const saveButton = document.getElementById('saveVideo'); startButton.addEventListener('click', startRecording); stopButton.addEventListener('click', stopRecording); saveButton.addEventListener('click', saveVideo); async function startRecording() { const stream = await navigator.mediaDevices.getDisplayMedia({ video: true, audio: true }); mediaRecorder = new MediaRecorder(stream); mediaRecorder.ondataavailable = function(event) { if (event.data.size > 0) { recordedChunks.push(event.data); } }; mediaRecorder.onstop = function() { const blob = new Blob(recordedChunks, { type: 'video/mp4' }); const url = URL.createObjectURL(blob); videoPlayer.src = url; }; mediaRecorder.start(); startButton.disabled = true; stopButton.disabled = false; } function stopRecording() { mediaRecorder.stop(); startButton.disabled = false; stopButton.disabled = true; } function saveVideo() { const blob = new Blob(recordedChunks, { type: 'video/mp4' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'screen_recording.mp4'; const clickHandler = () => { setTimeout(() => { URL.revokeObjectURL(url); a.removeEventListener('click', clickHandler); }, 150); }; a.addEventListener('click', clickHandler, false); a.click(); }
()()

上一篇:页游服务器端搭建全攻略,一步步带你走进游戏世界!

下一篇:香港日落探秘,黄昏美景,绝不容错过