Given the device orientation and Camera2 characteristics, this returns the required JPEG rotation for this camera. - Android Camera

Android examples for Camera:Camera Orientation

Description

Given the device orientation and Camera2 characteristics, this returns the required JPEG rotation for this camera.

Demo Code

/*// w  w w. jav  a2  s .c o m
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraMetadata;
import android.view.OrientationEventListener;

public class Main {
  /**
   * Given the device orientation and Camera2 characteristics, this returns the
   * required JPEG rotation for this camera.
   *
   * @param deviceOrientationDegrees
   *          the device orientation in degrees.
   * @return The JPEG orientation in degrees.
   */
  public static int getJpegRotation(int deviceOrientationDegrees,
      CameraCharacteristics characteristics) {
    if (deviceOrientationDegrees == OrientationEventListener.ORIENTATION_UNKNOWN) {
      return 0;
    }
    int facing = characteristics.get(CameraCharacteristics.LENS_FACING);
    int sensorOrientation = characteristics
        .get(CameraCharacteristics.SENSOR_ORIENTATION);
    if (facing == CameraMetadata.LENS_FACING_FRONT) {
      return (sensorOrientation + deviceOrientationDegrees) % 360;
    } else {
      return (sensorOrientation - deviceOrientationDegrees + 360) % 360;
    }
  }
}

Related Tutorials