Wednesday, March 8, 2017

How do i check for low resolution photos when using cordova camera plugin?

Leave a Comment

I have the following code. It pretty much works for hte most part. I'd like to catch people with crappy cameras and some how warn them their pictures will stink. (using "cordova-plugin-camera" version 2.3.0)

           var cameraOpts = {                 quality: 100,                 // destinationType: Camera.DestinationType.FILE_URI,                 destinationType: Camera.DestinationType.DATA_URL,                 sourceType: $scope.sourceType,                 allowEdit: false,                 encodingType: Camera.EncodingType.JPEG,                 // popoverOptions: CameraPopoverOptions,                 targetWidth: 186,                 targetHeight: 1024,                 saveToPhotoAlbum: true,                 correctOrientation: true             };              $cordovaCamera.getPicture(cameraOpts).then(function(imageData) {                 var image = "data:image/jpeg;base64," + imageData;                 $scope.setUpImage(index,image);              }, function(err) {                 // error                 $scope.showAlert('Warning!', 'Camera cancelled!');             }); 

Any ideas would be appreciated. I'm looking for a way to catch low resolution photos, and message the user.

2 Answers

Answers 1

cordova camera plugin provide quality camera option but it does not provide any information about resolution(default is 100) (enter link description here)

Answers 2

You should create an Image object (this is standard HTML5 API)

Relevant snippet copied from this answer:

HTML5 - how to get image dimension

From plugin doc: "The image is passed to the success callback as a Base64-encoded String, or as the URI for the image file"

If you choose base64 format in the camera plugin, the data URI would be

'data:image/jpg;base64,' + imageData 

However, the plugin doc recommends the file format, which will return a file URL that you can assign directly to your image.src

var image = new Image();         image.onload = function(evt) {             var width = this.width;             var height = this.height;             alert (width); // will produce something like 198         };          //here is where you pass the camera data to create the image image.src = imageData;  

As you can see, the method is asynchronous, you can wrap it in a Promise or emit an event when the image has been loaded and you have the width x height

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment