如何使用HTML5将用网络摄像头捕获的jpg图像/视频保存在本地硬盘中 [英] How to save a jpg image/video captured with webcam in the local hard drive with HTML5

查看:97
本文介绍了如何使用HTML5将用网络摄像头捕获的jpg图像/视频保存在本地硬盘中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题似乎很简单,尽管我找不到合适的解决方案因为我缺乏对HTML和Javascript的了解.

The problem appears simple, although I cannot find a suitable solution because of my lack of knowledge of HTML and Javascript.

任务只是设计一个网页,其中一个按钮将激活网络摄像头并将静态图像或视频(首选)存储在本地硬盘中.暂时不需要上载/下载.

The task is simply to design a webpage where a button will activate the webcam and store either a still image or a video (preferable) in the local hard drive. No upload/download required for the time being.

经过一些尝试,我能够使用getusermedia()api激活网络摄像头并在浏览器窗口中呈现视频,但无法保存它.这就是我的代码的样子.

After some attempts, I am able to use the getusermedia() api to activate the webcam and render the video in the browser window, but unable to save it. This is what my code looks like.

if (navigator.getUserMedia) {       
    navigator.getUserMedia({video: true}, handleVideo, videoError);
}

function handleVideo(stream) {
    video.src = window.URL.createObjectURL(stream);
}

那么关于如何将静态图像或视频保存到硬盘驱动器的任何想法都以相同的方式捕获了吗?

So any idea on how to save either a still image or a video in the hard drive captured the same way?

推荐答案

首先, navigator.getUserMedia API已被弃用,您现在应该使用

First, the navigator.getUserMedia API is being deprecated, you should now use the navigator.mediaDevices.getUserMedia method.

然后要拍摄静止图像,您确实可以使用可以绘制视频元素.

Then to take a still image, you can indeed use a canvas which can draw a video element.

const vid = document.querySelector('video');
navigator.mediaDevices.getUserMedia({video: true}) // request cam
.then(stream => {
  vid.srcObject = stream; // don't use createObjectURL(MediaStream)
  return vid.play(); // returns a Promise
})
.then(()=>{ // enable the button
  const btn = document.querySelector('button');
  btn.disabled = false;
  btn.onclick = e => {
    takeASnap()
    .then(download);
  };
})
.catch(e=>console.log('please use the fiddle instead'));

function takeASnap(){
  const canvas = document.createElement('canvas'); // create a canvas
  const ctx = canvas.getContext('2d'); // get its context
  canvas.width = vid.videoWidth; // set its size to the one of the video
  canvas.height = vid.videoHeight;
  ctx.drawImage(vid, 0,0); // the video
  return new Promise((res, rej)=>{
    canvas.toBlob(res, 'image/jpeg'); // request a Blob from the canvas
  });
}
function download(blob){
  // uses the <a download> to download a Blob
  let a = document.createElement('a'); 
  a.href = URL.createObjectURL(blob);
  a.download = 'screenshot.jpg';
  document.body.appendChild(a);
  a.click();
}

<button>take a snapshot</button>
<video id="vid"></video>

作为小提琴,因为Stacksnippets可能会阻止gUM请求...

As a fiddle since Stacksnippets may block gUM requests...

要另存为视频,可以使用[MediaRecorder API](https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorderà,这将使您将MediaStream保存为webm:

And to save as a video, you can use the [MediaRecorder API](https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorderà, which will allow you to save a MediaStream as webm:

const vid = document.querySelector('video');
navigator.mediaDevices.getUserMedia({video: true}) // request cam
.then(stream => {
  vid.srcObject = stream; // don't use createObjectURL(MediaStream)
  return vid.play(); // returns a Promise
})
.then(()=>{ // enable the button
  const btn = document.querySelector('button');
  btn.disabled = false;
  btn.onclick = startRecording;
})
.catch(e=>console.log('please use the fiddle instead'));

function startRecording(){
  // switch button's behavior
  const btn = this;
  btn.textContent = 'stop recording';
  btn.onclick = stopRecording;
  
  const chunks = []; // here we will save all video data
  const rec = new MediaRecorder(vid.srcObject);
  // this event contains our data
  rec.ondataavailable = e => chunks.push(e.data);
  // when done, concatenate our chunks in a single Blob
  rec.onstop = e => download(new Blob(chunks));
  rec.start();
  function stopRecording(){
    rec.stop();
    // switch button's behavior
    btn.textContent = 'start recording';
    btn.onclick = startRecording;
  }
}
function download(blob){
  // uses the <a download> to download a Blob
  let a = document.createElement('a'); 
  a.href = URL.createObjectURL(blob);
  a.download = 'recorded.webm';
  document.body.appendChild(a);
  a.click();
}

<button disabled>start recording</button>
<video></video>

作为小提琴

注意:

MediaRecorder API仍然是一个相当新的API,[少量浏览器实现中仍然存在一些错误.

The MediaRecorder API is still a quite new API and there are still some bugs in the [little set of browser implementations.

这篇关于如何使用HTML5将用网络摄像头捕获的jpg图像/视频保存在本地硬盘中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆