Java tutorial
//package com.java2s; import android.hardware.camera2.CameraCharacteristics; import android.util.SparseIntArray; public class Main { private static final SparseIntArray ORIENTATIONS = new SparseIntArray(); public static int getJPEGOrientation(int rotation, int sensorOrientation) { // Sensor orientation is 90 for most devices, or 270 for some devices (eg. Nexus 5X) // We have to take that into account and rotate JPEG properly. // For devices with orientation of 90, we simply return our mapping from ORIENTATIONS. // For devices with orientation of 270, we need to rotate the JPEG 180 degrees. return (ORIENTATIONS.get(rotation) + sensorOrientation + 270) % 360; } public static int getJPEGOrientation(int rotation, CameraCharacteristics cameraCharacteristics) { Integer sensorOrientation = cameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION); sensorOrientation = sensorOrientation == null ? 0 : sensorOrientation; // Sensor orientation is 90 for most devices, or 270 for some devices (eg. Nexus 5X) // We have to take that into account and rotate JPEG properly. // For devices with orientation of 90, we simply return our mapping from ORIENTATIONS. // For devices with orientation of 270, we need to rotate the JPEG 180 degrees. return (ORIENTATIONS.get(rotation) + sensorOrientation + 270) % 360; } }