如何正确设置Android相机方向?

如何正确设置Android相机方向?

我想根据Android中的设备方向设置相机方向,但似乎没有任何效果。我尝试旋转Surface以及相机参数,但是纵向模式下的相机预览总是颠倒过来。我需要顺时针旋转90度才能使其正确。这是我现在使用的代码,仅适用于横向模式。

    SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        camera.stopPreview();
        camera.release();
        camera = null;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {          
        initCamera();           
    }

    private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
        final double ASPECT_TOLERANCE = 0.2;
        double targetRatio = (double) w / h;
        if (sizes == null)
            return null;

        Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;

        int targetHeight = h;

        // Try to find an size match aspect ratio and size
        for (Size size : sizes) {
            Log.d(TAG, "Checking size " + size.width + "w " + size.height                    + "h");
            double ratio = (double) size.width / size.height;
            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
                continue;
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }

        // Cannot find the one match the aspect ratio, ignore the
        // requirement
        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }


素胚勾勒不出你
浏览 1377回答 3
3回答

慕勒3428872

此解决方案适用于所有&nbsp;Android版本。您可以在Java中使用反射使其适用于所有Android设备:基本上你应该创建一个反射包装来调用Android 2.2 setDisplayOrientation,而不是调用特定的方法。方法:&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;void&nbsp;setDisplayOrientation(Camera&nbsp;camera,&nbsp;int&nbsp;angle){ &nbsp;&nbsp;&nbsp;&nbsp;Method&nbsp;downPolymorphic; &nbsp;&nbsp;&nbsp;&nbsp;try &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;downPolymorphic&nbsp;=&nbsp;camera.getClass().getMethod("setDisplayOrientation",&nbsp;new&nbsp;Class[]&nbsp;{&nbsp;int.class&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(downPolymorphic&nbsp;!=&nbsp;null) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;downPolymorphic.invoke(camera,&nbsp;new&nbsp;Object[]&nbsp;{&nbsp;angle&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;catch&nbsp;(Exception&nbsp;e1) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;}}而不是使用camera.setDisplayOrientation(x)使用setDisplayOrientation(camera,x):&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(Integer.parseInt(Build.VERSION.SDK)&nbsp;>=&nbsp;8) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setDisplayOrientation(mCamera,&nbsp;90); &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(getResources().getConfiguration().orientation&nbsp;==&nbsp;Configuration.ORIENTATION_PORTRAIT) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p.set("orientation",&nbsp;"portrait"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p.set("rotation",&nbsp;90); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(getResources().getConfiguration().orientation&nbsp;==&nbsp;Configuration.ORIENTATION_LANDSCAPE) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p.set("orientation",&nbsp;"landscape"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p.set("rotation",&nbsp;90); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android