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