bensonruan / webcam-easy

javascript access webcam stream and take photo
https://bensonruan.com/how-to-access-webcam-and-take-photo-with-javascript
MIT License
241 stars 110 forks source link
computer-vision javascript npm npm-module npm-package webcam webcam-capture webcam-streaming webcamera

Webcam Easy JS

This is a javascript library for accessing webcam stream and taking photos.

You can easily add it as a module to your own app.

Live Demo

https://bensonruan.com/how-to-access-webcam-and-take-photo-with-javascript/

webcam-easy-demo

Installation

Use Git Clone

git https://github.com/bensonruan/webcam-easy.git

OR Use NPM

NPM

npm install webcam-easy

Usage

1. Include script tag in html

<script type="text/javascript" src="https://unpkg.com/webcam-easy/dist/webcam-easy.min.js"></script>
or Import into javascript
import Webcam from 'webcam-easy';

2. Place elements in HTML

<video id="webcam" autoplay playsinline width="640" height="480"></video>
<canvas id="canvas" class="d-none"></canvas>
<audio id="snapSound" src="https://github.com/bensonruan/webcam-easy/raw/master/audio/snap.wav" preload = "auto"></audio>

3. Call constructor in javascript

const webcamElement = document.getElementById('webcam');
const canvasElement = document.getElementById('canvas');
const snapSoundElement = document.getElementById('snapSound');
const webcam = new Webcam(webcamElement, 'user', canvasElement, snapSoundElement);

4. Start Webcam

webcam.start()
   .then(result =>{
      console.log("webcam started");
   })
   .catch(err => {
       console.log(err);
   });

5. Take Photo

var picture = webcam.snap();

6. Stop Webcam

webcam.stop();

Functions

Properties

Support me

ko-fi