When you use ImageView to display images in Android, you find that the images are not positive, the direction is skewed, or the reverse is reversed.
To solve this problem it is natural to think of two steps to go:
1, automatically identify the direction of the image, calculate the rotation angle;
2, the image is rotated and displayed.
First, identify the direction of the image
First, here is a concept exif (exchangeable image file Format, exchangeable image files ), specifically explained in wiki.
In short, EXIF is a standard used for electronic cameras (including mobile phones, scanners, etc.) to regulate images, sounds, video screens, and some of their auxiliary marking formats.
EXIF supports the following formats:
figure Like |
compressed image files: JPEG, DCT uncompressed image file: TIFF |
Not supported: JPEG 2000, PNG, GIF , |
audio |
RIFF, WAV |
|
Android provides support for EXIF interface to JPEG format image, can read JPEG file metadata information, see Exifinterface.
These metadata information generally fall into three categories: date time, spatial information (latitude and longitude, height), camera information (aperture, focal length, rotation angle, exposure, etc.).
Second, the image rotation
The operation of matrix rotation for bitmap is provided in Android, see Static CreateBitmap methods provided by bitmap.
Public static Bitmap CreateBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)Added in API Level 1
Returns an immutable bitmap from subset of the source bitmap, transformed by the optional matrix. The new bitmap is the same object as source, or a copy may has been made. It is initialized with the same density as the original bitmap. If the source bitmap is immutable and the requested subset are the same as the source bitmap itself, then the source bitmap is returned and no new bitmap is created.
Parameters
Source |
The bitmap we are subsetting |
X |
The X coordinate of the first pixel in source |
Y |
The Y coordinate of the first pixel in source |
Width |
The number of pixels in each row |
Height |
The number of rows |
M |
Optional matrix to is applied to the pixels |
Filter |
True if the source should be filtered. Only applies if the matrix contains more than just translation. |
Returns
- A bitmap that represents the specified subset of source
Throws
IllegalArgumentException |
If the x, y, width, height values are outside of the dimensions of the source bitmap. |
Both of these problems have been solved theoretically, so let's start with the actual operation, referring to the following code.
public class Iohelper {.../** loading a picture from a given path */public static Bitmap LoadBitmap (String imgpath) {return Bitmapfactory.decodefile (Imgpath);} /** loads the picture from the given path and specifies whether to automatically rotate the direction */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 camera orientation information in picture int ori = Exif.getattributeint (exifinterface.tag_orientation, exifinterface.orientation_undefined);//Calculate 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 picture matrix M = new Matrix (); M.postrotate (digree); BM = Bitmap.createbitmap (BM, 0, 0, bm.getwidth (), BM. GetHeight (), M, true);} return BM;}} ......}
Android image processing: Identify image orientation and display