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 に答える