Android Image Compression Tips

Source: Internet
Author: User

Please respect other people's labor results, reproduced please specify the source: Android Image Compression Tips

http://blog.csdn.net/fengyuzhengfan/article/details/41759835

When the Android client needs to upload pictures to the server, often need to compress the picture, about the image compression method, small series to share several common ways:

The first way: cut to achieve the purpose of compression

I have described in detail how to crop a photo in the article "cropped photos of Android development," and interested friends can go and see it.


The second way: To reduce the quality of the image processing (that is, reducing the image of the mass) to achieve the purpose of compression

This way is also a more common way, the following for you to describe how to reduce the quality of the picture:

To reduce the quality of the picture we can use this method of Bitmap: Boolean android.graphics.Bitmap.compress (compressformat format, int quality, OutputStream Stream

Where the parameter format represents the compressed format, quality the compressed picture quality (0 for the lowest, 100 for non-compression), and the stream represents the output stream to which the compressed picture will be saved.

Here is the detailed code:

/** * Multi-threaded compressed picture quality * @author JPH * @param bitmap in-memory picture * @param the save path of Imgpath picture * @date 2014-12-5 pm 11:30:43 */public Static void Compressimagebyquality (Final Bitmap bitmap,final String imgpath) {new Thread (new Runnable () {// Turn on multithreading for compression processing @overridepublic void Run () {//TODO auto-generated method Stubbytearrayoutputstream BAOs = new Bytearrayoutput Stream (); options = 100;bitmap.compress (Bitmap.CompressFormat.JPEG, Options, BAOs);//Quality compression method, the compressed data stored in the BAOs ( 100 means no compression, 0 is compressed to a minimum) while (Baos.tobytearray (). length/1024 > 100) {//cycle to determine if the image is larger than 100kb after compression, greater than the continued compression baos.reset ();// Reset BAOs to let the next write overwrite the contents of the options-= 10;//picture quality is reduced by 10if (options<0) options=0;//If the picture quality is less than 10, The quality of the picture is compressed to the minimum bitmap.compress (Bitmap.CompressFormat.JPEG, Options, BAOs);//Save the compressed picture to BAOs if (options==0) break;// If the quality of the picture has been minimized, it is no longer compressed}try {fileoutputstream fos = new FileOutputStream (new File (Imgpath));// Save the compressed picture locally on the specified path in Fos.write (Baos.tobytearray ()); Fos.flush (); Fos.close ();} catch (Exception e) {e.printstacktrace ();}}}). Start ();}

Method parsing:

Because I/O operations and recursive calls are time-consuming in this method, I use multithreading to handle them.


The Third way: proportionally reduce the pixels of the picture to achieve the purpose of compression

This method is mainly used android.graphics. bitmapfactory . Options . The options () method loads the picture into memory at the specified adoption rate and then outputs it locally to achieve the purpose of compressing the pixels.

Detailed code:

/** * Proportionally reduces the pixels of the image for compression purposes * @author JPH * @param imgpath * @date 2014-12-5 pm 11:30:59 */public static void Compressimagebypi Xel (String imgpath) {bitmapfactory.options newopts = new Bitmapfactory.options (); newopts.injustdecodebounds = true;// Read-only edge, unread content bitmap bitmap = Bitmapfactory.decodefile (Imgpath, newopts); newopts.injustdecodebounds = false;int width = Newopts.outwidth;int height = newopts.outheight;float maxSize = 1000f;//default 1000pxint be = 1;if (width > Height &&amp ; Width > MaxSize) {//zoom ratio, compute be = (int) (newopts.outwidth/maxsize) with a high or wide one of the larger data,} else if (Width < height &&am P Height > maxSize) {be = (int) (newopts.outheight/maxsize);} Be++;newopts.insamplesize = be;//Set Sample rate Newopts.inpreferredconfig = config.argb_8888;//The mode is the default, Newopts.inpurgeable = true;//is not set to be valid newopts.ininputshareable = true;//. When the system memory is not enough, the picture is automatically recycled bitmap = Bitmapfactory.decodefile (Imgpath, newopts); Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); Bitmap.compress (Bitmap.CompressFormat.JPEG, BAOs); try {fileoutputstream fos = new FileOutputStream (new File (Imgpath)); Fos.write (Baos.tobytearray ()); Fos.flush (); Fos.close ();} catch (Exception e) {e.printstacktrace ();}}

The Fourth way: compress the picture first and then drop the quality

This approach is mainly combined with the second and third methods the following is the detailed code:

/** * Multi-threaded compressed picture quality * @author JPH * @param bitmap in-memory picture * @param the save path of Imgpath picture * @date 2014-12-5 pm 11:30:43 */public Static void Compressimagebyquality (Final Bitmap bitmap,final String imgpath) {new Thread (new Runnable () {// Turn on multithreading for compression processing @overridepublic void Run () {//TODO auto-generated method Stubbytearrayoutputstream BAOs = new Bytearrayoutput Stream (); options = 100;bitmap.compress (Bitmap.CompressFormat.JPEG, Options, BAOs);//Quality compression method, the compressed data stored in the BAOs ( 100 means no compression, 0 is compressed to a minimum) while (Baos.tobytearray (). length/1024 > 100) {//cycle to determine if the image is larger than 100kb after compression, greater than the continued compression baos.reset ();// Reset BAOs to let the next write overwrite the contents of the options-= 10;//picture quality is reduced by 10if (options<0) options=0;//If the picture quality is less than 10, The quality of the picture is compressed to the minimum bitmap.compress (Bitmap.CompressFormat.JPEG, Options, BAOs);//Save the compressed picture to BAOs if (options==0) break;// If the quality of the picture has been minimized, it is no longer compressed}try {fileoutputstream fos = new FileOutputStream (new File (Imgpath));// Save the compressed picture locally on the specified path in Fos.write (Baos.tobytearray ()); Fos.flush (); Fos.close ();} catch (Exception e) {e.printstacktrace ();}}}). Start ();} /** * ProportionallyReduce the pixels of the image for compression purposes * @author JPH * @param imgpath * @date 2014-12-5 pm 11:30:59 */public static void Compressimagebypixel (Strin G Imgpath) {bitmapfactory.options newopts = new Bitmapfactory.options (); newopts.injustdecodebounds = true;//read-only Edge, Unread content Bitmap bitmap = Bitmapfactory.decodefile (Imgpath, newopts); newopts.injustdecodebounds = false;int width = Newopts.outwidth;int height = newopts.outheight;float maxSize = 1000f;//default 1000pxint be = 1;if (width > Height &&amp ; Width > MaxSize) {//zoom ratio, compute be = (int) (newopts.outwidth/maxsize) with a high or wide one of the larger data,} else if (Width < height &&am P Height > maxSize) {be = (int) (newopts.outheight/maxsize);} Be++;newopts.insamplesize = be;//Set Sample rate Newopts.inpreferredconfig = config.argb_8888;//The mode is the default, Newopts.inpurgeable = true;//is not set to be valid newopts.ininputshareable = true;//. When the system memory is not enough, the picture is automatically recycled bitmap = Bitmapfactory.decodefile (Imgpath, newopts); compressimagebyquality (Bitmap,imgpath);// Compress the scale size and then mass compress}


Android Image Compression Tips

Related Article

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.