Camera.parames.setDisplayOrientation问题解决

针对log错误:

java.lang.RuntimeException: set display orientation failed

首先确保,自己的SuferView控件可获取大小,意思是,Suferview有设置长和宽,之前因为一个项目中的Suferveiew是使用的自定义的控件,所以在使用的使用时在java代码中动态创建的,而导致后面不管怎么设置都不管用,一般情况下,可设置SuferView的大小为满屏,

接着,看应用是否需要在2.1以下版本运行,需要在2.1以下又需要根据不同系统之间来做稍微的处理,

/**
	 * 2.1之前的版本设置摄像头旋转90度
	 * @param orientation
	 */
	@SuppressLint("NewApi")
	public void setCameraDisplayOrientation(Camera camera,int orientation) {
		if(android.os.Build.VERSION.SDK_INT>=8)
			setDisplayOrientation(camera,90);
		else {
			if(null == camera)
				return;
			Method method;
			try {
				//通过反射机制获取相应的方法
				method = camera.getClass().getMethod("setDisplayOrientation", new Class[]{int.class});
				if(null != method){
					method.invoke(camera, new Object[]{orientation});
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * 
	 * @param camera
	 * @param angle
	 */
	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)
	    {
	    }
	}
针对 setDisplayOrientation方法,的解释是在调用直接使用camera.setDisplayOrientation(90);抛出异常时,可有使用该方法来设置。
经验分享 程序员 微信小程序 职场和发展