Android Image Processing: recognition and display of image directions,

Source: Internet
Author: User

Android Image Processing: recognition and display of image directions,

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) Added in API level 1

Returns an immutable bitmap from subset of the source bitmap, transformed by the optional matrix. the new bitmap may be the same object as source, or a copy may have been made. it is initialized with the same density as the original bitmap. if the source bitmap is immutable and the requested subset is 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 be applied to the pixels
Filter True if the source shocould 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.


These two problems have been solved theoretically. Start with the actual operation and refer to the following code.

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); // calculates 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 ;}}......}





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.