How to set Android camera orientation properly?

来源:互联网 发布:淘宝发布宝贝多种口味 编辑:程序博客网 时间:2024/04/29 00:48

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().orientation == Configuration.ORIENTATION_LANDSCAPE)        {            p.set("orientation", "landscape");            p.set("rotation", 90);        }    } 
原创粉丝点击