3

I am working on a application in which I have given facility to user that he can take a picture using his mobile camera and then I am displaying the image in a Imageview.

Now the problem is that if I am capturing the image in a portrait mode or in landscape mode it is always setting the image in landscape mode in ImageView, but I want the image to be set in portrait mode only. Please help me out with this problem.

Any help would be appreciable...

Thank you

Salman Khan
  • 2,822
  • 5
  • 32
  • 53

3 Answers3

5
Matrix mat = new Matrix();

ExifInterface exif = new ExifInterface(yourimagepath);
String orientstring = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
int orientation = orientstring != null ? Integer.parseInt(orientstring) : ExifInterface.ORIENTATION_NORMAL;
int rotateangle = 0;
if(orientation == ExifInterface.ORIENTATION_ROTATE_90) 
            rotateangle = 90;
if(orientation == ExifInterface.ORIENTATION_ROTATE_180) 
            rotateangle = 180;
if(orientation == ExifInterface.ORIENTATION_ROTATE_270) 
            rotateangle = 270;

mat.setRotate(rotateangle, (float) bmpPic.getWidth() / 2, (float) bmpPic.getHeight() / 2);

File f = new File(yourimagepath);       
Bitmap bmpPic = BitmapFactory.decodeStream(new FileInputStream(f), null, null); 
Bitmap bmpPic1 = Bitmap.createBitmap(bmpPic, 0, 0, bmpPic.getWidth(), bmpPic.getHeight(), mat, true);   
Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
2

use like that

Matrix matrix=new Matrix();
imageView.setScaleType(ScaleType.MATRIX);   //required
matrix.postRotate((float) angle, pivX, pivY);
imageView.setImageMatrix(matrix);

for exp:

matrix.postRotate( 90f, imageView.getDrawable().getBounds().width()/2, imageView.getDrawable().getBounds().height()/2)
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
-1

Here is a great solution I came across for this:

https://stackoverflow.com/a/34241250/8033090

One line solution:

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

or

Picasso.with(context).load("file:" + photoPath).into(imageView);

This will autodetect rotation and place image in correct orientation

Picasso is a very powerful library for handling images in your app includes: Complex image transformations with minimal memory use. It can take a second to load but I just put some text behind the image view that says "Loading image" and when the image loads it covers the text.

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/16252863) – Suraj Rao May 28 '17 at 04:41
  • @suraj Thanks for the advice I have updated my post. – Andrew Moreau May 28 '17 at 05:26