Android Image Compression Summary

Source: Internet
Author: User

First of all, the article is summary, not original, is by looking at the online other great God's articles and some of their own practice summed up. I. The form of the picture 1. File form (that is, in binary form on the hard disk)
2. The form of the stream (that is, in binary form in memory)
3.Bitmap form of these three forms of difference: the form of files and the format of the stream does not affect the size of the picture, that is, if your phone SD card if it is 100K, then through the form of stream read into memory, it must be the memory of 100K, note is the form of flow, not the form of bitmap , when the picture in the form of bitmap, its memory will be suddenly larger, I tried 500K file form of the image loaded into memory, in the form of bitmap, occupy memory will be nearly 10M, of course, this increase in multiples is not fixed detection of the image of the three forms of the size of the method: File Form: File.length () stream in the form of: Speak the picture file read into the memory input stream, see its byte number Bitmap:bitmap.getByteCount () two. Common compression Method 1. When the picture is saved to local compression, the picture from the bitmap form into file form compression, the feature is: The file form of the picture is really compressed, but when you re-read the compressed file is bitmap, it occupies the memory does not change [Java]View Plaincopy
  1. Public static void Compressbmptofile (Bitmap bmp,file File) {
  2. Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
  3. int options = N; Personally like starting from 80,
  4. Bmp.compress (Bitmap.CompressFormat.JPEG, Options, BAOs);
  5. While (Baos.tobytearray (). Length/ 1024x768 > ) {
  6. Baos.reset ();
  7. Options-= 10;
  8. Bmp.compress (Bitmap.CompressFormat.JPEG, Options, BAOs);
  9. }
  10. try {
  11. FileOutputStream fos = new FileOutputStream (file);
  12. Fos.write (Baos.tobytearray ());
  13. Fos.flush ();
  14. Fos.close ();
  15. } catch (Exception e) {
  16. E.printstacktrace ();
  17. }
  18.     }  
Method Description: The method is to compress the quality of the picture, note that it does not reduce the image of the pixel, for example, your picture is 300K, 1280*700 pixels, after the method is compressed, file form of the picture is under 100 to facilitate the uploading of the server, But when you bitmapfactory.decodefile into memory and become bitmap, its pixels are still 1280*700, and the method of calculating the pixels of the image is Bitmap.getwidth () and Bitmap.getheight (), A picture is made up of pixels, and what does each pixel contain?  People familiar with PS know that the picture is composed of hue, lightness and saturation. The official document of the method also explains that it will let the picture be reconstructed, but it is possible that the image's bit depth (i.e. color depth) and the transparency of each pixel will change, JPEG onlysupports opaque (opaque), that is, after compressing in JPEG format, The transparent elements in the original picture will disappear. So this format is likely to cause distortion since it is changing the image display quality, to the file form of the image compression, the image of the pixel has not changed, then re-read the compressed file is bitmap, it consumes less memory. (Do not believe can try) because: Bitmap.getbytecount () is the memory used to calculate its pixels, see official explanation: Returns the number of bytes used to store this bitmap ' s pixels . 2. When the picture is read from the local memory, compression, that is, the picture from the file form into the bitmap form feature: By setting the sampling rate, reduce the pixels of the picture, to bitmap in memory to compress first look at a method: The method is to the memory of the bitmap in the qualitative The amount of compression, by the above theory can be concluded that the method is invalid, and it is not necessary, because you have read it into memory, and then compress superfluous, although in the acquisition System album pictures, some phones will directly return a bitmap, but in this case, The returned bitmap are compressed, it is not possible to return directly to an acoustic bitmap form of the picture, the consequences can be imagined [Java]View Plaincopy
  1. Private Bitmap compressbmpfrombmp (Bitmap image) {
  2. Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
  3. int options = 100;
  4. Image.compress (Bitmap.CompressFormat.JPEG, BAOs);
  5. While (Baos.tobytearray (). Length/ 1024x768 > ) {
  6. Baos.reset ();
  7. Options-= 10;
  8. Image.compress (Bitmap.CompressFormat.JPEG, Options, BAOs);
  9. }
  10. Bytearrayinputstream ISBM = new Bytearrayinputstream (Baos.tobytearray ());
  11. Bitmap Bitmap = Bitmapfactory.decodestream (ISBM, null, null);
  12. return bitmap;
  13. }
Look at one more way: [Java]View Plaincopy
  1. private Bitmap Compressimagefromfile (String srcpath) {
  2. Bitmapfactory.options newopts = new Bitmapfactory.options ();
  3. Newopts.injustdecodebounds = true; Read-only edges, not read content
  4. Bitmap Bitmap = Bitmapfactory.decodefile (Srcpath, newopts);
  5. Newopts.injustdecodebounds = false;
  6. int w = newopts.outwidth;
  7. int h = newopts.outheight;
  8. float hh = 800f; //  
  9. float ww = 480f; //  
  10. int be = 1;
  11. if (w > H && w > ww) {
  12. be = (int) (NEWOPTS.OUTWIDTH/WW);
  13. } Else if (W < h && h > hh) {
  14. be = (int) (NEWOPTS.OUTHEIGHT/HH);
  15. }
  16. if (be <= 0)
  17. be = 1;
  18. Newopts.insamplesize = be; //Set sample rate
  19. Newopts.inpreferredconfig = config.argb_8888; //This mode is default and can not be set
  20. Newopts.inpurgeable = true; Both settings are valid
  21. Newopts.ininputshareable = true; 。 Images are automatically recycled when system memory is not enough
  22. Bitmap = Bitmapfactory.decodefile (Srcpath, newopts);
  23. Return compressbmpfrombmp (bitmap);//The original method calls this method to attempt to compress two times
  24. //is actually invalid, everyone despite trying
  25. return bitmap;
  26. }


Method Description: The method is to compress the image in the form of bitmap, that is, by setting the sampling rate to reduce the bitmap pixels, thus reducing the original memory that it occupies: http://blog.csdn.net/cherry609195946/article/ details/9264409

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.