4

我在尝试以纵向模式进行相机预览时遇到问题。我已经阅读了有关它的各种文章,并使用以下代码解决了它:

Display display = ((CaptureActivity)context).getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();

if (Integer.parseInt(Build.VERSION.SDK) >= 8) {
    setDisplayOrientation(camera, 90);
}else{
    Camera.Parameters parameters = camera.getParameters();
    parameters.set("orientation", "portrait");
    camera.setParameters(parameters);
}

其中 setDisplayOrientation() 定义为:

protected void setDisplayOrientation(Camera camera, int angle) {
    Method downPolymorphic;
    try {
        downPolymorphic = camera.getClass().getMethod(
                "setDisplayOrientation", new Class[] { int.class });
        if (downPolymorphic != null)
            downPolymorphic.invoke(camera, new Object[] { angle });
    } catch (Exception e1) {
    }
}

现在我在 Galaxy Tab 上尝试了这段代码,但失败了。我使用以下代码解决了它(尝试和错误方法):

if (height == 1024 && width == 600) {
    Camera.Parameters parameters = camera.getParameters();
    parameters.set("orientation", "portrait");
    parameters.setRotation(90);
    camera.setParameters(parameters);
}

现在我的两个问题是:

1) 为什么 Galaxy tab 有 2.2 版本时会出现这样的问题,以及

2)这个问题有没有更好的解决方案?

非常感谢您的时间!

4

1 回答 1

0

要设置显示方向,请查看官方文档,不要在那里硬编码 90 度。

于 2016-06-18T19:47:42.930 回答