Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compress Bitmap from Camera

I need to send an image taken from the Camera over network. The image is too large to create a bitmap needed to use bitmap.compress(); It looks like the Gmail application can attach images from the camera while maintaining their large pixel size but with a great reduction their file size.

This won't work because I getBitmap() will return an Image to large to allocate and I don't want to sub sample it down to a smaller size.

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    getBitmap().compress(Bitmap.CompressFormat.JPEG, 70, baos);

Any ideas on how I can do the same without exceeding my total memory?

Update:

For anyone coming back to this thread I followed Phil's answer and used Apache MultiPartEntity to get the job done easily. (It handles streaming files from disk to network for you) http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntity.html

like image 733
sgarman Avatar asked Mar 31 '11 01:03

sgarman


2 Answers

Probably it'll make a sense to use zipping before sending over network? InflaterInputStream on a sender side and DeflaterOutputStream on receiving side looks like workable combination.

ADDENDUM Try to use another approach:

FileOutputStream fos = new FileOutputStream(file); //intermediate file to store compressed image
InputStream is=this.getStream(imageUri); //image uri taken from camera
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize=2; //try to decrease decoded image
options.inPurgeable=true; //purgeable to disk
Bitmap bitmap=BitmapFactory.decodeStream(is, null, options);
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos); //compressed bitmap to file
like image 178
Barmaley Avatar answered Sep 24 '22 14:09

Barmaley


As you haven't defined quite what 'send over the network' means here, it's difficult to give specific advice.

However, if you want to send a lossless image (or with less loss than has already occurred), I don't think you'll be able to compress much more, as JPEG already compresses. It uses lossy compression, so if you increase the JPEG compression, you lose details (although maybe not ones you'd notice, as it's based on frequencies not pixels)

If you just need to send over the network, then why not just open an InputStream, and spool data directly to the network?

Hope this helps - if you can provide more details, I'll update the answer.

Best wishes,

Phil Lello

like image 24
Phil Lello Avatar answered Sep 22 '22 14:09

Phil Lello