14

我已经创建了一个相机应用程序,我希望我的应用程序能够以所有 4 个可能的方向打开,并相应地更新相机预览。为此,我使用了从以下方法复制的方法: Android - Camera preview is sideways

 public void updateCameraDisplay(int w, int h) {
        // set preview size and make any resize, rotate or
        // reformatting changes here

        Log.i("CameraPreviews", "Updating camera orientation with w=" + w
                + " and h=" + h);
        Parameters parameters = camera.getParameters();
        Display display = getActivity().getWindowManager()
                .getDefaultDisplay();

        int rotation = getActivity().getResources().getConfiguration().orientation;
        Log.i("CameraPreviews", "rotation is " + display.getRotation());
        if (display.getRotation() == Surface.ROTATION_0) {

            parameters.setPreviewSize(h, w);
            camera.setDisplayOrientation(0);
        }

        if (display.getRotation() == Surface.ROTATION_90) {
            parameters.setPreviewSize(w, h);
            camera.setDisplayOrientation(270);
        }

        if (display.getRotation() == Surface.ROTATION_180) {
            parameters.setPreviewSize(h, w);
            camera.setDisplayOrientation(180);
        }

        if (display.getRotation() == Surface.ROTATION_270) {
            parameters.setPreviewSize(w, h);
            camera.setDisplayOrientation(90);
        }

        try {
            camera.setParameters(parameters);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我已经调整了这些值,在三星 Galaxy Tab2 上对其进行了测试,以最终获得正确的方向,并且一切正常。当我在htc one的手机上尝试它时,它根本不起作用!!!!!!!所有的 e 方向都是完全错误的!所以我得出的结论是,必须有 2 种类型的设备(或更多......请不要!)因为旋转表示屏幕从其“默认”位置旋转了多少度,然后某些设备有一个默认位置和其他人。我怎样才能知道这个默认轮换并在我的代码中采取相应的行动?ej: defaultOrientation=some code if(defaultOrientation==0) ... else ...

锁定屏幕方向是不可能的。目标 api>=11 非常感谢

编辑:我已将代码修改为:

public void updateCameraDisplay(int w, int h) {
        // set preview size and make any resize, rotate or
        // reformatting changes here

        Log.i("CameraPreviews", "Updating camera orientation with w=" + w
                + " and h=" + h);
        Parameters parameters = camera.getParameters();
        Display display = getActivity().getWindowManager()
                .getDefaultDisplay();

        int rotation = getActivity().getResources().getConfiguration().orientation;
        Log.i("CameraPreviews", "screen rotation is " + rotation);
        Log.i("CameraPreviews", "display rotation is " + display.getRotation());
        if (display.getRotation() == Surface.ROTATION_0) {

            if (rotation == Configuration.ORIENTATION_LANDSCAPE) {
                parameters.setPreviewSize(h, w);
                camera.setDisplayOrientation(0);
            } else {
                parameters.setPreviewSize(h, w);
                camera.setDisplayOrientation(90);
            }
        }

        else if (display.getRotation() == Surface.ROTATION_90) {
            if (rotation == Configuration.ORIENTATION_PORTRAIT) {
                parameters.setPreviewSize(w, h);
                camera.setDisplayOrientation(270);
            } else {
                parameters.setPreviewSize(w, h);
                //camera.setDisplayOrientation(0);
            }
        }

        else if (display.getRotation() == Surface.ROTATION_180) {
            if (rotation == Configuration.ORIENTATION_LANDSCAPE) {
            parameters.setPreviewSize(h, w);
            camera.setDisplayOrientation(180);
            }else {
                parameters.setPreviewSize(h, w);
                camera.setDisplayOrientation(270);
            }
        }

        else if (display.getRotation() == Surface.ROTATION_270) {
            if (rotation == Configuration.ORIENTATION_PORTRAIT) {
                parameters.setPreviewSize(w, h);
                camera.setDisplayOrientation(90);
            } else {
                parameters.setPreviewSize(w, h);
                camera.setDisplayOrientation(180);
            }
        }

        try {
            camera.setParameters(parameters);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

只要我们不在纵向模式下倒置旋转手机,它在 htc one s 和 samsung galaxy tab 上的效果会更好

4

1 回答 1

0

the factors you need to think about are: the orientation of the device, the degree between the screen and the camera, assuming you are using the back camera, and whether you enable the activity to sense the orientation change.

And one important thing is whether the Camera HAL code implemented by the manufacturer is compliant with Google's protocol.

于 2014-04-17T22:22:40.003 回答