Capturing camera/picture data without PhoneGap

Two years later and this blog post is still highly popular. If you like this content, be sure to subscribe to the blog to get the latest updates. You may also want to check out my Apache Cordova book and the JavaScript videos I have available.

As people know, I’m a huge fan of PhoneGap and what it allows me to do with JavaScript, HTML, and CSS. But I think it is crucial to remember that you don’t always need PhoneGap. A great example of that is camera access. Did you know that recent mobile browsers support accessing the camera directly from HTML and JavaScript? Let’s look at an example.

Over a year ago I wrote a blog post in which I created an application called “Color Thief.” This application made use of PhoneGap’s Camera API and a third party JavaScript library called Color Thief. I loved this example because it demonstrated how you could combine the extra power that PhoneGap provides along with existing JavaScript libraries.

This morning I watched an excellent Google IO presentation ( on Mobile HTML. It was an overview of some of the exciting stuff you can now do with mobile HTML and JavaScript. To be clear, this was all without using wrappers like PhoneGap.

In one of the examples the presenters discussed the new “capture” support for the input/file field type. This is rather simple to implement:

<input type=”file” capture=”camera” accept=”image/*” id=”takePictureField”>

If supported (recent Android and latest iOS), the user can then use their camera to select a picture. I decided to rebuild my old demo to skip PhoneGap completely and just make use of this feature. Here’s the code:

For the most part, this is pretty similar to the last version. I no longer wait for the deviceready event, but instead just listen for the document itself to load. Instead of listening for a button click, I’ve switched to an input field using type=file. I now listen for the change event, and on that, I see if I have access to a file. If I do, I can then use the URL object to create a pointer to the source and then simply add it to my DOM. After that, Color Thief takes over.

The only tricky part I ran into was that in iOS the URL object is still prefixed. You can see how I get around that in the startup code. To be fair, this isn’t 100% backwards compatible, I could add a few checks in here to ensure that things will work and gracefully let people on older phones know they can’t use this feature.

But the end result is nearly the exact same functionality in a web page – no PhoneGap, no native code.

  • Duke Snyder

    great post ray. exactly what I needed and was implemented in less than 10 minutes using cffile. Android 4.4.2

  • Kumar Kashyap

    Hi Raymond, Thank you for sharing this article. Helped me a lot! I am trying to upload the image to my server using PHP/Javascript. Can you help me with that? It says image has been uploaded successfully but it doesnt actually get to the folder.Here is my code:

    echo $target_dir = “uploads/”;

    echo $target_file = $target_dir . basename($_FILES[“takeSinglePicture”][“name”]);

    $uploadOk = 1;

    echo $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

    echo $image_name = $_FILES[‘takeSinglePicture’][‘name’];

    //echo $imageFileType = $_FILES[‘image’][‘type’];

    echo $image_size = $_FILES[‘takeSinglePicture’][‘size’];

    echo $image_tmp_name = $_FILES[‘takeSinglePicture’][‘tmp_name’];

    if ($uploadOk == 0) {

    echo “Sorry, your file was not uploaded.”;

    echo “”;

    // if everything is ok, try to upload file

    } else {

    if($image_name == ”){

    echo “alert(‘Please Select an image’)”;





    echo “Image Uploaded Successfully!”;

    echo “”;



    • http://www.raymondcamdencom/ Raymond Camden

      I don’t use PHP, so I can’t help with this.

  • Sasha

    I guess it won’t work with KitKat

  • Amit

    is someone send me code for taking the screen shot of the mobile please

  • snabblån

    Interesting to see … thank you it’s well done :) snabblån

  • rendhon

    got it worked but it’s having a light error i think it’s not serious issue when i toke a pic this error message occur failed to open selected file.

  • Furkan Swor

    thanks !!