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

[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]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值