我在尝试以竖屏模式预览相机时遇到了问题。我已经阅读了多篇相关文章,并通过以下代码解决了这个问题:
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)是否有更好的解决方案来解决这个问题?
非常感谢您的时间!