Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing camera view inside html in android and then snap a picture

Is there a view of showing the live camera view inside html ( e.g. embedded in a div ) before we snap a picture using JavaScript? I have tried PhoneGap but it totally starts a new camera app and totally moves away from my html web app before returning to it. I need something embedded in my app

Thanks

like image 635
Imran Omar Bukhsh Avatar asked Jan 05 '13 20:01

Imran Omar Bukhsh


People also ask

How do you open camera through intent and display captured image?

The opening of the Camera from inside our app is achieved with the help of the ACTION_IMAGE_CAPTURE Intent of MediaStore class. This image shows the Image clicked by the camera and set in Imageview. When the app is opened, it displays the “Camera” Button to open the camera.

How do I access camera on Android?

Tap the app drawer icon. It's the icon made of 6 to 9 small dots or squares at the bottom of the home screen. This opens the list of apps on your Android. If you see the Camera app on the home screen, you don't have to open the app drawer. Just tap Camera or the icon that looks like a camera.


2 Answers

You can install mbppower/CordovaCameraPreview plugin (for android,ios) in your Cordova/phonegap application that allows camera interaction from HTML code. A really amazing plugin it is.. You can access Features such as:

Start a camera preview from HTML code. Drag the preview box. Set camera color effect (Android and iOS),Send the preview box to back of the HTML content,Set a custom position for the camera preview box,Set a custom size for the preview box and Maintain HTML interactivity.

Or you can also use donaldp24/CanvasCamera Plugin for your application if it fits to your requirements.Its supported in both android and iOS platforms. I have observed, for iOS its working fine but in android it isn't working.

Now you can install CordovaCameraPreview plugin through Online PhoneGap too. So without using CLI you can directly use it through this by adding

<gap:plugin name="com.mbppower.camerapreview" version="0.0.8" source="plugins.cordova.io" />

in your config.xml file and create ApplicationTemplate.apk/.ipa. For more information regarding this you can ask me. Happy to help.

UPDATE:10th Oct 2017 It used to work well during that time but, donaldp24/CanvasCamera is no longer maintained and currently fails build with the latest Cordova version.

like image 110
Ekta Jayswal Avatar answered Sep 28 '22 10:09

Ekta Jayswal


I did it for one of my projects. Check out navigator.getUserMedia(). There are a tonne of things you can do with your webcam and browser, including AR games! Here's just a basic example:

HTML:

    <html>
    <head>
    </head>

    <body>
       <form><input type='button' id='snapshot' value="snapshot"/></form> 
       <video autoplay></video>
       <canvas id='canvas' width='100' height='100'></canvas> 
       <script type="text/javascript" src="findit.js"></script>
    </body>

    </html>

findit.js

    var onFailSoHard = function(e)
    {
            console.log('failed',e);
    }

    window.URL = window.URL || window.webkitURL ;
    navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia;

    var video = document.querySelector('video');

    if(navigator.getUserMedia)
    {
        navigator.getUserMedia({video: true},function(stream) {
        video.src = window.URL.createObjectURL(stream);
        },onFailSoHard);
    }

    document.getElementById('snapshot').onclick = function() { 
        var canvas = document.getElementById('canvas'); 
        var ctx = canvas.getContext('2d'); 
        ctx.drawImage(video,0,0); 
    } 

Live Demo:

A personal project

More Resources, this is what I used:

Webcam Access

Update:

This solution is valid only for front camera if our device has one. It's basically a "webcam" solution. Not sure if it'll work for your case. Just in case, check out the resources.

like image 26
sanjeev mk Avatar answered Sep 28 '22 11:09

sanjeev mk