Java Code Examples for android.view.OrientationEventListener#ORIENTATION_UNKNOWN

The following examples show how to use android.view.OrientationEventListener#ORIENTATION_UNKNOWN . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DisplayOrientationDetector.java    From MediaPickerInstagram with Apache License 2.0 6 votes vote down vote up
public DisplayOrientationDetector(Context context) {
    mOrientationEventListener = new OrientationEventListener(context) {

        /** This is either Surface.Rotation_0, _90, _180, _270, or -1 (invalid). */
        private int mLastKnownRotation = -1;

        @Override
        public void onOrientationChanged(int orientation) {
            if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN ||
                    mDisplay == null) {
                return;
            }
            final int rotation = mDisplay.getRotation();
            if (mLastKnownRotation != rotation) {
                mLastKnownRotation = rotation;
                dispatchOnDisplayOrientationChanged(DISPLAY_ORIENTATIONS.get(rotation));
            }
        }
    };
}
 
Example 2
Source File: DisplayOrientationDetector.java    From cameraview with Apache License 2.0 6 votes vote down vote up
public DisplayOrientationDetector(Context context) {
    mOrientationEventListener = new OrientationEventListener(context) {

        /** This is either Surface.Rotation_0, _90, _180, _270, or -1 (invalid). */
        private int mLastKnownRotation = -1;

        @Override
        public void onOrientationChanged(int orientation) {
            if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN ||
                    mDisplay == null) {
                return;
            }
            final int rotation = mDisplay.getRotation();
            if (mLastKnownRotation != rotation) {
                mLastKnownRotation = rotation;
                dispatchOnDisplayOrientationChanged(DISPLAY_ORIENTATIONS.get(rotation));
            }
        }
    };
}
 
Example 3
Source File: ImageHelper.java    From fanfouapp-opensource with Apache License 2.0 6 votes vote down vote up
public static int roundOrientation(final int orientationInput) {
    // landscape mode
    int orientation = orientationInput;

    if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
        orientation = 0;
    }

    orientation = orientation % 360;
    int retVal;
    if (orientation < ((0 * 90) + 45)) {
        retVal = 0;
    } else if (orientation < ((1 * 90) + 45)) {
        retVal = 90;
    } else if (orientation < ((2 * 90) + 45)) {
        retVal = 180;
    } else if (orientation < ((3 * 90) + 45)) {
        retVal = 270;
    } else {
        retVal = 0;
    }

    return retVal;
}
 
Example 4
Source File: DisplayOrientationDetector.java    From TikTok with Apache License 2.0 6 votes vote down vote up
public DisplayOrientationDetector(Context context) {
    mOrientationEventListener = new OrientationEventListener(context) {

        /** This is either Surface.Rotation_0, _90, _180, _270, or -1 (invalid). */
        private int mLastKnownRotation = -1;

        @Override
        public void onOrientationChanged(int orientation) {
            if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN ||
                    mDisplay == null) {
                return;
            }
            final int rotation = mDisplay.getRotation();
            if (mLastKnownRotation != rotation) {
                mLastKnownRotation = rotation;
                dispatchOnDisplayOrientationChanged(DISPLAY_ORIENTATIONS.get(rotation));
            }
        }
    };
}
 
Example 5
Source File: DisplayOrientationDetector.java    From LockDemo with Apache License 2.0 6 votes vote down vote up
public DisplayOrientationDetector(Context context) {
    mOrientationEventListener = new OrientationEventListener(context) {

        /** This is either Surface.Rotation_0, _90, _180, _270, or -1 (invalid). */
        private int mLastKnownRotation = -1;

        @Override
        public void onOrientationChanged(int orientation) {
            if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN ||
                    mDisplay == null) {
                return;
            }
            final int rotation = mDisplay.getRotation();
            if (mLastKnownRotation != rotation) {
                mLastKnownRotation = rotation;
                dispatchOnDisplayOrientationChanged(DISPLAY_ORIENTATIONS.get(rotation));
            }
        }
    };
}
 
Example 6
Source File: DisplayOrientationDetector.java    From SimpleVideoEditor with Apache License 2.0 6 votes vote down vote up
public DisplayOrientationDetector(Context context) {
    mOrientationEventListener = new OrientationEventListener(context) {

        /** This is either Surface.Rotation_0, _90, _180, _270, or -1 (invalid). */
        private int mLastKnownRotation = -1;

        @Override
        public void onOrientationChanged(int orientation) {
            if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN ||
                    mDisplay == null) {
                return;
            }
            final int rotation = mDisplay.getRotation();
            if (mLastKnownRotation != rotation) {
                mLastKnownRotation = rotation;
                dispatchOnDisplayOrientationChanged(DISPLAY_ORIENTATIONS.get(rotation));
            }
        }
    };
}
 
Example 7
Source File: VideoCaptureAndroid.java    From droidkit-webrtc with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public VideoCaptureAndroid(int id, long native_capturer) {
  this.id = id;
  this.native_capturer = native_capturer;
  this.info = new Camera.CameraInfo();
  Camera.getCameraInfo(id, info);

  // Must be the last thing in the ctor since we pass a reference to |this|!
  final VideoCaptureAndroid self = this;
  orientationListener = new OrientationEventListener(GetContext()) {
      @Override public void onOrientationChanged(int degrees) {
        if (degrees == OrientationEventListener.ORIENTATION_UNKNOWN) {
          return;
        }
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
          degrees = (info.orientation - degrees + 360) % 360;
        } else {  // back-facing
          degrees = (info.orientation + degrees) % 360;
        }
        self.OnOrientationChanged(self.native_capturer, degrees);
      }
    };
  // Don't add any code here; see the comment above |self| above!
}
 
Example 8
Source File: CameraProxy.java    From CameraDemo with Apache License 2.0 5 votes vote down vote up
private void setPictureRotate(int orientation) {
    if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) return;
    orientation = (orientation + 45) / 90 * 90;
    int rotation;
    if (mCameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
        rotation = (mCameraInfo.orientation - orientation + 360) % 360;
    } else {  // back-facing camera
        rotation = (mCameraInfo.orientation + orientation) % 360;
    }
    mLatestRotation = rotation;
}
 
Example 9
Source File: Util.java    From VideoFaceDetection with MIT License 5 votes vote down vote up
public static int roundOrientation(int orientation, int orientationHistory) {
    boolean changeOrientation = false;
    if (orientationHistory == OrientationEventListener.ORIENTATION_UNKNOWN) {
        changeOrientation = true;
    } else {
        int dist = Math.abs(orientation - orientationHistory);
        dist = Math.min( dist, 360 - dist );
        changeOrientation = ( dist >= 45 + ORIENTATION_HYSTERESIS );
    }
    if (changeOrientation) {
        return ((orientation + 45) / 90 * 90) % 360;
    }
    return orientationHistory;
}
 
Example 10
Source File: CameraFilterActivity.java    From EZFilter with MIT License 5 votes vote down vote up
private int roundOrientation(int orientation, int orientationHistory) {
    boolean changeOrientation;
    if (orientationHistory == OrientationEventListener.ORIENTATION_UNKNOWN) {
        changeOrientation = true;
    } else {
        int dist = Math.abs(orientation - orientationHistory);
        dist = Math.min(dist, 360 - dist);
        changeOrientation = (dist >= 45 + ORIENTATION_HYSTERESIS);
    }
    if (changeOrientation) {
        return ((orientation + 45) / 90 * 90) % 360;
    }
    return orientationHistory;
}
 
Example 11
Source File: MultiInputActivity.java    From EZFilter with MIT License 5 votes vote down vote up
private int roundOrientation(int orientation, int orientationHistory) {
    boolean changeOrientation;
    if (orientationHistory == OrientationEventListener.ORIENTATION_UNKNOWN) {
        changeOrientation = true;
    } else {
        int dist = Math.abs(orientation - orientationHistory);
        dist = Math.min(dist, 360 - dist);
        changeOrientation = (dist >= 45 + ORIENTATION_HYSTERESIS);
    }
    if (changeOrientation) {
        return ((orientation + 45) / 90 * 90) % 360;
    }
    return orientationHistory;
}
 
Example 12
Source File: CameraSession.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
protected void configureRecorder(int quality, MediaRecorder recorder) {
    Camera.CameraInfo info = new Camera.CameraInfo();
    Camera.getCameraInfo(cameraInfo.cameraId, info);
    int displayOrientation = getDisplayOrientation(info, false);


    int outputOrientation = 0;
    if (jpegOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) {
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            outputOrientation = (info.orientation - jpegOrientation + 360) % 360;
        } else {
            outputOrientation = (info.orientation + jpegOrientation) % 360;
        }
    }
    recorder.setOrientationHint(outputOrientation);

    int highProfile = getHigh();
    boolean canGoHigh = CamcorderProfile.hasProfile(cameraInfo.cameraId, highProfile);
    boolean canGoLow = CamcorderProfile.hasProfile(cameraInfo.cameraId, CamcorderProfile.QUALITY_LOW);
    if (canGoHigh && (quality == 1 || !canGoLow)) {
        recorder.setProfile(CamcorderProfile.get(cameraInfo.cameraId, highProfile));
    } else if (canGoLow) {
        recorder.setProfile(CamcorderProfile.get(cameraInfo.cameraId, CamcorderProfile.QUALITY_LOW));
    } else {
        throw new IllegalStateException("cannot find valid CamcorderProfile");
    }
    isVideo = true;
}
 
Example 13
Source File: OrientationHelper.java    From Lassi-Android with MIT License 5 votes vote down vote up
public OrientationHelper(@NonNull Context context, @NonNull Callback callback) {
    mCallback = callback;
    mListener = new OrientationEventListener(context.getApplicationContext(), SensorManager.SENSOR_DELAY_NORMAL) {

        @SuppressWarnings("ConstantConditions")
        @Override
        public void onOrientationChanged(int orientation) {
            int or = 0;
            if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
                or = mDeviceOrientation != -1 ? mDeviceOrientation : 0;
            } else if (orientation >= 315 || orientation < 45) {
                or = 0;
            } else if (orientation >= 45 && orientation < 135) {
                or = 90;
            } else if (orientation >= 135 && orientation < 225) {
                or = 180;
            } else if (orientation >= 225 && orientation < 315) {
                or = 270;
            }

            if (or != mDeviceOrientation) {
                mDeviceOrientation = or;
                mCallback.onDeviceOrientationChanged(mDeviceOrientation);
            }
        }
    };
}
 
Example 14
Source File: CameraSession.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
protected void configureRecorder(int quality, MediaRecorder recorder) {
    Camera.CameraInfo info = new Camera.CameraInfo();
    Camera.getCameraInfo(cameraInfo.cameraId, info);
    int displayOrientation = getDisplayOrientation(info, false);


    int outputOrientation = 0;
    if (jpegOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) {
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            outputOrientation = (info.orientation - jpegOrientation + 360) % 360;
        } else {
            outputOrientation = (info.orientation + jpegOrientation) % 360;
        }
    }
    recorder.setOrientationHint(outputOrientation);

    int highProfile = getHigh();
    boolean canGoHigh = CamcorderProfile.hasProfile(cameraInfo.cameraId, highProfile);
    boolean canGoLow = CamcorderProfile.hasProfile(cameraInfo.cameraId, CamcorderProfile.QUALITY_LOW);
    if (canGoHigh && (quality == 1 || !canGoLow)) {
        recorder.setProfile(CamcorderProfile.get(cameraInfo.cameraId, highProfile));
    } else if (canGoLow) {
        recorder.setProfile(CamcorderProfile.get(cameraInfo.cameraId, CamcorderProfile.QUALITY_LOW));
    } else {
        throw new IllegalStateException("cannot find valid CamcorderProfile");
    }
    isVideo = true;
}
 
Example 15
Source File: CameraSession.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private int roundOrientation(int orientation, int orientationHistory) {
    boolean changeOrientation;
    if (orientationHistory == OrientationEventListener.ORIENTATION_UNKNOWN) {
        changeOrientation = true;
    } else {
        int dist = Math.abs(orientation - orientationHistory);
        dist = Math.min(dist, 360 - dist);
        changeOrientation = (dist >= 45 + ORIENTATION_HYSTERESIS);
    }
    if (changeOrientation) {
        return ((orientation + 45) / 90 * 90) % 360;
    }
    return orientationHistory;
}
 
Example 16
Source File: CameraUtil.java    From Camera2 with Apache License 2.0 5 votes vote down vote up
/**
 * Given the device orientation and Camera2 characteristics, this returns
 * the required JPEG rotation for this camera.
 *
 * @param deviceOrientationDegrees the clockwise angle of the device orientation from its
 *                                 natural orientation in degrees.
 * @return The angle to rotate image clockwise in degrees. It should be 0, 90, 180, or 270.
 */
public static int getJpegRotation(int deviceOrientationDegrees,
                                  CameraCharacteristics characteristics)
{
    if (deviceOrientationDegrees == OrientationEventListener.ORIENTATION_UNKNOWN)
    {
        return 0;
    }
    boolean isFrontCamera = characteristics.get(CameraCharacteristics.LENS_FACING) ==
            CameraMetadata.LENS_FACING_FRONT;
    int sensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
    return getImageRotation(sensorOrientation, deviceOrientationDegrees, isFrontCamera);
}
 
Example 17
Source File: CameraSession.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private int roundOrientation(int orientation, int orientationHistory) {
    boolean changeOrientation;
    if (orientationHistory == OrientationEventListener.ORIENTATION_UNKNOWN) {
        changeOrientation = true;
    } else {
        int dist = Math.abs(orientation - orientationHistory);
        dist = Math.min(dist, 360 - dist);
        changeOrientation = (dist >= 45 + ORIENTATION_HYSTERESIS);
    }
    if (changeOrientation) {
        return ((orientation + 45) / 90 * 90) % 360;
    }
    return orientationHistory;
}
 
Example 18
Source File: CameraActivity_OldAPI.java    From microbit with Apache License 2.0 4 votes vote down vote up
/**
 * Changes button orientation and updates button icon according to
 * passed orientation value.
 *
 * @param rotation New button rotation.
 */
private void updateButtonOrientation(int rotation) {
    rotation = (rotation + mOrientationOffset) % 360;
    int quant_rotation = 0;
    boolean buttonPortraitVisible = true;
    if(rotation == OrientationEventListener.ORIENTATION_UNKNOWN)
        return;
    if(rotation < 45 || rotation >= 315) {
        buttonPortraitVisible = true;
        quant_rotation = 0;
        mCurrentIconIndex = 0;
    } else if((rotation >= 135 && rotation < 225)) {
        buttonPortraitVisible = true;
        quant_rotation = 180;
        //mCurrentIconIndex = 2;
        mCurrentIconIndex = 0; //This way only 2 configurations are allowed
    } else if((rotation >= 45 && rotation < 135)) {
        buttonPortraitVisible = false;
        quant_rotation = 270;
        //mCurrentIconIndex = 1;
        mCurrentIconIndex = 3; //This way only 2 configurations are allowed
    } else if((rotation >= 225 && rotation < 315)) {
        buttonPortraitVisible = false;
        quant_rotation = 90;
        mCurrentIconIndex = 3;
    }
    if(quant_rotation != mCurrentRotation) {
        mCurrentRotation = quant_rotation;

        if(buttonPortraitVisible) {
            mButtonBack_landscape.setVisibility(View.INVISIBLE);
            mButtonBack_portrait.setVisibility(View.VISIBLE);
            mButtonBack_portrait.bringToFront();
        } else {
            mButtonBack_landscape.setVisibility(View.VISIBLE);
            mButtonBack_landscape.bringToFront();
            mButtonBack_portrait.setVisibility(View.INVISIBLE);
        }
        mButtonBack_portrait.invalidate();
        mButtonBack_landscape.invalidate();

        updateButtonClickIcon();
        updateCameraRotation();
    }
}
 
Example 19
Source File: DisplayOrientationDetector.java    From camerakit-android with MIT License 4 votes vote down vote up
public DisplayOrientationDetector(Context context) {
    mOrientationEventListener = new OrientationEventListener(context) {

        private int mLastKnownDisplayRotation = -1;

        @Override
        public void onOrientationChanged(int orientation) {
            if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN || mDisplay == null) {
                return;
            }

            boolean displayOrDeviceOrientationChanged = false;

            final int displayRotation = mDisplay.getRotation();
            if (mLastKnownDisplayRotation != displayRotation) {
                mLastKnownDisplayRotation = displayRotation;
                displayOrDeviceOrientationChanged = true;
            }

            int deviceOrientation;
            if (orientation >= 60 && orientation <= 140) {
                // the mDisplay.getRotation stuff is flipped for 90 & 270 vs. deviceOrientation here. This keeps it consistent.
                deviceOrientation = 270;
            } else if (orientation >= 140 && orientation <= 220) {
                deviceOrientation = 180;
            } else if (orientation >= 220 && orientation <= 300) {
                // the mDisplay.getRotation stuff is flipped for 90 & 270 vs. deviceOrientation here. This keeps it consistent.
                deviceOrientation = 90;
            } else {
                deviceOrientation = 0;
            }

            if (mLastKnownDeviceOrientation != deviceOrientation) {
                mLastKnownDeviceOrientation = deviceOrientation;
                displayOrDeviceOrientationChanged = true;
            }

            if (displayOrDeviceOrientationChanged) {
                dispatchOnDisplayOrDeviceOrientationChanged(DISPLAY_ORIENTATIONS.get(displayRotation));
            }
        }

    };
}
 
Example 20
Source File: MainActivity.java    From Android-Application with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onOrientationChanged(int orientation) {

    //Algorithm adopted from Open Camera project by Mark Harman

    if( orientation != OrientationEventListener.ORIENTATION_UNKNOWN ) {

        diff = Math.abs(orientation - current_orientation);
        if( diff > 180 ) {
            diff = 360 - diff;
        }

        // threshold
        if( diff > 60 ) {
            orientation = ((orientation + 45) / 90 * 90) % 360;

            if( orientation != current_orientation ) {
                current_orientation = orientation;

                Log.d(TAG, "current_orientation is now: " + current_orientation);

                int rotation = MainActivity.this.getWindowManager().getDefaultDisplay().getRotation();
                int degrees = 0;
                switch (rotation) {
                    case Surface.ROTATION_0: degrees = 0; break;
                    case Surface.ROTATION_90: degrees = 90; break;
                    case Surface.ROTATION_180: degrees = 180; break;
                    case Surface.ROTATION_270: degrees = 270; break;
                    default:
                        break;
                }

                int relative_orientation = (current_orientation + degrees) % 360;

                Log.d(TAG, "    current_orientation = " + current_orientation);
                Log.d(TAG, "    degrees = " + degrees);
                Log.d(TAG, "    relative_orientation = " + relative_orientation);

                final int ui_rotation = (360 - relative_orientation) % 360;
                uiRotate(ui_rotation);

            }
        }

    }

}