我正在使用包含前置和后置摄像头的自定义录像机。当我开始在后置摄像头录制时没有问题,当我开始在前置摄像头录制时,摄像头的亮度降低。
我有摄像头切换按钮来改变前后摄像头
int currentCameraId;
reverseCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCamera.stopPreview();
mCamera.release();
if (currentCameraId == Camera.CameraInfo.CAMERA_FACING_BACK) {
currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
} else {
currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
}
mCamera = Camera.open(currentCameraId);
setCameraDisplayOrientation(UnlVideoActivity.this, currentCameraId, mCamera);
try {
mCamera.setPreviewDisplay(mHolder);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
}
});
public void setCameraDisplayOrientation(Activity activity,
int cameraId, android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
params.setRotation(rotation);
switch (rotation) {
case Surface.ROTATION_0:
Toast.makeText(UnlVideoActivity.this, "LAND", Toast.LENGTH_SHORT).show();
degrees = 0;
break;
case Surface.ROTATION_90:
Toast.makeText(UnlVideoActivity.this, "PORT", Toast.LENGTH_SHORT).show();
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
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;
}
camera.setDisplayOrientation(result);
camera.startPreview();
}
通过这种方式,当我开始记录亮度变低时,我会切换前置摄像头。
录制代码
private boolean prepareMediaRecorder() {
mCamera = getCameraInstance();
mediaRecorder = new MediaRecorder();
mediaRecorder.setOrientationHint(orientationHint);
mCamera.unlock();
mediaRecorder.setPreviewDisplay(mHolder.getSurface());
mediaRecorder.setCamera(mCamera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mediaRecorder.setOutputFile("/sdcard/myvideo.mp4");
mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M
mediaRecorder.setPreviewDisplay(mCameraSurfaceView.getHolder().getSurface());
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}