Android 相机 setDisplayOrientation:Galaxy Tab 的奇怪行为

发布于 2024-12-11 01:07:29 字数 1336 浏览 5 评论 0原文

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

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)有没有更好的解决方案来解决这个问题?

非常感谢您抽出时间!

I face a problem a problem trying to have a camera preview in portrait mode. I have read various articles about it and I had solved it having the following code:

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

where setDisplayOrientation() is defined as:

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) {
    }
}

Now I tried this code to a Galaxy Tab and it failed. I solved it (trying and error approach) using the following code:

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

Now my two questions are:

1) Why there is such problem while Galaxy tab has the 2.2 version, and

2) Is there any better solution to this problem?

Thanks a lot for your time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别闹i 2024-12-18 01:07:29

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

for setting the display orientation check out the official docs, dont just hardcode 90 degrees there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文