View
 

iPhone: Camera API

Page history last edited by AveMaria 1 year, 3 months 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>