Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take picture from webcam or mobile camera in web applications

I'm developing a web application which browse and take pictures from local and also I want to capture images through the camera. Im using the following code and i can capture device camera.

<input type="file" capture="camera" accept="image/*" id="cameraInput" name="cameraInput">

Now, I want to get the image and onchangeevent, convert to base64 and want to show in that page itself.

Kindly help me guys!

like image 253
ganesh Avatar asked Feb 07 '16 12:02

ganesh


People also ask

Can a web cam take pictures?

If your PC has a built-in camera or a connected webcam, you can use the Camera app to take photos and videos. To find the Camera app, select Start > Camera.

How do you take a picture of a website?

Right click that URL and click Open in new tab. 6. In the new tab, right click the image and choose Save image as. Now you can name your file and save it to your computer.


2 Answers

Miles Erickson and Henock Bongi, you need to take reader.readAsDataUrl($data); out of the onload function in order that the onload fire.

If you don't want to use jQuery see below:

function readFile(file) {                                                       
    var reader = new FileReader();
    reader.onload = readSuccess;                                            
    function readSuccess(evt) {     
        document.getElementById("your_img_id").src = evt.target.result                   
    };
    reader.readAsDataURL(file);                                              
} 

document.getElementById('cameraInput').onchange = function(e) {
    readFile(e.srcElement.files[0]);
};
like image 95
Ana Houa Avatar answered Sep 30 '22 01:09

Ana Houa


You can do it like this:

$('#cameraInput').on('change', function(e){
 $data = e.originalEvent.target.files[0];
  $reader = new FileReader();
  reader.onload = function(evt){
  $('#your_img_id').attr('src',evt.target.result);
  reader.readAsDataUrl($data);
}});
like image 43
Henock Bongi Avatar answered Sep 30 '22 02:09

Henock Bongi