In Android, when you use ImageView to display an image, you can see that the image is not correctly displayed. The orientation is incorrect or incorrect.
To solve this problem, we naturally think of two steps:
1. automatically identifies the image direction and calculates the rotation angle;
2. Rotate and display the image.
I. Recognition of image directions
First, we will introduce the concept of EXIF (Exchangeable Image File Format, interchangeable Image files). For more information, see Wikipedia.
In short, Exif is a standard used on electronic cameras (including mobile phones and scanners) to regulate pictures, sounds, visual screens, and some of their secondary markup formats.
Exif supports the following formats:
Image
Compressed image files: JPEG and DCT
Non-compressed image file: TIFF
Unsupported: JPEG 2000, PNG, and GIF
Audio
RIFF, WAV
Android provides support for the Exif interface for JPEG images. You can read the metadata information of JPEG files. For more information, see ExifInterface.
The Metadata information is generally divided into three types: Date and Time, spatial information (longitude and latitude, height), and Camera information (aperture, focal length, rotation angle, exposure, and so on ).
Ii. Image Rotation
Android provides operations for rotating Bitmap matrices. For more information, see the static createBitmap method provided by Bitmap.
Public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
IllegalArgumentException if the x, y, width, height values are outside of the dimensions of the source bitmap.
These two problems have been solved theoretically. Start with the actual operation and refer to the following code.Copy codeThe Code is as follows: public class IOHelper {
......
/** Load images from a given path */
Public static Bitmap loadBitmap (String imgpath ){
Return BitmapFactory. decodeFile (imgpath );
}
/** Load the image from the specified path and specify whether to automatically rotate the image */
Public static Bitmap loadBitmap (String imgpath, boolean adjustOritation ){
If (! AdjustOritation ){
Return loadBitmap (imgpath );
} Else {
Bitmap bm = loadBitmap (imgpath );
Int digree = 0;
ExifInterface exif = null;
Try {
Exif = new ExifInterface (imgpath );
} Catch (IOException e ){
E. printStackTrace ();
Exif = null;
}
If (exif! = Null ){
// Read the camera direction information in the image
Int ori = exif. getAttributeInt (ExifInterface. TAG_ORIENTATION,
ExifInterface. ORIENTATION_UNDEFINED );
// Calculate the Rotation Angle
Switch (ori ){
Case ExifInterface. ORIENTATION_ROTATE_90:
Digree = 90;
Break;
Case ExifInterface. ORIENTATION_ROTATE_180:
Digree = 180;
Break;
Case ExifInterface. ORIENTATION_ROTATE_270:
Digree = 270;
Break;
Default:
Digree = 0;
Break;
}
}
If (digree! = 0 ){
// Rotate the image
Matrix m = new Matrix ();
M. postRotate (digree );
Bm = Bitmap. createBitmap (bm, 0, 0, bm. getWidth (),
Bm. getHeight (), m, true );
}
Return bm;
}
}
......
}