How to access multiple camera drivers using Javascript?

I need complete javascript code to access multiple camera drivers.

List of camera drivers should come in the drop down. I should be able to choose required camera driver from the drop down.
Based on my selection, i will capture the photo.

Whenver i change the drop down option, the corresponding camera driver should open

Kindly help me in building this code.

What code do you have so far, and where are you getting stuck with it?

3 Likes

I have written below code. Can you please customize this according to my requirement.

<!DOCTYPE html>
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="utf-8"%>
<%@ page import="java.io.*" %>
<html>
   <body>
   
      <video id="video"></video>
      <canvas id="canvas" width="640" height="480"></canvas>
      <div id="buttoncontent"></div>
      <button id="startbutton">CAPTURE</button>
	  <a id="link"></a>
      <script type="text/javascript">
         // Grab elements, create settings, etc.
         (function() {
         
         var streaming = false,
         video = document.getElementById("video"),
         canvas = document.getElementById("canvas"),
         buttoncontent = document.getElementById("buttoncontent"),
         //photo = document.getElementById("photo"),
         startbutton = document.getElementById("startbutton"),
         width = 320,
         height = 0;
		 
         /*
         navigator.getMedia = (navigator.getUserMedia ||
         navigator.webkitGetUserMedia ||
         navigator.mozGetUserMedia ||
         navigator.msGetUserMedia);
         
         navigator.getMedia({
           video: true,
           audio: false
         },
         function(stream) {
           if (navigator.mozGetUserMedia) {
             video.mozSrcObject = stream;
           } else {
             var vendorURL = window.URL || window.webkitURL;
             video.src = vendorURL.createObjectURL(stream);
           }
           video.play();
         },
         function(err) {
           console.log("An error occured! " + err);
         }
         );*/
		 
		 //Get Access to the camera
		if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
			// Not adding `{ audio: true }` since we only want video now
			navigator.mediaDevices.getUserMedia({ video: true }).then(function(stream) {
				video.src = window.URL.createObjectURL(stream);
				video.play();
			});
		}
         
         video.addEventListener('canplay', function(ev) {
         if (!streaming) {
           height = video.videoHeight / (video.videoWidth / width);
           video.setAttribute('width', width);
           video.setAttribute('height', height);
           canvas.setAttribute('width', width);
           canvas.setAttribute('height', height);
           streaming = true;
         }
         }, false);
         
         function takepicture() {
         video.style.display = "none";
         canvas.style.display = "block";
         startbutton.innerText= "RETAKE";
         canvas.width = width;
         canvas.height = height;
         canvas.getContext('2d').drawImage(video, 0, 0, width, height);
         var data = canvas.toDataURL('image/jpeg');  //Convert Canvas Image to URL Format(Base64)
		 console.log("Result="+data);
		 		 
		 //window.open(canvas.toDataURL('image/png'));
		 
		 <!--Save an Image Logic Starts-->
		 <!--var link = document.createElement('a');-->
		 link.innerHTML = 'download image';
		 link.href = data;
		 link.download = sessionStorage.getItem("pkColValue");//"myimage.jpg"; //try giving customer number CIF NO
		 
		 <!--Save an Image Logic Ends-->
		 
         //photo.setAttribute('src', data);
         }
         		 
		 // Trigger photo take
         startbutton.addEventListener('click', function(ev) {
         if(startbutton.innerText==="CAPTURE")
         {
             takepicture();
         }
         else
         {
             video.style.display = "block";
             canvas.style.display = "none";
           startbutton.innerText= "CAPTURE";
         }
         ev.preventDefault();
         }, false);
         
         })();
         
      </script>
   </body>
</html>

What will more likely occur, is that if it is possible to achieve what you require, someone will help you to make the required changes yourself.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.