因版本差异,扫描效果略有不同,我测试的时候使用的是ZXING3.22,与原文作者使用版本一致,修改后,测试通过,在此仅作记录,并感谢原作者的无私贡献,谢谢!
转自:https://blog.csdn.net/xiaojun8547093/article/details/48784431?ticket=ST-75335-QFEIquoDoNoA5L3R6cqM-passport.csdn.net
原文:
在网上找了很多源码,也找了很多很多修改的方法,但都会出现复杂的二维码扫描不出来,或者扫描很慢的问题,所以,自己把现在修改的整理下,以后自己好查看.
1.先修改CameraConfigurationManager类
cameraResolution = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, screenResolution);
//修改为
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 = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, screenResolutionForCamera);
/*结束*/
2.在setDesiredCameraParameters方法中添加
在第一行添加就可以了
camera.setDisplayOrientation(90);
/*结束*/
3. 修改CameraManager类
// 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.修改DecodeHandler类
// 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];
}
int tmp = width;
width = height;
height = tmp;
PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(rotatedData, width, height);
5.修改CaptureActivity类
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
// return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
// return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
6.最后将清单文件AndroidManifest.xml
> android:screenOrientation="portrait"`
运行,测试成功!