< Back to | Open article on dev.to

How to capture picture using JavaScript | Webcam Js Tutorial

Hello, guys In this tutorial we will try to solve the mentioned query. and also we will learn how to capture picture using JavaScript.

Common Query

  1. How to capture picture using JavaScript?
  2. How to capture a webcam image using JavaScript?
  3. How to draw a snapshot of a webcam in HTML?

For capturing pictures using JavaScript, first we need the Webcam JS library

See Also:- How to Integrate Webcam using JavaScript

What is webcam js?

Webcam.js is an Open Source JavaScript library that allows us to capture a picture from the webcam. It uses HTML5 getUserMedia API to capture the picture.

Webcam Js Quick Start Guide

We need to host the webcam.js and webcam.swf files on your web server, and drop in this HTML snippet:

<script src="webcam.js"></script>
<div id="camera"></div>
<div id="snapShot"></div>
<script language="JavaScript">
    Webcam.attach( '#camera' );

    takeSnapShot = function() {
      Webcam.snap(function(data_uri) {
        document.getElementById('snapShot').innerHTML = 
        '<img src=" ' +data_uri+' " width="400" height="400">';
      })
    }
</script>
<input type="button" value="" id="cameraBtn" onclick="takeSnapShot()">
Enter fullscreen mode Exit fullscreen mode

This will create a live camera view in the #camera DIV, and when the Take Snapshot link is clicked it will take a still snapshot, convert it to a JPEG, and deliver a Data URI which is inserted into the #snapShot DIV as a standard <img> tag.

Webcam Js Configuration

If you want to change the default settings, just call Webcam.set() and pass in a hash with any of the following keys:

Height    : Auto
Width   : Auto
dest_width :    Auto
dest_height :   Auto
crop_width :    Disabled
crop_height :   Disabled
image_format :  jpeg
force_flash :   false
jpeg_quality :  90
Enter fullscreen mode Exit fullscreen mode

I will show you an example of overriding some parameters. Remember to call this before you attach the viewer.

Webcam.set({
    width:650,
    height:310,
    dest_width: 1300,
    dest_height: 620,
    image_format: 'jpeg',
    jpeg_quality: 90,
    force_flash: false   
});
Enter fullscreen mode Exit fullscreen mode

Capture picture using JavaScript Video Output

Capture picture using JavaScript Codepen Output

We will update soon:)

Check Full Article With Source Code

Hello, guys In this tutorial we will try to solve the mentioned query. and also we will learn how to...