首先我在github上下载zxing的时间是2014年8月,版本好像是zxing-3。
个人参考了这篇文章,很不错
http://blog.csdn.net/chenbin520/article/details/16362459
但是还是有点问题,毕竟年代久远了点,zxing源代码是有一定修改的。
其实我稍微修改的地方就是CaptureActivity的onResume方法,找到
if (prefs.getBoolean(PreferencesActivity.KEY_DISABLE_AUTO_ORIENTATION, true)) {
setRequestedOrientation(getCurrentOrientation());
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
直接替换成
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
这样xml里面都不用指定方向了。其他大家参考上面链接的文章。我这里也把我的步骤贴出来。
1.如上,替换CaptureActivity的onResume里面的
if (prefs.getBoolean(PreferencesActivity.KEY_DISABLE_AUTO_ORIENTATION, true)) {
setRequestedOrientation(getCurrentOrientation());
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
为
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
这时屏幕就变成竖屏了,但是相机成像的图像却旋转了。
2.找到这个类CameraConfigurationManager的这个方法setDesiredCameraParameters,我是在camera.setParameters(parameters);后面添加了如下代码camera.setDisplayOrientation(90);其他地方插入会不会有影响没测试,这时候相机拍摄成像方向正常了。改完这两个地方好像可以啦,但是图像有点拉伸,扫二维码好像也没问题,但是扫竖线条码却还是要把手机转动90度,咱们继续。
3.继续在CameraConfigurationManager里面找到initFromCameraParameters方法,把
cameraResolution = CameraConfigurationUtils.findBestPreviewSizeValue(
parameters, screenResolution);
替换成
if (screenResolution.x < screenResolution.y) {
screenResolutionForCamera.x = screenResolution.y;
screenResolutionForCamera.y = screenResolution.x;
}
cameraResolution = CameraConfigurationUtils.findBestPreviewSizeValue(
parameters, screenResolutionForCamera);
这样子屏幕垂直方向的拉伸就没了。
4.然后找到CameraManager这个类,把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;
5.最后找到DecodeHandler这个类,把decode里的
PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height);
这段替换为
byte[] rotatedData = new byte[data.length];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++)
rotatedData[x * height + height - y - 1] = data[x + y * width];
}
PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(rotatedData, height, width);
经过这几步,我手机上是没问题了,小伙伴,你的问题解决没?