Android Rotate Photos/images

Source: Internet
Author: User

Today more busy (is the task done, not lazy), on more than a few updates, to make up for something before.

Develop good habits, first recommended reading:

Android image processing: Identify image orientation and show example tutorials

Call system camera in Android to get the direction of the photo to be judged

Before I do the picture rotation, I think of a problem, is to take the Android device to take pictures, open the picture is not the reverse. The test found that no matter whether I was taking pictures or taking pictures backwards, the picture browser in any (and most) of the devices was able to read the photo, so I thought there was something in the picture and it was standard information to show the positive direction of the photo.

Later looked up the information, found that there is such a thing: EXIF

If there is a standard, just look for the interface.

Another need to know is how the image data rotation.

With CreateBitmap (Bitmap Source, int x, int y, int width, int height, Matrix m, boolean filter) is good.

Method Description:

Bitmap android. Graphics. Bitmap. CreateBitmap (Bitmap Source, int x, int y, int width, int height, Matrix m, boolean filter)

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 Bitma P.

Put the following code:

Read EXIF information to find the angle of rotation in the positive direction of the picture

1 Private intreadpicturedegree (String path) {2         intdegree = 0;3         Try {4Exifinterface Exifinterface =Newexifinterface (path);5             intOrientation =Exifinterface.getattributeint (exifinterface.tag_orientation,6 exifinterface.orientation_normal);7             Switch(orientation) {8              Caseexifinterface.orientation_rotate_90:9degree = 90;Ten                  Break; One              Caseexifinterface.orientation_rotate_180: Adegree = 180; -                  Break; -              Caseexifinterface.orientation_rotate_270: thedegree = 270; -                  Break; -             } -}Catch(IOException e) { + e.printstacktrace (); -         } +         returndegree; A}

Rotate the angle to turn it back to the positive direction.

1     Private StaticBitmap Rotate (Bitmap B,intdegrees) {2         if(degrees = = 0) {3             returnb;4         }5         if(degrees! = 0 && B! =NULL) {6Matrix m =NewMatrix ();7M.setrotate (degrees, (float) B.getwidth (), (float) B.getheight ());8             Try {9Bitmap B2 = bitmap.createbitmap (b, 0, 0, b.getwidth (), B.getheight (), M,true);Ten                 if(b! =B2) { One b.recycle (); Ab =B2; -                 } -}Catch(OutOfMemoryError ex) { the             } -         } -         returnb; -}

This is basically no problem. Perhaps some boards will not be able to read the right direction.

Android Rotate Photos/images

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.