不,没有办法让 API 为您执行此操作,但要弄清楚您需要如何旋转数据,请查看Camera.Parameters.setRotation中的示例代码。虽然 setRotation() 仅影响 JPEG,但您希望对在 onPreviewFrame() 中获得的数据应用与对 JPEG 完全相同的旋转量。
在此处复制示例代码,稍作更改:
public void onOrientationChanged(int orientation) {
if (orientation == ORIENTATION_UNKNOWN) return;
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
orientation = (orientation + 45) / 90 * 90;
int rotation = 0;
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
rotation = (info.orientation - orientation + 360) % 360;
} else { // back-facing camera
rotation = (info.orientation + orientation) % 360;
}
mCameraOrientation = rotation; // Store rotation for later use
}
...
void onPreviewFrame(byte[] data, Camera camera) {
switch(mCameraOrientation) {
case 0:
// data is correctly rotated
break;
case 90:
// rotate data by 90 degrees clockwise
case 180:
// rotate data upside down
case 270:
// rotate data by 90 degrees counterclockwise
}
}
所以你需要从 OrientationEventListener 继承并覆盖 onOrientationChanged ,然后使用从那里计算的方向值来旋转预览帧。