Android Image Processing: Identifying the image direction and displaying the example tutorial

Source: Internet
Author: User

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;
}
}
......
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.