问题
I don't want to support landscape UI at all across my app, but I want to be able to automatically rotate the photos users take in landscape mode. Currently if a user takes a photo in landscape mode, it remains on screen as if it was taken in portrait (the horizon in the photo is vertical).
I've tried to get the orientation from the system like this:
val display = (getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay
val screenOrientation = display.rotation
Log.d("orientation", screenOrientation.toString())
But it doesn't work and just gives back 0 every time.
I've tried a few other solutions but couldn't get them to work.
I am wondering, am I wasting my time trying to figure this out? Is it even possible to know the orientation in which the photo was captured if my activity only operates in portrait mode? There are a few articles out there talking about camera orientation but they don't talk about whether the orientation in their activity is locked or not.
Thank you.
回答1:
//Yes, find the Orientation Using ExifInterface
try{
File imageFile = new File(imagePath);
ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
// dont know exactly but, orientation for landscape may be 180 or 270.
// From the orientation value you can convert image according to orientation and can be set it vertically, horrizentally as below
int rotate = 0;
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.e(Common.TAG, "Exif orientation: " + orientation);
}
catch (Exception e) {
e.printStackTrace();
}
// Now Change image as your wish...
// Image rotation //
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
Bitmap cropped = Bitmap.createBitmap(scaled, x, y, width, height, matrix, true);
来源:https://stackoverflow.com/questions/56017583/can-i-get-the-orientation-of-a-photo-taken-in-my-app-if-i-limit-the-activitys-o