首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何正确设置Android摄像头的朝向?

如何正确设置Android摄像头的朝向?
EN

Stack Overflow用户
提问于 2011-01-10 18:47:43
回答 6查看 178.6K关注 0票数 91

我想根据Android中的设备方向来设置摄像头方向,但似乎什么都不起作用。我试图旋转曲面以及相机参数,但在纵向模式下相机预览总是颠倒。我需要将它顺时针旋转90度才能正确。这是我现在使用的代码,它只在横向模式下工作。

代码语言:javascript
复制
    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();
    }
};  
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2011-03-18 17:36:32

我终于用谷歌的相机应用解决了这个问题。它通过使用传感器获取手机的方向,然后适当地设置EXIF标签。相机输出的JPEG不是自动定向的。

此外,相机预览仅在横向模式下才能正常工作。如果您需要将activity布局设置为纵向,则必须使用方向传感器的值手动执行此操作。

票数 5
EN

Stack Overflow用户

发布于 2011-02-28 18:48:42

来自其他成员和我的问题:

摄像头旋转问题取决于不同的设备和特定的版本。

版本1.6:修复旋转问题,适用于大多数设备

代码语言:javascript
复制
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
        {   
            p.set("orientation", "portrait");
            p.set("rotation",90);
        }
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
        {                               
            p.set("orientation", "landscape");          
            p.set("rotation", 90);
        }

版本2.1:取决于设备的种类,例如,不能解决XPeria X10的问题,但它对X8和迷你很好

代码语言:javascript
复制
Camera.Parameters parameters = camera.getParameters();
parameters.set("orientation", "portrait");
camera.setParameters(parameters);

版本2.2:并非适用于所有设备

代码语言:javascript
复制
camera.setDisplayOrientation(90);

http://code.google.com/p/android/issues/detail?id=1193#c42

票数 71
EN

Stack Overflow用户

发布于 2012-04-19 05:29:02

setDisplayOrientation(int)的Javadoc(需要API9级):

代码语言:javascript
复制
 public static void setCameraDisplayOrientation(Activity activity,
         int cameraId, android.hardware.Camera camera) {
     android.hardware.Camera.CameraInfo info =
             new android.hardware.Camera.CameraInfo();
     android.hardware.Camera.getCameraInfo(cameraId, info);
     int rotation = activity.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);
 }
票数 31
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4645960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档