如何正确设置Android摄像头的方向? [英] How to set Android camera orientation properly?

查看:588
本文介绍了如何正确设置Android摄像头的方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据Android的,但没有设备的方向来设置相机的方向似乎是工作。我试着旋转曲面以及相机参数,但相机preVIEW在纵向模式下永远是上下颠倒。我需要90度顺时针旋转它为它是正确的。这里是code,我使用的是现在在横向模式下它才起作用。

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

    @覆盖
    公共无效surfaceDestroyed(SurfaceHolder持有者){
        camera.stop preVIEW();
        camera.release();
        摄像头= NULL;
    }

    @覆盖
    公共无效surfaceCreated(SurfaceHolder持有者){
        initCamera();
    }

    私人大小getOptimal previewSize(名单<大小>的大小,INT W,INT高){
        最终双ASPECT_TOLERANCE = 0.2;
        双targetRatio =(双)W / H;
        如果(大小== NULL)
            返回null;

        大小optimalSize = NULL;
        双minDiff = Double.MAX_VALUE;

        INT targetHeight = H;

        //试着找一个大小匹配纵横比和尺寸
        对于(尺寸大小:大小){
            Log.d(TAG,检查大小+ size.width +W+ size.height
                    +H);
            双率=(双)size.width / size.height;
            如果(Math.abs(比 -  targetRatio)> ASPECT_TOLERANCE)
                继续;
            如果(Math.abs(size.height  -  targetHeight)< minDiff){
                optimalSize =大小;
                minDiff = Math.abs(size.height  -  targetHeight);
            }
        }

        //不能找到一个匹配的纵横比,忽略
        // 需求
        如果(optimalSize == NULL){
            minDiff = Double.MAX_VALUE;
            对于(尺寸大小:大小){
                如果(Math.abs(size.height  -  targetHeight)< minDiff){
                    optimalSize =大小;
                    minDiff = Math.abs(size.height  -  targetHeight);
                }
            }
        }
        返回optimalSize;
    }

    @覆盖
    公共无效surfaceChanged(SurfaceHolder持有人,INT格式,诠释的宽度,高度INT){
        Camera.Parameters参数= camera.getParameters();

        名单<大小>大小= parameters.getSupported previewSizes();
        大小optimalSize = getOptimal previewSize(尺寸,宽,高);
        Log.d(TAG,表面大小+宽+W+高度+H);
        Log.d(TAG,最优尺寸为+ optimalSize.width +瓦特+ optimalSize.height +H);
        parameters.set previewSize(optimalSize.width,optimalSize.height);
        // parameters.set previewSize(宽,高);
        camera.setParameters(参数);
        camera.start preVIEW();
    }
};
 

解决方案

我终于用谷歌的相机应用解决了这个问题。它让手机的方向通过使用传感器,并设置适当的EXIF标签。它散发出来的相机的JPEG不会自动导向。

此外,相机preVIEW正常工作只能在横向模式。如果你需要你的活动布局为导向的肖像,你将不得不手动使用来自方位传感器的数值做。

I want to set the camera orientation according to the device orientation in Android but nothing seems to be working. I tried to rotate the Surface as well as the camera parameters but the camera preview in portrait mode always comes upside down. I would need to rotate it by 90 degree clockwise for it to be correct. Here is the code I am using right now which works in landscape mode only.

    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;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Camera.Parameters parameters = camera.getParameters();

        List<Size> sizes = parameters.getSupportedPreviewSizes();
        Size optimalSize = getOptimalPreviewSize(sizes, width, height);         
        Log.d(TAG, "Surface size is " + width + "w " + height + "h");
        Log.d(TAG, "Optimal size is " + optimalSize.width + "w " + optimalSize.height + "h");           
        parameters.setPreviewSize(optimalSize.width, optimalSize.height);           
        // parameters.setPreviewSize(width, height);            
        camera.setParameters(parameters);
        camera.startPreview();
    }
};  

解决方案

I finally fixed this using the Google's camera app. It gets the phone's orientation by using a sensor and then sets the EXIF tag appropriately. The JPEG which comes out of the camera is not oriented automatically.

Also, the camera preview works properly only in the landscape mode. If you need your activity layout to be oriented in portrait, you will have to do it manually using the value from the orientation sensor.

这篇关于如何正确设置Android摄像头的方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆