我使用camera2 API捕获照片并将屏幕方向设置为纵向。我发现从两个设备以横向模式捕获照片时会有不同的行为。
设备1可以正常显示照片,但设备2可以显示倒置的照片。 这是我的代码:
static {
ORIENTATIONS.append(Surface.ROTATION_0, 90);
ORIENTATIONS.append(Surface.ROTATION_90, 0);
ORIENTATIONS.append(Surface.ROTATION_180, 270);
ORIENTATIONS.append(Surface.ROTATION_270, 180);
}
private int getOrientation(int rotation) {
return (ORIENTATIONS.get(rotation) + mSensorOrientation + 270) % 360;
}
....
mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
int rotation = currentOrientation
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));
....
我使用OrientationEventListener来检测旋转
@Override
public void onOrientationChanged(int orientation) {
int currentOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;
if (orientation >= 330 || orientation < 30) {
currentOrientation = Surface.ROTATION_0;
} else if (orientation >= 60 && orientation < 120) {
currentOrientation = Surface.ROTATION_90;
} else if (orientation >= 150 && orientation < 210) {
currentOrientation = Surface.ROTATION_180;
} else if (orientation >= 240 && orientation < 300) {
currentOrientation = Surface.ROTATION_270;
}
if (prevOrientation != currentOrientation && orientation != OrientationEventListener.ORIENTATION_UNKNOWN) {
prevOrientation = currentOrientation;
if (currentOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) {
Log.i(TAG, "currentOrientation : " + currentOrientation);
uxOrientationChanged(currentOrientation);
}
}
}
我还比较了两个设备的值并且具有相同的旋转值和mSensorOrientation,有人可以帮忙吗?谢谢