Android more reliable image compression

Source: Internet
Author: User

First: Let's look at the mass compression method first:

Java code
  1. Private Bitmap Compressimage (Bitmap image) {
  2. Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
  3. Image.compress (Bitmap.CompressFormat.JPEG, BAOs); Mass compression method, where 100 means no compression, the compressed data stored in the BAOs
  4. int options = 100;
  5. while (Baos.tobytearray (). Length/ 1024>) { //cycle to determine if the image is larger than 100kb after compression, /c2>
  6. Baos.reset (); //Reset BAOs to Empty BAOs
  7. Image.compress (Bitmap.CompressFormat.JPEG, Options, BAOs); //Compress the options% here and store the compressed data in the BAOs
  8. options = Ten; Each time, it's reduced by ten
  9. }
  10. Bytearrayinputstream ISBM = new Bytearrayinputstream (Baos.tobytearray ()); Store the compressed data BAOs in the Bytearrayinputstream
  11. Bitmap Bitmap = Bitmapfactory.decodestream (ISBM, null, null); Generate images from the Bytearrayinputstream data
  12. return bitmap;
  13. }

Second: Picture by proportional size compression method (get picture according to Path and compress):

Java code
  1. Private Bitmap getimage (String srcpath) {
  2. Bitmapfactory.options newopts = new Bitmapfactory.options ();
  3. ///start reading the picture and set the Options.injustdecodebounds back to True
  4. Newopts.injustdecodebounds = true;
  5. Bitmap Bitmap = Bitmapfactory.decodefile (srcpath,newopts); //Return BM to NULL at this time
  6. Newopts.injustdecodebounds = false;
  7. int w = newopts.outwidth;
  8. int h = newopts.outheight;
  9. //Now the mainstream phone is more 800*480 resolution, so the height and width we set to
  10. float hh = 800f; //Set height to 800f here
  11. float ww = 480f; //Set width to 480f here
  12. //Zoom ratio. Because it is a fixed scale, only one of the data with high or wide is calculated
  13. int be = 1; Be=1 indicates no scaling
  14. If (W > H && w > ww) {//If the width is large, zoom according to the fixed width size
  15. be = (int) (NEWOPTS.OUTWIDTH/WW);
  16. } Else if (W < h && h > hh) {//if height is scaled according to width fixed size
  17. be = (int) (NEWOPTS.OUTHEIGHT/HH);
  18. }
  19. if (be <= 0)
  20. be = 1;
  21. Newopts.insamplesize = be; //Set zoom ratio
  22. //re-read the picture, note that the options.injustdecodebounds is now set back to False
  23. Bitmap = Bitmapfactory.decodefile (Srcpath, newopts);
  24. return Compressimage (bitmap); //compress the proportional size before compressing the mass.
  25. }

Third: Image compression by proportional size (according to bitmap image compression):

Java code
  1. Private Bitmap Comp (Bitmap image) {
  2. Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
  3. Image.compress (Bitmap.CompressFormat.JPEG, BAOs);
  4. if (Baos.tobytearray (). Length/ 1024>) {//Judging if the picture is greater than 1M, Compress to avoid overflow when creating pictures (Bitmapfactory.decodestream)
  5. Baos.reset (); //Reset BAOs to Empty BAOs
  6. Image.compress (Bitmap.CompressFormat.JPEG, BAOs); This compresses 50% and stores the compressed data in the BAOs .
  7. }
  8. Bytearrayinputstream ISBM = new Bytearrayinputstream (Baos.tobytearray ());
  9. Bitmapfactory.options newopts = new Bitmapfactory.options ();
  10. ///start reading the picture and set the Options.injustdecodebounds back to True
  11. Newopts.injustdecodebounds = true;
  12. Bitmap Bitmap = Bitmapfactory.decodestream (ISBM, null, newopts);
  13. Newopts.injustdecodebounds = false;
  14. int w = newopts.outwidth;
  15. int h = newopts.outheight;
  16. //Now the mainstream phone is more 800*480 resolution, so the height and width we set to
  17. float hh = 800f; //Set height to 800f here
  18. float ww = 480f; //Set width to 480f here
  19. //Zoom ratio. Because it is a fixed scale, only one of the data with high or wide is calculated
  20. int be = 1; Be=1 indicates no scaling
  21. If (W > H && w > ww) {//If the width is large, zoom according to the fixed width size
  22. be = (int) (NEWOPTS.OUTWIDTH/WW);
  23. } Else if (W < h && h > hh) {//if height is scaled according to width fixed size
  24. be = (int) (NEWOPTS.OUTHEIGHT/HH);
  25. }
  26. if (be <= 0)
  27. be = 1;
  28. Newopts.insamplesize = be; //Set zoom ratio
  29. //re-read the picture, note that the options.injustdecodebounds is now set back to False
  30. ISBM = New Bytearrayinputstream (Baos.tobytearray ());
  31. Bitmap = Bitmapfactory.decodestream (ISBM, null, newopts);
  32. return Compressimage (bitmap); //compress the proportional size before compressing the mass.
  33. }

Android more reliable image compression

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.