【问题标题】:How to detect device rotation for setting EXIF orientation tag (Android)如何检测设备旋转以设置 EXIF 方向标签(Android)
【发布时间】:2012-10-07 15:02:53
【问题描述】:

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

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

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

但是……那张纵向拍摄的就没有那么幸运了…… 好像向左转了 90 度。

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

【问题讨论】:

    标签: android camera orientation exif


    【解决方案1】:

    使用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

    【讨论】:

    • 感谢您的快速回复,但是...因为我的 UI 是固定为纵向的,所以... display.getRotation() 总是返回 0。
    • @Gandroid,哪种方法有效?根据我的经验,当您将活动设置为纵向(或横向)时,getRotation、OrientationEventListener 不起作用。期待您的澄清。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 2012-02-13
    • 2014-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多