Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choose an image from camera or gallery using phonegap

<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;


}

function capturePhoto() {

    navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 25, destinationType: 
Camera.DestinationType.FILE_URI });

}

function onPhotoURISuccess(imageURI) {

    createFileEntry(imageURI);
}

function createFileEntry(imageURI) {

    window.resolveLocalFileSystemURI(imageURI, copyPhoto, fail);    
}

function copyPhoto(fileEntry) {

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) { 
        fileSys.root.getDirectory("photos", {create: true, exclusive: false}, 

function(dir) { 

  fileEntry.copyTo(dir, "file.jpg", onCopySuccess, fail); 

            }, fail); 
    }, fail); 
}

function onCopySuccess(entry) {

    console.log(entry.fullPath)
}

function fail(error) {

    console.log(error.code);
}


    </script>
like image 788
Janmejoy Avatar asked Nov 04 '22 17:11

Janmejoy


1 Answers

You should use the PhoneGap 2.0.0 camera object. The documentation provides a full photo capturing example.

Furthermore the navigator.camera.getPicture( cameraSuccess, cameraError, [ cameraOptions ] ); takes a photo using the camera or retrieves a photo from the device's album. The image is returned as a base64 encoded String or as the URI of an image file.

I hope this helps.

like image 106
Apostolos Emmanouilidis Avatar answered Nov 12 '22 11:11

Apostolos Emmanouilidis