80,350
社区成员
发帖
与我相关
我的任务
分享
public static int getDisplayOrientation(int degrees, int cameraId) {
// See android.hardware.Camera.setDisplayOrientation for
// documentation.
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
return result;
}
楼主用上面代码获取Camera Preview的方向,然后调用setDisplayOrientation设置
上述方法的degree默认是0,一般手机不用改,有些手机硬件确实存在方向不对的bug,需要自行调试后选择对应的degree
cameraId可以通过下面方法获取,其中-1表示不支持任何摄像头
public static int getCameraId(boolean front) {
int num = Camera.getNumberOfCameras();
for (int i = 0; i < num; i++) {
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == CameraInfo.CAMERA_FACING_FRONT && front) {
return i;
}
if (info.facing == CameraInfo.CAMERA_FACING_BACK && !front) {
return i;
}
}
return -1;
}