Compress Bitmap from Camera

15,772

Solution 1

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

Solution 2

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
Share:
15,772
sgarman
Author by

sgarman

Updated on June 04, 2022

Comments

  • sgarman
    sgarman almost 2 years

    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