首页 > 学院 > 开发设计 > 正文

Google ZXing系列讲解(四)——ZXing 解决竖屏扫描问题

2019-11-06 09:52:34
字体:
来源:转载
供稿:网友

前言

本篇文章带你了解 - ZXing Android Demo竖屏后无法实现扫描原因 - ZXing 横竖屏下扫描条码/二维码

ZXing横竖屏扫描

默认的ZXing Demo提供的是横屏扫描,讲真,确实不符合使用习惯。然,仅仅修改AndroidManifest.xml中的 android:screenOrientationportrait 无法扫描成功。

<activity android:name=".CaptureActivity"- android:screenOrientation="sensorLandscape"+ android:screenOrientation="portrait" android:clearTaskOnLaunch="true"

要想在竖屏下, 扫描成功,手机还得横屏扫码。(无语!”’)

问题分析

有关ZXing增加竖屏扫描问题,在ZXing issue中有提到,作者跟网友讨论了不少, 也有人给出其他的解决方案。但是作者标记为了 enhancement , 到现在3.3.0稳定版本 还是没有实现改善。 https://github.com/zxing/zxing/issues/111

仔细查看其中的讨论,会有不少收获: - 有人给出了其他方法,基于ZXing 网友封装的jar - 给出特定的解决方法

这篇文章是基于其中名叫 @GuoJinyu 的方法实现。该同学深入研究了ZXing,并说明横屏无法扫描的原因。 首先给出了Android设备上, 存在以下几个概念:

屏幕方向:在Android系统中,屏幕的左上角是坐标系统的原点(0,0)坐标。原点向右延伸是X轴正方向,原点向下延伸是Y轴正方向。 相机传感器方向:手机相机的图像数据都是来自于摄像头硬件的图像传感器,这个传感器在被固定到手机上后有一个默认的取景方向,坐标原点位于手机横放时的左上角,即与横屏应用的屏幕X方向一致。换句话说,与竖屏应用的屏幕X方向呈90度角。 相机的预览方向:由于手机屏幕可以360度旋转,为了保证用户无论怎么旋转手机都能看到“正确”的预览画面(这个“正确”是指显示在UI预览界面的画面与人眼看到的眼前的画面是一致的),Android系统底层根据当前手机屏幕的方向对图像传感器采集到的数据进行了旋转处理,然后才送给显示系统,因此可以保证预览画面始终“正确”。在相机API中可以通过setDisplayOrientation()设置相机预览方向。在默认情况下,这个值为0,与图像传感器一致。因此对于横屏应用来说,由于屏幕方向和预览方向一致,预览图像不会颠倒90度。但是对于竖屏应用,屏幕方向和预览方向垂直,所以会出现颠倒90度现象。为了得到正确的预览画面,必须通过API将相机的预览方向旋转90,保持与屏幕方向一致

也就是说,相机得到的图像数据始终是一个横屏的姿态,当手机处于竖屏时,即使我们通过设置在屏幕上看到的拍摄画面是准确的,没有90度翻转的,我们通过API得到的图像数据仍然是基于横屏的,因此在判断到width < height即屏幕处于竖屏状态时,我们首先对byte数组进行一个手动90度旋转,然后将结果构造为一个PlanarYUVLuminanceSource对象,进行真正的解析处理去了,这里我们就不管了。

详细参看 Android二维码扫描的简单实现及源码分析

ZXing 横竖屏下扫描条码/二维码

本篇基于ZXing3.3.0分析,参考了 https://github.com/zxing/zxing/issues/111 中 @GuoJinyu的方法

1. 删除 android:screenOrientation=”sensorLandscape”

AndroidManifest.xml

2. CaptureActivity内部类 MyOrientationDetector使用

2.1 定义内部类PRivate class MyOrientationDetector extends OrientationEventListener { private int lastOrientation = -1; MyOrientationDetector(Context context) { super(context); } void setLastOrientation(int rotation) { switch (rotation) { case Surface.ROTATION_90: lastOrientation = 270; break; case Surface.ROTATION_270: lastOrientation = 90; break; default: lastOrientation = -1; } } @Override public void onOrientationChanged(int orientation) { Log.d(TAG, "orientation:" + orientation); if (orientation > 45 && orientation < 135) { orientation = 90; } else if (orientation > 225 && orientation < 315) { orientation = 270; } else { orientation = -1; } if ((orientation == 90 && lastOrientation == 270) || (orientation == 270 && lastOrientation == 90)) { Log.i(TAG, "orientation:" + orientation + "lastOrientation:" + lastOrientation); Intent intent = getIntent(); finish(); startActivity(intent); lastOrientation = orientation; Log.i(TAG, "SUCCESS"); } } }2.2 声明MyOrientationDetector变量MyOrientationDetector myOrientationDetector;2.3 onCreate中使用 //add by tancolo myOrientationDetector = new MyOrientationDetector(this); myOrientationDetector.setLastOrientation(getWindowManager().getDefaultDisplay().getRotation()); //end add2.4 onResume中使用if (prefs.getBoolean(PreferencesActivity.KEY_DISABLE_AUTO_ORIENTATION, true)) { setRequestedOrientation(getCurrentOrientation()); } else { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); // 旋转 myOrientationDetector.enable(); //启用监听 }2.5 onPause中使用 //add by tancolo myOrientationDetector.disable(); //end add super.onPause();

3. 获得准确的相机预览框 (CameraManager.java)

getFramingRectInPreview()

if (cameraResolution == null || screenResolution == null) { // Called early, before init even finished return null; } //add by tancolo if(screenResolution.x < screenResolution.y){ // portrait 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; } else { // landscape 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; } // end add

4. 修改PreviewCallback.java

onPreviewFrame()

Handler thePreviewHandler = previewHandler; if (cameraResolution != null && thePreviewHandler != null) { //add by tancolo Point screenResolution = configManager.getScreenResolution(); Message message; if (screenResolution.x < screenResolution.y){ // portrait message = thePreviewHandler.obtainMessage(previewMessage, cameraResolution.y, cameraResolution.x, data); } else { // landscape message = thePreviewHandler.obtainMessage(previewMessage, cameraResolution.x, cameraResolution.y, data); }// Message message = thePreviewHandler.obtainMessage(previewMessage, cameraResolution.x,// cameraResolution.y, data); //end add

5. DecodeHandler.java

decode()

long start = System.currentTimeMillis(); //add by tancolo if (width < height) { // portrait byte[] rotatedData = new byte[data.length]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) rotatedData[y * width + width - x - 1] = data[y + x * height]; } data = rotatedData; } //end add

6. 验证成果

MIUI8.1,CM12.1版本 扫描条码/二维码, 通过!

开启手机旋转模式竖屏开启 ZXing app -> 扫描OK横屏开启ZXing app -> 扫描OK 修改后ZXing apk 下载地址
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表