二维码扫描--横屏识别转为竖屏识别

[size=medium][color=gray]最近在参加一次比赛,需要实现一个二维码扫描的功能,于是找到了google的开源框架Zxing,我们可以去http://code.google.com/p/zxing/下载源码和Jar包。下载下来运行时发现二维码在识别时是横屏的,用户体验很不爽,于是在网上求教大佬们支招。具体解决方法如下:
1、在AndroidManifest.xml中,修改CaptureActivity的属性:将android:screenOrientation="landscape"改为portrait
2、找到DecodeHandler.java,修改其中的decode方法:
将PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(data, width, height);注释掉,并添加下面代码段:
byte[] newData = new byte[data.length];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++)
newData[x * height + height - y - 1] = data[x + y * width];
}
int tmp = width;
width = height;
height = tmp;

PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(newData, width, height);
3、找到CameraManager.java,修改其中的getFramingRectInPreview方法:
注释掉rect.left = rect.left * cameraResolution.x / screenResolution.x;
rect.right = rect.right * cameraResolution.x / screenResolution.x;
rect.top = rect.top * cameraResolution.y / screenResolution.y;
rect.bottom = rect.bottom * cameraResolution.y / screenResolution.y;
并添加rect.left = rect.left * cameraResolution.y / screenResolution.x;
rect.right = rect.right * cameraResolution.y / screenResolution.x;
rect.top = rect.top * cameraResolution.x / screenResolution.y;
rect.bottom = rect.bottom * cameraResolution.x / screenResolution.y;
4、找到CameraConfigurationManager.java,修改其中的initFromCameraParameters方法:
在最后添加代码Point screenResolutionForCamera =new Point();
screenResolutionForCamera.x = screenResolution.x;
screenResolutionForCamera.y = screenResolution.y;
if (screenResolution.x < screenResolution.y) {
screenResolutionForCamera.x = screenResolution.y;
screenResolutionForCamera.y = screenResolution.x;
}
cameraResolution = getCameraResolution(parameters, screenResolutionForCamera);
5、同样是上面的类,修改其中的方法setDesiredCameraParameters方法:
在最后添加代码
// 使摄像头旋转90度
setDisplayOrientation(camera, 90);
并写一个方法代码如下:
/*改变照相机成像的方向的方法*/
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();
}
}
至此修改已经结束,亲测有效。[/color][/size]

[size=small][color=gray]参考文章:[url]http://blog.csdn.net/chenbin520/article/details/16362459[/url]
[url]http://407827531.iteye.com/blog/1488676[/url][/color][/size]

你可能感兴趣的:(Android)