/*** Read Picture properties: Angle of rotation *@paramPath Image Absolute path *@returndegree angle of rotation*/ Public intreadpicturedegree (String path) {intdegree = 0; Try{exifinterface Exifinterface=Newexifinterface (path); intOrientation =Exifinterface.getattributeint (exifinterface.tag_orientation, exifinterface.orientation_normal); Switch(orientation) { CaseExifInterface.ORIENTATION_ROTATE_90:degree= 90; Break; CaseExifInterface.ORIENTATION_ROTATE_180:degree= 180; Break; CaseExifInterface.ORIENTATION_ROTATE_270:degree= 270; Break; } } Catch(IOException e) {e.printstacktrace (); } returndegree; } /** Rotate picture * @param angle * @param bitmap * @return Bitmap*/ PublicBitmap Rotaingimageview (intangle, Bitmap Bitmap) { //Rotate picture ActionMatrix Matrix =NewMatrix (); Matrix.postrotate (angle); //Create a new picture returnBitmap.createbitmap (Bitmap, 0, 0, bitmap.getwidth (), Bitmap.getheight (), Matrix,true); }/*** * save bitmap in SD JPEG format * *@paramBitmap * Picture bitmap *@paramFilePath * To save the picture path *@paramQuality * Compression quality value*/ Public voidSaveImage (Bitmap Bitmap, String FilePath,intquality) {FileOutputStream FileOutputStream=NULL; Try{FileOutputStream=NewFileOutputStream (FilePath); Bitmap.compress (compressformat.jpeg, quality, fileoutputstream); Fileoutputstream.flush (); Fileoutputstream.close (); //If the picture has not yet been recycled, force the recycle if(!bitmap.isrecycled ()) {bitmap.recycle (); System.GC (); } } Catch(Exception e) {}}bitmap=imagecompress.decodesampledbitmapfromresource (filename, 480, 800); //Picture Rotation intdegree =imageutils.readpicturedegree (filename); Bitmap=Imageutils.rotaingimageview (degree, bitmap); //save picture to sd card//imagecompress.saveimage (bitmap, filename);Imagecompress.saveimage (bitmap, filename,90);
View Code
/*** * Dynamic Setting insamplesize multiple * *@parampathName * Picture path *@paramReqwidth * The width to compress *@paramreqheight * Height to compress *@return */ Public StaticBitmap Decodesampledbitmapfromresource (String pathName,intReqwidth,intreqheight) { //first set the injustdecodebounds=true to get the picture size FinalBitmapfactory.options Options =Newbitmapfactory.options (); Options.injustdecodebounds=true; Bitmapfactory.decodefile (pathName, Options); //calculate the value of a insamplesizeOptions.insamplesize =calculateinsamplesize (Options, Reqwidth, reqheight); //decode the image generation based on the computed insamplesize bitmapOptions.injustdecodebounds =false; returnbitmapfactory.decodefile (pathName, Options); }
View Code
Android Photos rotate and save