Reputation: 4188
I am trying out the Camera API for Phonegap and I have ran into a problem. Using the code from the Official Documentation:
<script type="text/javascript" charset="utf-8">
var pictureSource; // picture source
var destinationType; // sets the format of returned value
// Wait for PhoneGap to connect with the device
//
document.addEventListener("deviceready",onDeviceReady,false);
// PhoneGap is ready to be used!
//
function onDeviceReady() {
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;
}
// Called when a photo is successfully retrieved
//
function onPhotoDataSuccess(imageData) {
// Uncomment to view the base64 encoded image data
// console.log(imageData);
// Get image handle
//
var smallImage = document.getElementById('smallImage');
// Unhide image elements
//
smallImage.style.display = 'block';
// Show the captured photo
// The inline CSS rules are used to resize the image
//
smallImage.src = "data:image/jpeg;base64," + imageData;
}
// Called when a photo is successfully retrieved
//
function onPhotoURISuccess(imageURI) {
// Uncomment to view the image file URI
// console.log(imageURI);
// Get image handle
//
var largeImage = document.getElementById('largeImage');
// Unhide image elements
//
largeImage.style.display = 'block';
// Show the captured photo
// The inline CSS rules are used to resize the image
//
largeImage.src = imageURI;
}
// A button will call this function
//
function capturePhoto() {
// Take picture using device camera and retrieve image as base64-encoded string
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50 });
}
// A button will call this function
//
function capturePhotoEdit() {
// Take picture using device camera, allow edit, and retrieve image as base64-encoded string
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20, allowEdit: true });
}
// A button will call this function
//
function getPhoto(source) {
// Retrieve image file location from specified source
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
destinationType: destinationType.FILE_URI,
sourceType: source });
}
// Called if something bad happens.
//
function onFail(message) {
alert('Failed because: ' + message);
}
</script>
And my button:
<button onclick="capturePhoto();">Capture Photo</button>
And img tag:
<img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
The camera opens up fine, and takes a photo no problem, however, it does not show up on the page.
I have more code that lets you select an image form the photo album, and this works perfectly, displaying it in a different image tag.
I believe the problem is that it cannot find imageData.
The captured photo does get saved to the phone, and it can be displayed using the other button, but I want it to show straight after taking the photo.
I am using JQM btw and compiling my APK using the Phonegap:Build web compiler.
Upvotes: 3
Views: 3993
Reputation: 159
In the CapturePhoto() function, add this option;
destinationType : Camera.DestinationType.FILE_URI,
As Phonegap says, using FILE_URI is the best practice for taking pictures with new generation phones.
To show the image use this approach in the getPhotoDataSuccess;
$('#smallImage').attr("src",imageURI);
Upvotes: 0
Reputation: 129
It is much easier and cleaner to use the file URI (but be careful if you want the image to persist, on iOS the images are saved to a temporary folder - http://docs.phonegap.com/en/1.7.0/cordova_camera_camera.md.html#Camera). You can display the images right away as follows:
navigator.camera.getPicture(displayPicture, function(err){}, {quality : 40,
destinationType : Camera.DestinationType.FILE_URI,
sourceType : Camera.PictureSourceType.CAMERA});
function displayPicture(file_uri){
var img_tag = '<img style="width:60px;height:60px;" id="smallImage" src="'+file_uri+'" />';
//Attach the img tag where ever you want it ... $(<some parent tag>).append(img_tag) etc.
}
Upvotes: 0
Reputation: 1313
The documentation on Phonegap is wrong. To get the base64 encoded picture you need to call
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality:50, destinationType:Camera.DestinationType.DATA_URL });
Upvotes: 5