我没做过视频,不过从描述猜测是前后置摄像头切换的问题,希望以下方法能帮到你 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; }
monodin, 你贴的第一段代码我也看过,其中的“compensate the mirror”十分令我费解,“(360 - result) % 360”只是变换了角度,怎么和mirror相关呢?还请解释一下
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;
}