Android get bitmap camera orientation? And turn back -90 degrees

I have this code:

//choosed a picture public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { if (requestCode == ImageHelper.SELECT_PICTURE) { String picture = ""; Uri selectedImageUri = data.getData(); //OI FILE Manager String filemanagerstring = selectedImageUri.getPath(); //MEDIA GALLERY String selectedImagePath = ImageHelper.getPath(mycontext, selectedImageUri); picture=(selectedImagePath!=null)?selectedImagePath:filemanagerstring; 

...

This is just a selection of images from the gallery. this is good, but when I open this image on the image, the images when they took “PORTRAIT MODE” looked good, but the images that shot “LANDSCAPE MODE” with the camera opening at -90 degrees.

How can I rotate these photos back?

  Bitmap output = Bitmap.createBitmap(newwidth, newheight, Config.ARGB_8888); Canvas canvas = new Canvas(output); 

I tried this:

 Log.e("wh", bitmap.getWidth()+" "+bitmap.getHeight()); if (bitmap.getWidth()<bitmap.getHeight()) canvas.rotate(-90); 

but this does not work, all image sizes: * 2560 1920 pixels (PORTRAIT and LANDSCAPE mode)

What can I do to rotate LANDSCAPE photos?

thanks Leslie

+42
android
Sep 02 2018-11-11T00:
source share
2 answers

If the photo was taken using a digital camera or smartphone, the rotation is often saved on Exif photos as part of the image file. You can read Exif metafiles using Android ExifInterface .

First create an ExifInterface :

 ExifInterface exif = new ExifInterface(uri.getPath()); 

Then find the current rotation:

 int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 

Convert exif to degrees:

 int rotationInDegrees = exifToDegrees(rotation); 

Where

 private static int exifToDegrees(int exifOrientation) { if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; } return 0; } 

Then use the actual image rotation as a reference point to rotate the image using Matrix .

 Matrix matrix = new Matrix(); if (rotation != 0f) {matrix.preRotate(rotationInDegrees);} 

You create a new rotated image using the Bitmap.createBitmap method, which takes a Matrix as a parameter:

 Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter) 

where Matrix m contains a new rotation:

 Bitmap adjustedBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, width, height, matrix, true); 

See these guides for useful source code examples:

+168
Jun 18 '12 at 11:32
source share

The last answer was technically perfect, but I tried to create a system for managing images, rotating, resizing, caching and loading in ImageViews, and I can say that this is hell. Even when all this has been done, crashes sometimes cause OutOfMemory on some devices.

My task is not to reinvent the wheel, it has a perfect design. Google itself recommends you use Glide . It works on one line, is super easy to use, has a lightweight size and number of functions, it controls EXIF ​​by default , and it uses memory as a charm. This is just coded black magic;)

I'm not sure that Picasso also controls EXIF, but there is quick input for both of them:

https://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en

My advice: do not waste time and do not use them. You can solve your problem in one line:

 Glide.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView); 
0
Aug 04 '16 at 18:45
source share



All Articles