对于前后摄像头,我需要以90,180,270,360角度顺时针旋转我的摄像机屏幕,逆时针旋转。我在SurfaceView上应用了一个代码块。我在这里提供代码:
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// Set camera preview size,orientation,rotation using parameters
if (camera != null) {
if (Build.VERSION.SDK_INT >= 8) {
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(camId, info);
int rotation = getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
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;
}
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.Parameters parameters = camera.getParameters();
camera.setParameters(parameters);
camera.startPreview();
}
}
如果有人可以告诉我如何在我的应用程序中使用内置相机功能,那么也欢迎。
答案 0 :(得分:0)
release
<强>第一强>
if(mCamera != null){
mCamera.stopPreview();
mCamera.release();
}
<强>第二强> 如果你想改变前置或后置摄像头
try{
mCamera = Camera.open(currentFacing);
}catch(Exception e){
e.printStackTrace();
}
if (mCamera == null) {
mCamera = Camera.open();
}
过去强>
if(mCamera != null && mHolder.getSurface() != null){
try{
//here change the degress
mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
}catch(Exception e){
e.printStackTrace();
}
}
答案 1 :(得分:0)
您应该使用Camera.setRotation
来正确捕捉图像,而不只是设置预览的显示方向。您还应该以不同方式处理前后摄像头。以下代码应该处理任何屏幕方向,包括前后摄像头的不同处理:
// Set camera rotation (consider display orientation)
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int displayOrientation = display.getRotation();
int rotation = cameraInfo.orientation;
if (Surface.ROTATION_0 != displayOrientation)
{
if (CameraInfo.CAMERA_FACING_BACK == cameraInfo.facing)
{
if (Surface.ROTATION_90 == displayOrientation)
{
rotation -= 90;
}
else if (Surface.ROTATION_180 == displayOrientation)
{
rotation -= 180;
}
if (Surface.ROTATION_270 == displayOrientation)
{
rotation -= 270;
}
if (rotation < 0)
{
rotation += 360;
}
}
else
{
if (Surface.ROTATION_90 == displayOrientation)
{
rotation += 90;
}
else if (Surface.ROTATION_180 == displayOrientation)
{
rotation += 180;
}
if (Surface.ROTATION_270 == displayOrientation)
{
rotation += 270;
}
rotation %= 360;
}
}
Log.d(TAG, "Camera orientation (" + cameraInfo.orientation + ", " + displayOrientation + ", " + rotation + ")");
cameraParams.setRotation(rotation);