此博客是基于开源的框架ZXing。ZXing用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口。ZXing可以实现使用手机的内置的摄像头完成条形码的扫描及解码。该项目可实现的条形码编码和解码。
http://download.csdn.net/detail/u013761665/8853561
第1步:
进入com.google.zxing.client.android包下的CaptureActivity类:
注释以下代码:
if (prefs.getBoolean(PreferencesActivity.KEY_DISABLE_AUTO_ORIENTATION, true)) { setRequestedOrientation(getCurrentOrientation()); } else { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); }
第2步:
进入com.google.zxing.client.android包下的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; // Here we are swapping, that's the difference to #11 width = height; height = tmp; data = rotatedData;
第3步:
进入com.google.zxing.client.android.camera包下的CameraConfigurationManager类:
在Log.i(TAG, "Screen resolution: " + 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);
第4步:
进入com.google.zxing.client.android.camera包下的CameraConfigurationManager类:
在代码camera.setParameters(parameters)的上面添加:
camera.setDisplayOrientation(90);
第5步:
进入com.google.zxing.client.android.camera包下的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;
上面的5步操作会让原本横屏扫描变为竖屏扫描。不过只能是一个方向上的。如果你想让用户可以在横屏和竖屏两种情况下都能使用的话,就要做一些必要的处理了。
主要的思路就是,设置一个标志位Flag来标志当前的横竖屏状态。Flag是在扫描界面CaptureActivity的onResume的时候进行变换,并对外提供getter接口。不过在我的代码中还用到了一点就是使用EventBus框架来发布信息。
竖屏
横屏
版权声明:本文为博主原创文章,未经博主允许不得转载。
修正Android基于ZXing的二维码扫描——横竖屏自由切换
原文地址:http://blog.csdn.net/lemon_tree12138/article/details/46692999