我已经完成了一个应用程序,我必须录制视频。当我在我的模拟器中使用网络摄像头方向运行它是正常的,但当我在手机中运行相同时,方向得到90度改变。了解它发生了什么谁能帮我 ?这是我的代码---
private boolean prepareMediaRecorder(){
myCamera = getCameraInstance();
mediaRecorder = new MediaRecorder();
myCamera.unlock();
mediaRecorder.setCamera(myCamera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mediaRecorder.setOutputFile("/sdcard/video.mp4");
mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M
mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface());
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}
答案 0 :(得分:2)
预览方向取决于设备的方向和相机方向。
基本上您需要的是根据这些条件计算相机预览的方向。
我们需要两种帮助方法:
1 - 计算设备方向:
public int getDeviceOrientation(Context context) {
int degrees = 0;
WindowManager windowManager =
(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
int rotation = windowManager.getDefaultDisplay().getRotation();
switch(rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
return degrees;
}
2 - 计算相机预览旋转:
public static int getPreviewOrientation(Context context, int cameraId) {
int temp = 0;
int previewOrientation = 0;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, cameraInfo);
int deviceOrientation = getDeviceOrientation(context);
temp = cameraInfo.orientation - deviceOrientation + 360;
previewOrientation = temp % 360;
return previewOrientation;
}
在您的代码中,mediaRecorder.prepare();
之前
int rotation = getPreviewOrientation(context, cameraId);
mediaRecorder.setOrientationHint(rotation);
要使用这些方法,需要使用上下文和相机ID。