2

我需要将我的相机屏幕顺时针和逆时针旋转 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();
    }

}

如果有人能告诉我如何在我的应用程序中使用内置相机功能,那么也欢迎。

4

2 回答 2

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);
于 2014-03-26T08:34:42.817 回答
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();
        }
    }
于 2013-05-20T08:43:35.780 回答