Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

How to Take Picture From Webcam in Javascript Using Webcam.js | Webcam Capture in Javascript

Posted on January 14, 2023

 

 

SOURCE CODE

 

 

First of all you need to make an index.html file and copy paste the following code.

 

 

index.html

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<!-- CSS -->
<style>
    #my_camera{
        width: 320px;
        height: 240px;
        border: 1px solid black;
    }
    </style>
    
    <!-- -->
    <div id="my_camera"></div>
    <input type=button value="Take Snapshot" onClick="take_snapshot()">
    
    <div id="results" ></div>
    
    <!-- Script -->
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/webcamjs/1.0.26/webcam.min.js"></script>
    
    <!-- Code to handle taking the snapshot and displaying it locally -->
    <script language="JavaScript">
    
     // Configure a few settings and attach camera
     Webcam.set({
         width: 320,
         height: 240,
         image_format: 'jpeg',
         jpeg_quality: 90
     });
     Webcam.attach( '#my_camera' );
    
     // preload shutter audio clip
     var shutter = new Audio();
     shutter.autoplay = true;
     shutter.src = navigator.userAgent.match(/Firefox/) ? 'shutter.ogg' : 'shutter.mp3';
    
    function take_snapshot() {
       // play sound effect
       shutter.play();
    
       // take snapshot and get image data
       Webcam.snap( function(data_uri) {
          // display results in page
          document.getElementById('results').innerHTML =
              '<img src="'+data_uri+'"/>';
        });
    }
    </script>

 

 

As you can see we are including the webcam.js cdn and then we have the simple option to display the live webcam feed of the user webcam and then we have the button to capture the image of the user and then we are displaying the image of the user as shown below

 

 

 

Recent Posts

  • Android Java Project to Crop,Scale & Rotate Images Selected From Gallery and Save it inside SD Card
  • Android Kotlin Project to Load Image From URL into ImageView Widget
  • Android Java Project to Make HTTP Call to JSONPlaceholder API and Display Data in RecyclerView Using GSON & Volley Library
  • Android Java Project to Download Youtube Video Thumbnail From URL & Save it inside SD Card
  • Android Java Project to Embed Google Maps & Add Markers Using Maps SDK
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme