Android image processing: Identify image orientation and display

Source: Internet
Author: User

When you use ImageView to display images in Android, you find that the images are not displayed. The direction is biased or upside down.


To solve the problem it is natural to think of a two-step walk:

1, self-actively 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 ), explained in detail in the wiki.

In short, EXIF is a standard for electronic cameras (including mobile phones, scanners, etc.). Used to standardize pictures, sounds, video screens, and some of their auxiliary markup formats.

The formats supported by EXIF are as follows:

image  

  uncompressed image file: TIFF

      not supported: JPEG 2000, PNG, GIF  

audio   

  RIFF, WAV

 


Android provides support for the EXIF interface of JPEG format images, able to 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 on bitmap is provided in Android. 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 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 and take a look at the code below.

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 rotate the direction itself */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;}} ......}


==========================================================================================

Welcome to increase our Technical Exchange group:
Android Group: 66756039
Java EE group: 361579846

Android image processing: Identify image orientation and display

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.