Look good on the Internet image compression code

Source: Internet
Author: User

 PackageTest.yc.com.fragmentdemo;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.graphics.Matrix;ImportAndroid.media.ExifInterface;Importandroid.util.Base64;ImportJava.io.ByteArrayOutputStream;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;/*** Created by Uctimes on 2016/10/22.*/ Public classBitmaputils {//Calculate the zoom value of a picture     Public Static intCalculateinsamplesize (Bitmapfactory.options Options,intReqwidth,intreqheight) {        Final intHeight =Options.outheight; Final intwidth =Options.outwidth; intInsamplesize = 1; if(Height > Reqheight | | width >reqwidth) {            Final intHeightRatio = Math.Round ((float) height/(float) reqheight); Final intWidthRatio = Math.Round ((float) Width/(float) reqwidth); Insamplesize= HeightRatio < WidthRatio?Heightratio:widthratio; }        returninsamplesize; }    //get the picture and compress it according to the path, return bitmap for display     Public 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); }    //convert bitmap to String     Public Staticstring bitmaptostring (String filePath) {Bitmap BM=Getsmallbitmap (FilePath); Bytearrayoutputstream BAOs=NewBytearrayoutputstream (); Bm.compress (Bitmap.CompressFormat.JPEG,40, BAOs); byte[] B =Baos.tobytearray (); returnbase64.encodetostring (b, Base64.default); }//    /*//Compress Pictures, handle some mobile phone camera angle rotation problem//*///Public static string Compressimage (context context, string FilePath, string fileName, int q) throws Filenotfoundex ception {////Bitmap BM = Getsmallbitmap (FilePath);////int degree = Readpicturedegree (filePath);////if (degree!=0) {//Rotate Photo Angle//Bm=rotatebitmap (bm,degree);//        }////File Imagedir = sdcardutils.getimagedir (context);////file Outputfile=new file (imagedir,filename);////FileOutputStream out = new FileOutputStream (outputFile);////bm.compress (Bitmap.CompressFormat.JPEG, q, out);////return Outputfile.getpath ();//    }////    //Judging the photo angle//public static int readpicturedegree (String path) {//int degree = 0;//try {//exifinterface exifinterface = new Exifinterface (path);//int orientation = Exifinterface.getattributeint (//Exifinterface.tag_orientation,//exifinterface.orientation_normal);//switch (orientation) {//Case exifinterface.orientation_rotate_90://degree =;//Break ;//Case exifinterface.orientation_rotate_180://degree = n;//Break ;//Case exifinterface.orientation_rotate_270://degree =;//Break ;//            }//} catch (IOException e) {//e.printstacktrace ();//        }//return degree;//    }////Rotate Photos//Public static Bitmap Rotatebitmap (Bitmap bitmap,int degress) {//if (bitmap! = null) {//matrix M = new Matrix ();//m.postrotate (degress);//bitmap = bitmap.createbitmap (bitmap, 0, 0, bitmap.getwidth (),//bitmap.getheight (), M, true);//return bitmap;//        }//return bitmap;//    }}

Look good on the Internet image compression code

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.