简体   繁体   English

固定方向后获取屏幕方向

[英]get Screen Orientation when Orientation is fixed

The OpenCV Library forces me to define android:screenOrientation="landscape" in my AndroidManifest in order to display the camera in full screen. OpenCV库迫使我在AndroidManifest中定义android:screenOrientation="landscape"以便全屏显示摄像机。 I want to detect certain objects in my android app (object recognition of basic forms). 我想在我的android应用中检测某些对象(基本形式的对象识别)。 The Problem is that in order to detect objects the Frame from the camera is analyzed - no matter in what orientation the Phone is - in landscape. 问题在于,为了检测物体,无论是手机的朝向如何,都要分析相机的镜框。 So when you have you phone in portrait mode in your hand the image recognition algorithm won't detect any objects, because it only analyzes the frame in landscape mode. 因此,当您将手机置于纵向模式时,图像识别算法将不会检测到任何物体,因为它仅以横向模式分析帧。

I would like to display a Toast when the user has the phone in portrait mode so that he knows that the image recognition only works in landscape. 当用户将手机置于纵向模式时,我想显示一个Toast,以便他知道图像识别只能在横向模式下进行。 But the classic ways to get the screen orientation (eg with getResources().getConfiguration().orientation ) won't work because the orientation is fixed to landscape in the Manifest (so the result is always ORIENTATION_LANDSCAPE ) 但是获取屏幕方向的经典方法(例如,使用getResources().getConfiguration().orientation )将不起作用,因为方向在Manifest中固定为横向(因此,结果始终为ORIENTATION_LANDSCAPE

Are there any other ways to get the physical orientation of the Phone (even if the Activity's orientation is fixed) ? 是否还有其他方法可以获取电话的物理方向(即使“活动”的方向是固定的)?

For your activity to catch this though you need to add an attribute to the activity node in the manifest file to handle the change. 为了使您的活动能够捕捉到这一点,尽管您需要在清单文件中的活动节点上添加一个属性来处理更改。

<activity android:name=".My_Activity"
android:label="@string/app_name"
android:configChanges="orientation">

In You Activity Class: 在您的活动班级中:

@Override
public void onConfigurationChanged(Configuration myConfig) {
    super.onConfigurationChanged(myConfig);
    int orient = getResources().getConfiguration().orientation; 
    switch(orient) {
                case Configuration.ORIENTATION_LANDSCAPE:
                    //you can remove this case if you want
                    break;
                case Configuration.ORIENTATION_PORTRAIT:
                    //You can give a toast here
                    break;
                default:
                    //take necessary action
                }
}

In short - you have to use the sensors directly: Android: get device orientation from Azimuth, roll & pitch 简而言之-您必须直接使用传感器: Android:从方位角,侧倾和俯仰获取设备方向

He created a nice utility class: https://gist.github.com/abdelhady/501f6e48c1f3e32b253a#file-deviceorientation 他创建了一个不错的实用程序类: https : //gist.github.com/abdelhady/501f6e48c1f3e32b253a#file-deviceorientation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM