android zxing 横屏改为竖屏的配置
android的zxing扫描demo默认是横屏的,但是竖屏更加符合目前的使用情况,网上也有很多关于改为横屏的文章,但是有些地方的配置是错误的,在此本人在实践的基础上重新整理一份修改说明,以作备忘。
【一】修改 AndroidManifest.xml,改为竖屏
将扫描的Activity的
android:screenOrientation="landscape"
更改为:
android:screenOrientation="portrait"
【二】修改 CameraConfigurationManager 类
1.竖屏后预览方向变为横向的问题
修改方法 setDesiredCameraParameters,在方法末尾添加以下代码:
if (Integer.parseInt(Build.VERSION.SDK) >= 8)setDisplayOrientation(camera, 90);else {if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {parameters.set("orientation", "portrait");parameters.set("rotation", 90);}if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {parameters.set("orientation", "landscape");parameters.set("rotation", 90);}}2.预览图像拉伸的问题
修改方法 initFromCameraParameters,在Log.d(TAG, "Screen resolution: " + screenResolution);后添加以下代码:
// 竖屏后,预览拉伸的问题---start---- Point screenResolutionForCamera = new Point(); screenResolutionForCamera.x = screenResolution.x; screenResolutionForCamera.y = screenResolution.y; // preview size is always something like 480*320, other 320*480 if (screenResolution.x < screenResolution.y) { screenResolutionForCamera.x = screenResolution.y; screenResolutionForCamera.y = screenResolution.x; } // 竖屏后,预览拉伸的问题---end----同时修改 cameraResolution = getCameraResolution(parameters, screenResolution );
为cameraResolution = getCameraResolution(parameters, screenResolutionForCamera);
【三】修改 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;
【四】修改 DecodeHandler 类
修改方法 decode,在source = CameraManager.get().buildLuminanceSource(data, width, height);
之前添加以下代码:
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; data = rotatedData;另外,网上有文章说的修改 PlanarYUVLuminanceSource.java中的renderCroppedGreyscaleBitmap方法,将其中的dataWidth和dataHeight互换等的修改都不需要,否则虽然可以正常扫描,但是会影响扫描后图片的生产。
关于更过自定义的修改,可以参考网上其他文章,也感谢网友的分享。
取景框的绘制、连续扫描等可参到以下文章:
http://www.cnblogs.com/dolphin0520/p/3355728.html
http://www.wefashional.com/f/bokeDetail.jhtml?id=257
http://www.2cto.com/kf/201506/404765.html
更多相关文章
- Android文件读写实例代码
- Android 公共库的建立方法
- 浅谈Android中的 Fragment、生命周期回调方法 以及使用
- android的intent使用方法 .
- 网络对讲机C#服务器 Android客户端(三) android客户端代码分析 附
- Android文件系统的提取方法(一)
- 第一行代码(第二版)自学笔记之第一篇:Android Studio开发环境的搭建