Solve the problem that images are rotated after taking photos on android phones.

Source: Internet
Author: User

Requirement: I want to perform a project similar to Sina Weibo to upload images. There are two other projects that use mobile phones to take pictures. Both of them need to upload images to the server.

Problem: some mobile phones rotate images 90 degrees, some images rotate 180 degrees, some mobile phones are normal, and the server requires a positive attitude, users cannot send a picture on Weibo to see that the picture has been rotated. In another project, the rotated picture has a direct matching problem, which is even more serious.

Solution: At the beginning, no good solution was found on the Internet. Google searched Baidu and thought of the first solution. When the mobile phone took a picture, go to a new page immediately in the returned result processing page. In the new page, let the user manually rotate the image, and then click OK. You can upload images to the server. The second solution is a method found in the forum. You can obtain the image attributes and read the image rotation angle.

Method 1 code:

Handle redirection in onActivityResult

 

Intent intent = new Intent (SendMicoBlog. this, RotaingActivity. class); MyApp myApp = (MyApp) getApplication (); myApp. setName (bitmap); // The modified name myApp. setPhoto (isPhoto); startActivityForResult (intent, 1 );

In the RotaingActivity, the corresponding rotation can be performed.
// Obtain bitmapmyapp MyApp = (MyApp) getapplication (); bitmap = MyApp. getname (); system. out. println ("rotaingactivity W =" + bitmap. getwidth () + "H =" + bitmap. getheight (); mybitmap = bitmap; mywidth = bitmap. getwidth (); myheight = bitmap. getheight (); // The Matrix object matrix = new matrix () used to create an image; drawable = imagedispose. bitmaptodrawable (Bitmap); imageview. setbackgrounddrawable (drawable); btncancel. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {rotaingactivity. this. finish () ;}}); btnrightrotaing. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {mybitmap = rotaingimageview (-90) ;}}); btnleftrotaing. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {mybitmap = rotaingimageview (+ 90) ;}}); btnok. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {MyApp = (MyApp) getapplication (); MyApp. setname (mybitmap); // after modification, system. out. println ("OK resultcoder"); intent = new intent (rotaingactivity. this, sendmicoblog. class); setresult (6, intent); rotaingactivity. this. finish () ;}) ;}private bitmap rotaingimageview (INT angle2) {// rotating the image action matrix. postrotate (angle2); system. out. println ("angle2 =" + angle2); // create a new image bitmap resizedbitmap = bitmap. createbitmap (bitmap, 0, 0, mywidth, myheight, matrix, true); drawable = imagedispose. bitmaptodrawable (resizedbitmap); imageview. setbackgrounddrawable (drawable); Return resizedbitmap ;}
Method 2: It's much simpler. You just steal the photo and process it yourself. You don't need to perform any operations, so the experience will be better.

Processing in onActivityResult

BitmapFactory. options bitmapOptions = new BitmapFactory. options (); bitmapOptions. inSampleSize = 8; File file = new File (SD_CARD_TEMP_DIR);/*** get the image rotation angle. Some systems rotate the image, some do not rotate */int degree = ImageDispose. readPictureDegree (file. getAbsolutePath ());

 

Bitmap cameraBitmap = BitmapFactory. decodeFile (SD_CARD_TEMP_DIR, bitmapOptions); bitmap = cameraBitmap;/*** rotate the image to a positive direction */bitmap = ImageDispose. rotaingImageView (degree, bitmap); upload (bitmap );

 

/*** Rotate the image * @ param angle * @ param bitmap * @ return Bitmap */public static Bitmap rotaingImageView (int angle, Bitmap bitmap) {// rotating image action Matrix matrix = new Matrix (); matrix. postRotate (angle); System. out. println ("angle2 =" + angle); // create a new image Bitmap resizedBitmap = Bitmap. createBitmap (bitmap, 0, 0, bitmap. getWidth (), bitmap. getHeight (), matrix, true); return resizedBitmap ;}

 

/*** Read image attributes: Rotation Angle * @ param path: Absolute image path * @ return degree rotation angle */public static int readPictureDegree (String path) {int degree = 0; try {ExifInterface exifInterface = new ExifInterface (path); int orientation = exifInterface. getAttributeInt (ExifInterface. TAG_ORIENTATION, ExifInterface. ORIENTATION_NORMAL); switch (orientation) {case ExifInterface. ORIENTATION_ROTATE_90: degree = 90; break; case ExifInterface. ORIENTATION_ROTATE_180: degree = 180; break; case ExifInterface. ORIENTATION_ROTATE_270: degree = 270; break ;}} catch (IOException e) {e. printStackTrace ();} return degree ;}

 

In this case, the photo you uploaded is positive.

From: http://blog.csdn.net/walker02/article/details/8211628

 

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.