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

 # 纯启动摄像头:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>使用摄像头</title>
    <script>
        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);
                });
        }

    </script>
</head>
<body>
<h1>使用摄像头</h1>
<button onclick="accessCamera()">访问摄像头</button>
<video id="video" width="50%" height="50%" autoplay></video>
</body>
</html>

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

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>使用摄像头</title>
    <script>
        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);
        }
    </script>
</head>
<body>
<h1>使用摄像头</h1>
<button onclick="accessCamera()">访问摄像头</button>
<button onclick="takePhoto()">拍照保存</button>
<video id="video" width="50%" height="50%" autoplay></video>
</body>
</html>

 录制视频并保存:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Video Recorder</title>
</head>
<body>
<h1>Video Recorder</h1>
<div>
  <button id="startRecord">Start Recording</button>
  <button id="stopRecord" disabled>Stop Recording</button>
</div>
<div>
  <video id="videoPlayer" width="640" height="480" controls></video>
</div>

<script>
  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;
  }
</script>
</body>
</html>

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

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Screen Recorder</title>
</head>
<body>
<h1>Screen Recorder</h1>
<div>
  <button id="startRecord">Start Recording</button>
  <button id="stopRecord" disabled>Stop Recording</button>
</div>
<div>
  <video id="videoPlayer" width="640" height="480" controls></video>
</div>
<div>
  <button id="saveVideo">Save Video</button>
</div>

<script>
  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();
  }
</script>
</body>
</html>

  • 8
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Android中,可以使用HTML实现调用摄像头进行扫码功能。以下是一种实现方法: 1. 首先,需要在Android的Manifest文件中添加权限,以允许应用程序使用相机功能: ``` <uses-permission android:name="android.permission.CAMERA" /> ``` 2. 在HTML文件中,使用`<input>`标签创建一个按钮,用于触发扫码功能: ``` <input type="button" value="Scan QR Code" id="scanButton" onclick="scanQRCode()" /> ``` 3. 在HTML中,创建一个JavaScript函数,用于调用摄像头进行扫码操作: ``` <script> function scanQRCode() { // 使用JavaScript的Navigator对象,调用Android原生的摄像头功能 navigator.camera.getPicture( function(cameraImage) { // 此处可以处理扫码成功后的操作,比如将扫码结果显示在页面上 alert("QR Code Scanned: " + cameraImage); }, function(errorMessage) { alert("Error: " + errorMessage); }, { // 设置摄像头调用的选项,比如设置摄像头的分辨率、前后摄像头等 quality: 100, destinationType: Camera.DestinationType.DATA_URL, sourceType: Camera.PictureSourceType.CAMERA, encodingType: Camera.EncodingType.JPEG, allowEdit: true, saveToPhotoAlbum: false } ); } </script> ``` 4. 最后,在Android的WebView中加载该HTML文件,即可在应用中通过点击按钮来调用摄像头进行扫码操作。 需要注意的是,上述代码仅为演示使用,并不能直接运行。具体的实现还需要根据实际需求进行调整,并在代码中添加相应的错误处理及其他功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ALIM-MASTIK

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值