我不想在我的应用程序上完全支持Landscape UI,但是我希望能够自动旋转用户在景观模式下拍摄的照片。目前,如果用户在景观模式下拍照,它仍在屏幕上,就好像是在肖像中拍摄的(照片中的地平线是垂直的(。
我试图从这样的系统中获得方向:
val display = (getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay
val screenOrientation = display.rotation
Log.d("orientation", screenOrientation.toString())
但它不起作用,每次都回馈0。
我已经尝试了其他一些解决方案,但无法让它们工作。
我想知道,我在浪费时间试图解决这个问题吗?是否有可能知道如果我的活动仅在肖像模式下运行,则捕获照片的方向?那里有几篇文章在谈论相机方向,但他们没有谈论活动中的方向是否锁定。
谢谢。
//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);