1

The OpenCV Library forces me to define android:screenOrientation="landscape" in my AndroidManifest in order to display the camera in full screen. I want to detect certain objects in my android app (object recognition of basic forms). 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. But the classic ways to get the screen orientation (e.g. with getResources().getConfiguration().orientation) won't work because the orientation is fixed to landscape in the Manifest (so the result is always ORIENTATION_LANDSCAPE)

Are there any other ways to get the physical orientation of the Phone (even if the Activity's orientation is fixed) ?

  • Not a solution to your problem though but you could just show a Toast message to the user before opening camera warning him not to use phone in landscape mode. – Seshu Vinay Sep 09 '14 at 08:38

2 Answers2

1

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
                }
}
Sagar Pilkhwal
  • 3,998
  • 2
  • 25
  • 77
1

In short - you have to use the sensors directly: Android: get device orientation from Azimuth, roll & pitch

He created a nice utility class: https://gist.github.com/abdelhady/501f6e48c1f3e32b253a#file-deviceorientation