android - 如何检测设备旋转以设置 EXIF 方向标签 (Android)

标签 android camera orientation exif

在我的相机应用程序中,UI 方向固定为纵向。

通过设置camera.setdisplayorientation(90),预览可以在横向和纵向上正确显示。

我可以拍照并将其保存到文件中。 当我通过 ImageView 显示它时, 横向拍摄的照片可以正确显示。 (手机顶部在我的左侧)

但是...纵向拍摄的就没那么幸运了... 看起来像向左转 90 度。

我尝试通过传感器检测设备方向,以便设置 EXIF header , 但是...到目前为止...这对我来说太难实现...有人可以帮助我解决这个问题吗?

最佳答案

使用 getRotation 方法:

Display display = ((WindowManager)
context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();

来自文档:

Returns the rotation of the screen from its "natural" orientation. 
The returned value may be Surface.ROTATION_0 (no rotation), Surface.ROTATION_90, Surface.
ROTATION_180, or Surface.ROTATION_270. 
For example, if a device has a naturally tall screen, and the user has turned it on its side to go into a landscape orientation, the value returned here may be either Surface.ROTATION_90 or Surface.ROTATION_270 depending on the direction it was turned. 
The angle is the rotation of the drawn graphics on the screen, which is the opposite direction of the physical rotation of the device.
For example, if the device is rotated 90 degrees counter-clockwise, to compensate rendering will be rotated by 90 degrees clockwise and thus the returned value here will be Surface.ROTATION_90.

getRotation 是从 Android 2.2 开始引入的。如果您的目标是较旧的设备,请使用 getOrientation。

在这里得到答复表格: how to detect orientation of android device?

如果您只想检测设备的方向,那么您可以使用 OrientationEventListener。

这是官方文档: http://developer.android.com/reference/android/view/OrientationEventListener.html#onOrientationChanged(int)

但是在使用任何东西之前,请查看这篇关于 Android 中方向处理的优秀博客文章: http://android-developers.blogspot.in/2010/09/one-screen-turn-deserves-another.html

关于android - 如何检测设备旋转以设置 EXIF 方向标签 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12769914/

相关文章:

camera - Expo recordAsync() 不返回响应

ios - 错误 noCamerasAvailable,仅适用于 iPhone <8+ Swift

ios - 当应用程序面向横向和纵向时更改不同的 self.view.backgroundColor 图像

android - 如何避免在配置更改和启动 Activity 时重新加载 Admob 广告?

java - Box2D 通过 libGDX : How to create one MouseJoint per body for separate Android touches?

java - 奇怪的 SocketExceptions : connection reset and Connection timed out

java - 如何绘制平滑/圆润的路径?

java.net.ConnectException : Connection timed out: connect error connecting to remote database

ios - 使用 AVFoundation 暂停视频录制

Android 菜单选项复选框未选中方向更改修复