user1455310
user1455310

Reputation:

Need to calculate the size of image in bytes - for images from phonegap camera

I am working on a functionality, where I am uplaoding an image after capturing it from native camera. Here, have a limitation regarding size of the image. So, I need to calculate the image Size in MB.

Camera.getPicture(gotPicture, cameraError, {
                                quality: 50,
                                allowEdit: true,
                                targetWidth: 200,
                                targetHeight: 200,
                                destinationType: Camera.DestinationType.DATA_URL
                            });
camera
                        .getPicture(
                            gotPicture,
                            cameraError, {
                                quality: 100,
                                allowEdit: true,
                                targetWidth: 200,
                                targetHeight: 200,
                                sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
                                destinationType: Camera.DestinationType.DATA_URL
                            });

I found lot of suggestions where it is being save as a file, but here I am receiving it as DATA URI(As I need to pass Base64 Image for processing).

Can someone help in calculating the actual size of the image clicked/referenced from PHOTO_GALLERY.

I have one more query as when I am calling the Phonegap API, Is there any compression of size is being done at the API level,

Thanks in advance!!!

Upvotes: 1

Views: 1887

Answers (1)

Homen
Homen

Reputation: 1212

You can get both size and Base64 data of the image like following:

function capturePhoto() {
   navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
   destinationType: Camera.DestinationType.FILE_URI,
   saveToPhotoAlbum: true});
}
function onPhotoDataSuccess(imageURI) {
	getSize(imageURI);
}
function onFail(message) {
   alert('Failed');
}
function getSize(fileUri) {
    window.resolveLocalFileSystemURL(
          fileUri,
          function(fileEntry){
        	  fileEntry.getMetadata(function(metadata){
        		  alert("size is"+metadata.size);
        	  }, resOnError);
        	  fileEntry.file( function(file) {
  		        var reader = new FileReader();
  		        reader.onloadend = function(evt) {
  		            alert(evt.target.result);
  		        };
  		        reader.readAsDataURL(file);
  		    }, resOnError);
          },
          resOnError);
}
function resOnError(error) {
	alert("error");
   }

You can do this for Photo gallery by setting sourceType :Camera.PictureSourceType.PHOTOLIBRARY.And for compression of size I recommend you to set lower quality in getPicture.

Upvotes: 1

Related Questions