Photos are sometimes rotated by the system, and the corrective steps are as follows:
1. The angle at which the picture file is rotated is read first:
/** * Read the image file's rotation angle through the Exifinterface class * @param path: The path to the picture file * @return The image file's rotated angle*/ Public Static intreadpicdegree (String path) {intdegree =0; //class Exifinterface to read picture file informationExifinterface exif =NULL; Try{EXIF=Newexifinterface (path); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } if(Exif! =NULL) { intOrientation =Exif.getattributeint (exifinterface.tag_orientation, Exifinterface.orientation_ NORMAL); Switch(orientation) { CaseExifInterface.ORIENTATION_ROTATE_90:degree= -; Break; CaseExifInterface.ORIENTATION_ROTATE_180:degree= the; Break; CaseExifInterface.ORIENTATION_ROTATE_270:degree= the; Break; } } returndegree; }
2. Pass the above angle as a parameter to the following function correction:
/** * To correct the picture in the right direction * * @param degree: The angle of the picture being rotated by the system * @param bitmap: Picture to correct the direction * @return Straighten backward picture */ Public StaticBitmap Rotatebitmap (intdegree, Bitmap Bitmap) {Matrix Matrix=NewMatrix (); Matrix.postrotate (degree); Bitmap BM= Bitmap.createbitmap (Bitmap,0,0, Bitmap.getwidth (), Bitmap.getheight (), Matrix,true); returnBM; }
Android Image rotation