Android:How to Detect the Image Orientation (Portrait or Landscape) Picked from Gallery While Setting on an Imageview

Android : How to detect the image orientation (portrait or landscape) picked from gallery while setting on an imageview?

Use ExifInterface for rotate image. Use this method for get correct value to be rotate captured image from camera.

public int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){
int rotate = 0;
try {
context.getContentResolver().notifyChange(imageUri, null);
File imageFile = new File(imagePath);

ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}

Log.i("RotateImage", "Exif orientation: " + orientation);
Log.i("RotateImage", "Rotate value: " + rotate);
} catch (Exception e) {
e.printStackTrace();
}
return rotate;
}

And put this code in Activity result method and get value to rotate image...

String selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};

Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
cursor.close();

int rotateImage = getCameraPhotoOrientation(MyActivity.this, selectedImage, filePath);

Hope this helps..

How to find out if the image from the gallery was taken in portrait or landscape from camera

I have a image in gallery which was taken from camera

There is no way for a developer to guarantee that any particular image came from a camera app, unless the developer is the author of that camera app.

I am trying to find out if the image is portrait or landscape

There is no requirement for an image to be either portrait or landscape. For example, the camera app might crop its photos to be circular or square.

I have the path obtained from below code

That code will fail for lots of Uri values on lots of devices. If you want to use ExifInterface for a Uri, use getContentResolver().openInputStream() to get an InputStream on the content identified by the Uri, then pass that InputStream to this ExifInterface constructor.

I always get orientation as ExifInterface.ORIENTATION_UNDEFINED

Images do not have to have EXIF headers, let alone ExifInterface.TAG_ORIENTATION.

If that tag does not exist, you could examine the width and height of the image and make a guess:

  • If the width is greater than the height, it might be landscape
  • If the height is greater than the width, it might be portrait

This will fail for square images, and this will fail for cropped images (e.g., the photo was taken portrait, but the user cropped it such that the width is now greater than the height).

The best solution is to change your app to not care whether the image is portrait or landscape. The next-best solution is to ask the user how they want to handle the image, where perhaps you use the above algorithm to set the default option.

Camera picture Uri null path

So I know that it must be something with the path.

That's because it's not a filesystem path. A Uri is not a file, and while you are handling that properly elsewhere, you are not doing so here.

You need to switch to EXIF logic that can handle an InputStream, such as this code culled from the AOSP Mms app.

how to set camera Image orientation?

just include this code

public void rotateImage(String file) throws IOException{

BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file, bounds);

BitmapFactory.Options opts = new BitmapFactory.Options();
Bitmap bm = BitmapFactory.decodeFile(file, opts);

int rotationAngle = getCameraPhotoOrientation(getActivity(), Uri.fromFile(file1), file1.toString());

Matrix matrix = new Matrix();
matrix.postRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);
FileOutputStream fos=new FileOutputStream(file);
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
}

public static int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){
int rotate = 0;
try {
context.getContentResolver().notifyChange(imageUri, null);
File imageFile = new File(imagePath);
ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
switch (orientation) {
case ExifInterface.ORIENTATION_NORMAL:
rotate = 0;
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
} catch (Exception e) {
e.printStackTrace();
}
return rotate;
}

Picture taken from camera or gallery when using in imageview its orientation getting changed, and sometimes vertically stretched in Android

The images has different orientations so it rotates according to the orientation when putting on imageview. You can check the orientation of the photo from properties of image.
To set the image in proper manner you can use the following code...

     int rot=getCameraPhotoOrientation(this,Uri,picturePath);
if(rot!=0)
bitmap=new RotateOrientation().RotateOrientationCall(bitmap,rot);

The getCameraPhotoOrientation Method:-

 public static int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){
int rotate = 0;
try {
context.getContentResolver().notifyChange(imageUri, null);
File imageFile = new File(imagePath);
ExifInterface exif = new ExifInterface(
imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);

switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}

Log.d(TAG, "Exit orientation: " + orientation);
} catch (Exception e) {
e.printStackTrace();
}
return rotate;
}

Add RotateOrientation class to rotate class according to orientation.

 public class RotateOrientation  {

Bitmap RotateOrientationCall(Bitmap src,float degree)
{

Matrix matrix=new Matrix();
matrix.postRotate(degree);
Bitmap bmOut = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
return bmOut;

}
}


Related Topics



Leave a reply



Submit