Camera bitmap compression

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

This will not work, because I get Bitmap () to return the image to large to highlight, and I don't want to fit it 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 full memory?

Update:

For those returning to this topic, I followed Phil and used Apache MultiPartEntity to make the job easy. (It processes stream files from disk to network for you) http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntity.html

+4
source share
5 answers

Since you have not defined what “send over the network” means, it’s hard to give specific advice.

However, if you want to send an image without loss (or with less loss than has already happened), I don’t think you can compress much more, because JPEG is already compressed. It uses lossy compression, so if you increase JPEG compression, you lose details (although maybe not the ones you would notice, because they are based on non-pixel frequencies)

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

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

Best wishes,

Phil Lello

+2
source

Perhaps it makes sense to use zipping before sending over the network? The inflaterInputStream on the sender side and the DeflaterOutputStream on the receiver side looks like a working combination.

ADDITION Try a different 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 
+3
source

You can use the lower version of the image, i.e. 512x384 resolution (mini-sketch). Mark this answer on how to get miniatures.

Thumbnails are already compressed to 80%.

0
source

I think you can only do this in native code, where you can allocate a lot more memory. This means that you will need to use and compile a native library, such as libjpeg , or some kind of complete package, such as ImageMagick , to support reading from various image formats.

However, you will not use the Bitmap class at all for compression. Your own code will take care of reading and writing image files. If you manage to compile the library, your own code should not exceed several tens of lines.

If you do not know C and have already played with JNI and Android NDK , this will not be easy. Especially, compiling third-party libraries for Android can be difficult at times.

But I believe that switching to the native language is the only solution here.

0
source

Perhaps a preliminary splitting of the image; compression of each individual and restoration at the end of sounds is logical.

Here are a few attempts

http://kalanir.blogspot.com/2010/02/how-to-split-image-into-chunks-java.html

and

http://www.anddev.org/multimedia-problems-f28/chunk-of-a-big-big-image-t6211.html

0
source

Source: https://habr.com/ru/post/1345972/


All Articles