现在的位置: 首页 > 综合 > 正文

How to set Android camera orientation properly?

2013年01月21日 ⁄ 综合 ⁄ 共 916字 ⁄ 字号 评论关闭

This solution will work for all versions
of Android. You can use reflection in Java to make it work for all Android devices:

Basically you should create a reflection wrapper to call the Android 2.2 setDisplayOrientation, instead of calling the specific method.

The method:

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

And instead of using camera.setDisplayOrientation(x) use setDisplayOrientation(camera,
x)
 :

    if (Integer.parseInt(Build.VERSION.SDK) >= 8)
        setDisplayOrientation
(mCamera, 90);
   
else
   
{
       
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
       
{
            p
.set("orientation", "portrait");
            p
.set("rotation", 90);
       
}
       
if (getResources().getConfiguration().

抱歉!评论已关闭.