|
| 1 | +>- 文章来源:itsCoder 的 [WeeklyBolg](https://github.com/itsCoder/weeklyblog) 项目 |
| 2 | +>- itsCoder主页:[http://itscoder.com/](http://itscoder.com/) |
| 3 | +>- 作者:[Melo](https://itsmelo.github.io/) |
| 4 | +>- 审阅者:[]() |
| 5 | +
|
| 6 | +**写在前面:** |
| 7 | + |
| 8 | +春天到了,天气转暖,风吹走了北京的雾霾也带来了困倦。每天感觉就是睡不醒、起不来。前一阵研究了 View 的体系,还差滑动冲突和 View 的绘制没有落笔成文,还看了很多关于 MVP 这种代码模式的文章,未来争取把它们都整理分享出来,便于记忆和交流。 |
| 9 | + |
| 10 | +不知不觉二维码已经深刻影响了我们的生活,为我们提供了极大的便利。线下付账、租一辆单车、或者去要一个妹子的微信号等等。张小龙把它称为从线下到线上的入口。正因为二维码如此的重要,并且出现的频率越来越高,所以 Android 应用中扫面二维码、条形码的需求也很常见了。本文就是来接入使用一个不错的二维码库 **ZXing** |
| 11 | + |
| 12 | +二维码是什么 |
| 13 | +-- |
| 14 | + |
| 15 | +在研究 ZXing 之前,我一直好奇二维码是根据什么生成的,并且最大能保存多少的信息,去查阅了一下资料,正好解决了我的疑问,在此介绍一下。 |
| 16 | + |
| 17 | +二维条码是指在一维条码(条形码)的基础上扩展出另一维具有可读性的条码,使用黑白矩形图案表示二进制数据,被设备扫描后可获取其中所包含的信息。一维条码的宽度记载着数据,而其长度没有记载数据。二维条码的长度、宽度均记载着数据。二维条码有一维条码没有的“定位点”和“容错机制”。容错机制在即使没有辨识到全部的条码、或是说条码有污损时,也可以正确地还原条码上的信息。二维条码的种类很多,不同的机构开发出的二维条码具有不同的结构以及编写、读取方法。 |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +总结起来就是二维码是将有限的信息转成二进制,并且表现为黑白矩阵图,与一维的条形码相比,二维码拥有更好的容错能力。 |
| 22 | + |
| 23 | +ZXing |
| 24 | +-- |
| 25 | +现在我们大概知道了二维码是怎么生成的,有关 Android 上扫码的库有很多,这次来介绍的是 ZXing,一个出色的开源扫码库。 |
| 26 | + |
| 27 | +来看看它在 github 上的仓库: |
| 28 | + |
| 29 | +[GitHub ZXing](https://github.com/zxing/zxing) |
| 30 | + |
| 31 | +上面 ReadMe 文件传递的信息大概就是 ZXing 是个很厉害的库,支持各种平台等等...然后又找了它一些相关的连接,我发现有关 Android 的信息少之又少,仅仅说了怎么引入,具体使用上没说,并且源码中给出的 Android Module 代码量有点多,掌握的成本太高,总之当时我觉得这个学习的姿势不太对。 |
| 32 | + |
| 33 | +回到 google 重新搜索了一下 Android ZXing,终于找到了一个正确的仓库来引入并使用它: |
| 34 | + |
| 35 | +[zxing-android-embedded](https://github.com/journeyapps/zxing-android-embedded) |
| 36 | + |
| 37 | +这个库是一个基于 ZXing 的 Android 二维码解码库,使用起来还是非常简单的。 |
| 38 | + |
| 39 | +我把它 fork 回之后,做了一些优化和更新,具体请查看下文。 |
| 40 | + |
| 41 | +[中文版 ZXing Android Embedded](https://github.com/itsMelo/zxing-android-embedded) |
| 42 | + |
| 43 | +快速使用: |
| 44 | + |
| 45 | +``` |
| 46 | +new IntentIntegrator(this).initiateScan(); // `this` is the current Activity |
| 47 | +
|
| 48 | +
|
| 49 | +// Get the results: |
| 50 | +@Override |
| 51 | +protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 52 | + IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); |
| 53 | + if(result != null) { |
| 54 | + if(result.getContents() == null) { |
| 55 | + Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show(); |
| 56 | + } else { |
| 57 | + Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show(); |
| 58 | + } |
| 59 | + } else { |
| 60 | + super.onActivityResult(requestCode, resultCode, data); |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +可以看到使用起来非常便捷,`onActivityResult`带回扫码出来的结果,然后进行处理就OK了。下文中就不再介绍 ReadMe 中的内容,转而介绍一下这个库一些其他的配置。 |
| 66 | + |
| 67 | +``` |
| 68 | + protected Class<?> getDefaultCaptureActivity() { |
| 69 | + return CaptureActivity.class; |
| 70 | + } |
| 71 | +``` |
| 72 | +一步步跟进 `IntentIntegrator(this).initiateScan()` 方法可以发现,当我不设置 CaptureActivity 时,调用默认的 Activity 就是 `CaptureActivity` |
| 73 | + |
| 74 | +所以对于调起的扫码 Activity 来说,他的方向可以在 manifest 文件中的 `android:screenOrientation` 属性直接指定: |
| 75 | + |
| 76 | +``` |
| 77 | + <activity |
| 78 | + android:name="com.journeyapps.barcodescanner.CaptureActivity" |
| 79 | + android:screenOrientation="landscape" |
| 80 | + tools:replace="screenOrientation" /> |
| 81 | +``` |
| 82 | +除了最基本的扫码使用,再来看看他 `IntentIntegrator` 中其它的 setXXX 属性吧。 |
| 83 | + |
| 84 | + - integrator.setPrompt 在扫描页面添加一个文字描述,空字符串时,可以取消显示它。 |
| 85 | + |
| 86 | + - integrator.setOrientationLocked 设置是否锁定扫码 Activity 的方向。默认为 true |
| 87 | + |
| 88 | + - integrator.setCameraId 设置使用摄像头的 id,0 为后置摄像头,1 为前置摄像头。这是系统 CameraInfo 的属性。 |
| 89 | + |
| 90 | +``` |
| 91 | + /** |
| 92 | + * The facing of the camera is opposite to that of the screen. |
| 93 | + */ |
| 94 | + public static final int CAMERA_FACING_BACK = 0; |
| 95 | +
|
| 96 | + /** |
| 97 | + * The facing of the camera is the same as that of the screen. |
| 98 | + */ |
| 99 | + public static final int CAMERA_FACING_FRONT = 1; |
| 100 | +``` |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + - integrator.setBeepEnabled 设置扫描完成时是否允许“嘟嘟”的声音。默认为 true。源码中的这个设置位于 BeepManager,通过 MediaPlayer 播放了一段 .ogg 文件。 |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + - integrator.setBarcodeImageEnabled 保存扫描完成后二维码的图像。源码在这里: |
| 109 | + |
| 110 | +``` |
| 111 | + /** |
| 112 | + * Save the barcode image to a temporary file stored in the application's cache, and return its path. |
| 113 | + * Only does so if returnBarcodeImagePath is enabled. |
| 114 | + * |
| 115 | + * @param rawResult the BarcodeResult, must not be null |
| 116 | + * @return the path or null |
| 117 | + */ |
| 118 | + private String getBarcodeImagePath(BarcodeResult rawResult) { |
| 119 | + String barcodeImagePath = null; |
| 120 | + if (returnBarcodeImagePath) { |
| 121 | + Bitmap bmp = rawResult.getBitmap(); |
| 122 | + try { |
| 123 | + File bitmapFile = File.createTempFile("barcodeimage", ".jpg", activity.getCacheDir()); |
| 124 | + FileOutputStream outputStream = new FileOutputStream(bitmapFile); |
| 125 | + bmp.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); |
| 126 | + outputStream.close(); |
| 127 | + barcodeImagePath = bitmapFile.getAbsolutePath(); |
| 128 | + } catch (IOException e) { |
| 129 | + Log.w(TAG, "Unable to create temporary file and store bitmap! " + e); |
| 130 | + } |
| 131 | + } |
| 132 | + return barcodeImagePath; |
| 133 | + } |
| 134 | +``` |
| 135 | + - integrator.setDesiredBarcodeFormats 设置扫描的二维码格式 |
| 136 | + |
| 137 | + - integrator.setTimeout 设置一个超时时间,超过这个时间之后,扫描的 Activity 将会被 finish 。 |
| 138 | + |
| 139 | +自定义 |
| 140 | +-- |
| 141 | + |
| 142 | +通过昨天半天的时间,算是把这个库看明白了,写得很好值得推荐,有心的朋友可以好好看看源码,有的可学。 |
| 143 | + |
| 144 | +项目中的需求各有不同,所以在 UI 上可定制就成了很重要的一点,这个库虽然没开放出很多方法,但是自定义起来依然自由方便,一起来看看姿势吧。 |
| 145 | + |
| 146 | + - integrator.setCaptureActivity 我们可以通过这个方法,指定要传入的自定义的扫描 Activity,那这个 Activity 应该怎么去定制呢? |
| 147 | + |
| 148 | +``` |
| 149 | + <com.journeyapps.barcodescanner.DecoratedBarcodeView |
| 150 | + android:id="@+id/zxing_barcode_scanner" |
| 151 | + android:layout_width="match_parent" |
| 152 | + android:layout_height="match_parent" |
| 153 | + app:zxing_scanner_layout="@layout/custom_barcode_scanner"> |
| 154 | + </com.journeyapps.barcodescanner.DecoratedBarcodeView> |
| 155 | +``` |
| 156 | + |
| 157 | +`DecoratedBarcodeView` 是扫描 View 的一个封装类,具体的 UI 属性,在 `app:zxing_scanner_layout="@layout/custom_barcode_scanner"` 引入的 `custom_barcode_scanner` 布局文件中进行定制修改。来看看这个布局: |
| 158 | + |
| 159 | +``` |
| 160 | +<merge xmlns:android="http://schemas.android.com/apk/res/android" |
| 161 | + xmlns:tools="http://schemas.android.com/tools" |
| 162 | + xmlns:app="http://schemas.android.com/apk/res-auto"> |
| 163 | +
|
| 164 | + <com.journeyapps.barcodescanner.BarcodeView |
| 165 | + android:layout_width="match_parent" |
| 166 | + android:layout_height="match_parent" |
| 167 | + android:id="@+id/zxing_barcode_surface" |
| 168 | + app:zxing_framing_rect_width="250dp" |
| 169 | + app:zxing_framing_rect_height="50dp"/> |
| 170 | +
|
| 171 | + <com.journeyapps.barcodescanner.ViewfinderView |
| 172 | + android:layout_width="match_parent" |
| 173 | + android:layout_height="match_parent" |
| 174 | + android:id="@+id/zxing_viewfinder_view" |
| 175 | + app:zxing_possible_result_points="@color/zxing_custom_possible_result_points" |
| 176 | + app:zxing_result_view="@color/zxing_custom_result_view" |
| 177 | + app:zxing_viewfinder_laser="@color/zxing_custom_viewfinder_laser" |
| 178 | + app:zxing_viewfinder_mask="@color/zxing_custom_viewfinder_mask"/> |
| 179 | +
|
| 180 | + <TextView |
| 181 | + android:id="@+id/zxing_status_view" |
| 182 | + android:layout_width="wrap_content" |
| 183 | + android:layout_height="wrap_content" |
| 184 | + android:layout_gravity="bottom|center_horizontal" |
| 185 | + android:background="@color/zxing_transparent" |
| 186 | + android:text="@string/zxing_msg_default_status" |
| 187 | + android:textColor="@color/zxing_status_text"/> |
| 188 | +
|
| 189 | +</merge> |
| 190 | +``` |
| 191 | + |
| 192 | + |
| 193 | +这个图很直观了吧,要是定制扫描线的颜色啊,或者扫描框的大小啊,都没有问题了~ |
| 194 | + |
| 195 | +当然我把它 fork 回来之后,做了一些改动,比如修了两个 bug,然后开放了两个我认为比较合适的新的设置项: |
| 196 | + |
| 197 | + - setBeepResource 让扫描之后发出的嘟嘟声可定制,需要传入本地的一个 raw 文件。 |
| 198 | + |
| 199 | + - setVibrateEnable 设置扫描完成之后,是否让手机震动。 |
| 200 | + |
| 201 | +我做了修改之后的仓库连接: |
| 202 | + |
| 203 | +[ZXing Android Embedded](https://github.com/itsMelo/zxing-android-embedded) |
| 204 | + |
| 205 | +一直会持续更新它,需要的朋友保持关注吧~ |
0 commit comments