在网上找了很多方法,但最后都有问题,自己调试了好几个小时,最后终于完美解决了竖屏识别。
首先你需要有zxing项目的简化版代码,在这里。
使用简化版可以免去许多不必要的代码,方便学习研究,更好定位核心功能。
如果你调试成功后,就可以着手修改将其变为竖屏识别了。
第1步:
在AndroidManifest中将CaptureActivity的screenOrientation属性做如下修改:
android:screenOrientation="portrait"
第2步:
我们要把摄像头预览景调为竖向
CameraConfigurationManager类中的setDesiredCameraParameters()方法中添加如下代码:
// 使摄像头旋转90度 setDisplayOrientation(camera, 90);
然后在CameraConfigurationManager类的最后添加setDisplayOrientation()方法:
/*改变照相机成像的方向的方法*/ protected void setDisplayOrientation(Camera camera, int angle) { Method downPolymorphic = null; try { downPolymorphic = camera.getClass().getMethod("setDisplayOrientation", new Class[] { int.class }); if (downPolymorphic != null) downPolymorphic.invoke(camera, new Object[]{angle}); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } }