【问题标题】:How to rotate camera screen at any angle in android?如何在android中以任意角度旋转相机屏幕?
【发布时间】:2013-05-20 07:33:36
【问题描述】:

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

}

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

【问题讨论】:

    标签: android rotation android-camera screen-rotation


    【解决方案1】:

    在你旋转你的相机之前,你应该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();
            }
        }
    

    【讨论】:

    • 有什么问题?不旋转或不更换相机?
    • 相机没有在所有 4 个方向上旋转并且保存的图像不正确。
    【解决方案2】:

    您应该使用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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-28
      • 1970-01-01
      • 2012-11-09
      • 1970-01-01
      • 1970-01-01
      • 2015-01-08
      • 1970-01-01
      相关资源
      最近更新 更多