7

I have a phonegap application that has multiple input fields including textboxes/textareas/signatures/cad sketches/camera images. This data is uploaded to the server via an ajax post.

Problem:

When uploading the data the file is unreasonably large and takes a great deal of time to upload due to the images. When no photos are added the size is around 200kb - 400kb but with images the file has reached 60Mb and in terms of using an android device with a poor internet connection (Area with bad signal) this is unusable.

What I have tried:

I have used image compression in the camera options as seen in the below extract of code, however the challenge I am facing is that different devices with different camera specifications are utilized so if the device has a poor quality camera vs a device with a good quality camera image compression will take place regardless so the image quality will be terrible.

 var cameraOptions = {
                    destinationType: Camera.DestinationType.DATA_URL,
                    quality: 40,
                    encodingType: 0,
                };

Reference to the phonegap camera API and options:

http://docs.phonegap.com/en/2.0.0/cordova_camera_camera.md.html#Camera

From reading the below post I deduced that jpeg is the lightest format so used the endodingType of 0

PNG vs. GIF vs. JPEG vs. SVG - When best to use?

What we want to achieve:

Limiting the image size to maximum of 500kb per image and if its larger then than that compress it.

Community
  • 1
  • 1
Chad Bonthuys
  • 2,001
  • 3
  • 25
  • 33
  • What in the world are you doing to land JPG images that are that large? Or are you trying to upload a collection of images from a single form (even then, I would expect it to take at least 20 images to get anywhere near 60mb, and that's pushing it quite a bit). – Kerri Shotts May 15 '14 at 17:57
  • Correct its a single dynamically built form of image fields that are used to capture photos with aproximately 15 - 25 images – Chad Bonthuys May 15 '14 at 18:13
  • 1
    Do you need the images in their original sizes? Why not limit the width/height of the captured image using targetWidth/targetHeight? It might not guarantee a 500kb image, but it would certainly help. – Kerri Shotts May 15 '14 at 18:20
  • Having the images in the original sizes is not necessary. I was thinking of trying that and setting the width/height as a cameraOption on photo capture but I wasn't too sure what size I should use to retain a decent image. Additional the problem is this will only work for "new" images and previously downloaded images will still retain the old size – Chad Bonthuys May 15 '14 at 18:32
  • IIRC, Facebook used to limit photos to something on the order of 1000 pixels on the long side. I think that's changed now to 2000 to better support retina displays, but if you don't need a full-screen image, you could probably get away with 500-1000px. – Kerri Shotts May 15 '14 at 18:44
  • This plugin might be of use for resizing images after-the-fact (you could draw the image to a smaller canvas and save that data off): http://plugins.cordova.io/#/package/org.devgeeks.canvas2imageplugin – Kerri Shotts May 15 '14 at 18:48
  • Thanks for the advice limiting the resolution seemed to do the trick without loosing quality. Optimizations are not perfect as of yet but lost more to try and work on – Chad Bonthuys May 22 '14 at 14:46

1 Answers1

6

I tried with different camera options and with the following I got file sizes of 4.5MB from an iPhone 5:

navigator.camera.getPicture(onSuccess, onFail, {
    destinationType : Camera.DestinationType.DATA_URL,
    sourceType : Camera.PictureSourceType.CAMERA,
    quality : 50,
    encodingType : Camera.EncodingType.JPEG
});

Only by adding the line

correctOrientation : true

File size reduced to ~500kB.

I don't have any explanation to why it should work this way, but I suggest you try it if the quality property doesn't seem to do anything.

kirayatail
  • 69
  • 1
  • 2