View
 

iPhone: Camera API

Page history last edited by MingGL 2 days ago
The function returns a base64 encoded string representation of the image taken using the camera (or photo library), in JPEG format. There is an option to specify JPEG quality (0 for max compression up to 100 for min compression).
 
As you can see from the code below, the base64 data is used in a data url for an <img>. The image shows up fine in UIWebView. 
 
-------
VERSION NOTE:
As of today, in the Edge source, there is support for a "sourceType" option (see code below). For non-Edge versions (pre-0.8.2), grab the patch here
 
The code below is backwards-compatible for iPhone PhoneGap versions below 0.8.2.
------- 
 
Sample javascript (put in a script tag somewhere):
 
function PictureSourceType() {};
PictureSourceType.PHOTO_LIBRARY = 0;
PictureSourceType.CAMERA = 1;
 
function getPicture(sourceType)
{
     var options = { quality: 10 };
     if (sourceType != undefined) {
          options["sourceType"] = sourceType;
     }
     // if no sourceType specified, the default is CAMERA 
     navigator.camera.getPicture(getPicture_Success, null, options);
};

 

function getPicture_Success(imageData)
{
         alert("getpic success");
        document.getElementById("test_img").src = "data:image/jpeg;base64," + imageData;
}
 
------
In your <body> put: 
 

<img style="width:60px;height:60px" id="test_img" src="" /> 

 

<!-- for testing, add the buttons below -->

 

<button onclick="getPicture()">From Camera</button>

<button onclick="getPicture(PictureSourceType.PHOTO_LIBRARY)">From Photo Library</button>

 

 

Comments (19)

profile picture

pixelpossible said

at 10:41 am on Nov 27, 2009

I am assuming this is only possible with the PhoneGap framework and not part of native navigator object. Is this true? Thanks!

profile picture

Shazron Abdullah said

at 10:44 am on Nov 27, 2009

Yes of course.

profile picture

pixelpossible said

at 10:51 am on Nov 27, 2009

Excellent! Thanks Shazron!

profile picture

venkat said

at 2:20 am on Jan 4, 2010

hey i am using the EDGE version. This camera API works on index.html but not in other pages... I thought this problem was fixed in EDGE isnt it??

profile picture

Shazron Abdullah said

at 11:15 am on Jan 4, 2010

Are you looking for the deviceready event? There is a FAQ item about the index.html stuff.

profile picture

venkat said

at 9:42 pm on Jan 4, 2010

i am calling it from a click event not onload so i guess phonegap must have loaded by then.

profile picture

venkat said

at 12:49 am on Jan 5, 2010

ok used deviceready to just give it a try and it worked... but i need the image extension to save the image in my server.. How do i get that??

profile picture

Ben Bowler said

at 8:51 am on Jan 10, 2010

I've implemented this but the Photo Library button doesn't work. Am I being idiotic?

profile picture

Martijn Pannevis said

at 9:07 am on Jan 28, 2010

How about video's? I can imagine the base64 encoded string of a video would be huge, but I'd like to be able to record one..

profile picture

Todd Blanchard said

at 3:18 pm on Jan 28, 2010

This fails on the current edge with iPhone OS 3.01. The camera comes up, you can take the picture, then the "use/retake" preview view comes up and the device freezes - eventually it runs out of memory and is killed by the OS. So, right now, it does not work.

profile picture

RambOe said

at 3:19 am on Feb 9, 2010

@Todd Blanchard
Same problem here.

profile picture

Travis said

at 12:37 pm on Mar 31, 2010

I put this code into the default site that comes with phonegap and nothing happens I was using version 0.8.0 simulated in xcode in all 3.0 versions of iphone. Anybody have an idea!

profile picture

Travis said

at 12:48 pm on Apr 1, 2010

Oh the simulator doesn't have camera support. f

profile picture

Travis said

at 1:17 pm on Apr 3, 2010

I can't seem to call getPictureFromLibrary i have version 0.8 installed. When I download the edge version it has folders but its empty and I can't download any patch. getPicture works fine though.

function getPicture()
{
navigator.camera.getPicture(getPicture_Success, null, { quality: 10 });
};
function getPicture2()
{
navigator.camera.getPictureFromLibrary(getPicture_Success, null, null);
};

Can anybody help! Thanks.

profile picture

George C Brackett said

at 10:17 am on Jun 3, 2010

The source code provided works fine for me. However, it either (a) compreses or stretches the image to fit a defined <img> width and height, or (b) it displays the image full size if the <img> tag has no specified width and height. I would like to display the image in a defined box (i.e., a <div>) such that it is sized to fit, leaving empty spaces if necessary, as the jquery plugin jquery.scale promises to do. Neither that plugin no another I have tried will resize the image, even after waiting until the image is fully displayed and then initiating a resize via a button. The problem seems to be that the surrounding <div> has an undefined width, for example. Anybody know how to display the image from the camera or the library within the boundaries of a <div> without distortion?

profile picture

George C Brackett said

at 4:49 pm on Jun 4, 2010

Further, the dimensions of the newly-sourced image are 0 x 0 pixels at this point:

function getPicture_Success(imageData)
{
var newPhoto = new Image();
newPhoto.src = 'data:image/jpeg;base64,' + imageData;
var imgWidth = newPhoto.width;
var imgHeight = newPhoto.height;
alert(imgWidth + 'x' + imgHeight); // gives 0 x 0
}

Later, using an onload call fro the <img> tag

<img id="test_img" src="" style="position: relative;" onload="resizeImage()">

the resizeImage function gets the dimensions of the PREVIOUS image, not the new one from the camera or library. Frankly, I'm stuck -- I can't resize because I don't know the dimensions of the camera or library image. I can set the size of the new image, for example

$('#test_img').attr('width', 300);
$('#test_img').attr('height', 380);

but that just gives me a squozed image, not what I want. Any hints from the gurus out there?

profile picture

Francois Simard said

at 12:00 pm on Jul 19, 2010

Just tested this with an iPod Touch and i can't get the image library to work. Is that even available using ipod touch?

profile picture

MingGL said

at 2:40 pm on Jul 27, 2010

This doesn't seem to work anymore in the latest SDK (I'm using SDK 4.0.1). imageData = "(null)" when the success callback is invoked.

You don't have permission to comment on this page.