Android under compressed image-faint distortion

Source: Internet
Author: User

How to compress pictures under Android:

About 3M of the picture can be compressed to about 100K, almost no distortion. The code is as follows:

Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.OutputStream;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory; Public classBitmaputil {/*** Save as file after compressing picture * *@paramFilePath * The full path of the original picture *@paramStoreimgpath * The full path of the picture to be stored after compression *@returnBoolean *@authorDoraemon * @time June 27, 2014 PM 5:10:19*/     Public Static Booleansavecompressimg (String filePath, String storeimgpath) {Bitmap BM=Getsmallbitmap (FilePath); OutputStream Stream=NULL; Try{Stream=NewFileOutputStream (Storeimgpath); } Catch(FileNotFoundException e) {e.printstacktrace (); }        returnBm.compress (Bitmap.CompressFormat.JPEG, 40, stream); }    /*** Breakthrough based on path and compression return bitmap for display * *@paramIMAGESRC *@return     */    Private StaticBitmap Getsmallbitmap (String filePath) {FinalBitmapfactory.options Options =Newbitmapfactory.options (); Options.injustdecodebounds=true;        Bitmapfactory.decodefile (FilePath, Options); //Calculate insamplesizeOptions.insamplesize = calculateinsamplesize (Options, 480, 800); //Decode bitmap with Insamplesize setOptions.injustdecodebounds =false; returnbitmapfactory.decodefile (FilePath, Options); }    /*** Calculate the zoom value of the picture * *@paramOptions *@paramReqwidth *@paramReqheight *@return     */    Private Static intCalculateinsamplesize (Bitmapfactory.options Options,intReqwidth,intreqheight) {        //Raw Height and width of image        Final intHeight =Options.outheight; Final intwidth =Options.outwidth; intInsamplesize = 1; if(Height > Reqheight | | width >reqwidth) {            //Calculate ratios of height and width to requested height and//width            Final intHeightRatio = Math.Round ((float) Height/(float) reqheight); Final intWidthRatio = Math.Round ((float) Width/(float) reqwidth); //Choose the smallest ratio as insamplesize value, this would//Guarantee//a final image with both dimensions larger than or equal to the//requested height and width.Insamplesize = HeightRatio < WidthRatio?Heightratio:widthratio; }        returninsamplesize; }}

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.