SANSONAN
SANSONAN

Reputation: 41

how to use camera plugin cordova 3.4 with platform android

I want to use camera plugin cordova 3.4. I need two options. First it can take a photo with camera and Second i can select photo in gallery.

This is my code that i use only camera

  function Photo(id, data, format) {
        this.id = id;
        this.data = data;
        this.format = format || "png";
        this.name = function() {
            var date = new Date();
            return "" + date.getTime() + "_" + this.id + "." + this.format;
        };
    }

    SiteCamera = {
        dataWithMimeType: function(data) {
            return 'data:image/png;base64,' + data;
        },
        takePhoto: function(idField, updated) {
            SiteCamera.id = idField;
            SiteCamera.updated = updated;

            navigator.camera.getPicture(SiteCamera.onSuccess, SiteCamera.onFail, {
                quality: 50,
                destinationType: Camera.DestinationType.DATA_URL,
                sourceType : Camera.PictureSourceType.CAMERA,
                encodingType: Camera.EncodingType.JPEG
            });
        },
        onSuccess: function(imageData) {
            var imageId = SiteCamera.updated ? "update_" + SiteCamera.id : SiteCamera.id;
            var image = document.getElementById(imageId);
            var photo = new Photo(SiteCamera.id, imageData);
            image.src = SiteCamera.dataWithMimeType(imageData);
            PhotoList.add(photo);
        },
        onFail: function() {
            alert("Failed");
        }
    };

Who can help me i want function that can allow me to use camera option or select photo in gallery. The code that i show all everyone just only chose. if i use 1st option i can use this option only but i need both but i don't know how to do it.

Upvotes: 0

Views: 966

Answers (1)

manukv
manukv

Reputation: 2031

 var destinationType = navigator.camera.DestinationType;
 var source = navigator.camera.PictureSourceType.PHOTOLIBRARY;

 navigator.camera.getPicture(function (imageURI) { 


                                   /*Success callback*/


                             }, 
                             function (e) { 

                                   /*Fail callback*/

                             }, 
                             {
                              quality: 100,
                              destinationType: destinationType.FILE_URI,
                              sourceType: source
                              });

You can get image from Camera, Photo Library and Album, to do that just change the source type.

 source = navigator.camera.PictureSourceType.PHOTOLIBRARY; //From PhotoLibrary
 source = navigator.camera.PictureSourceType.SAVEDPHOTOALBUM; //From Album
 source = navigator.camera.PictureSourceType.CAMERA; //From Camera(Default)

Upvotes: 1

Related Questions