Following code illustrate how to capture a photo from camera and copy it in to a local cache and then use the copied file in the app. Methods from both Phonegap Camera API and File API are used. Variable ‘root’ referred to the Application root folder and how to get the app root prefer the previous post.
Code:
function capturePhoto() {
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 100, destinationType: Camera.DestinationType.FILE_URI,correctOrientation: true, saveToPhotoAlbum: false });
}
//getting picture from camera succeeded
function onPhotoURISuccess(imageURI) {
createFileEntry(imageURI);
}
//getting picture from camera failed
function onFail(message) {
console.log("Failed to load picture because: "+message);
}
//get the image file
function createFileEntry(imageURI) {
window.resolveLocalFileSystemURI(imageURI, copyPhoto, fail);
}
//copy image in the cache
function copyPhoto(fileEntry) {
var d = new Date();
var n = d.getTime();
//new file name with unique name
var newFileName = n + ".jpg";
root.getDirectory("ImageCache", {create: true, exclusive: false}, function(dir) {
fileEntry.copyTo(dir, newFileName, onCopySuccess, fail);
}, fail);
}
//copying image succeeded
function onCopySuccess(entry) {
console.log(entry.name);
var image = document.getElementById('myImage');
image.src = entry.fullPath;
}
//copying image failed
function fail(error) {
console.log(error.code);
}
No comments:
Post a Comment