读书人

android利用ZXing开展条码扫描二维码扫

发布时间: 2012-07-02 17:46:22 作者: rapoo

android利用ZXing进行条码扫描二维码扫描源码简化
导入项目

此时编译一下项目,会发现报错,“?Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute?”之类的。打开raw 下的Values 发现错误是在一个<String>上。这里把 “preferences_custom_product_search_summary” 里的 ?%s ?%f ?全部都改成 ?%1$s ?%1$f(因为我们用不到多国语言,建议只保留默认的Value ,其他全部删除)。

  原因:由于新的SDK采用了新版本的aapt(Android项目编译器),这个版本的aapt编译起来会比老版本更加的严格,然后在Android最新的开发文档的描述String的部分,已经说明如何去设置 %s 等符号

  “If you need to format your strings using String.format(String, Object...) , then you can do so by putting your format arguments in the string resource. For example, with the following resource:

  <string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>

  In this example, the format string has two arguments: %1$s is a string and %2$d is a decimal number. You can format the string with arguements from your application...“

  经过以上步骤后项目应该就可以运行了。

  但是ZXing的android项目东西太多了,有很多是我们不需要的,得新建另一个项目简化它。

简化

  在开始前大致介绍一下简化ZXing需要用到各个包 、类的职责。

CaptureActivity。这个是启动Activity 也就是扫描器(如果是第一安装,它还会跳转到帮助界面)。CaptureActivityHandler 解码处理类,负责调用另外的线程进行解码。DecodeThread 解码的线程。com.google.zxing.client.android.camera 包,摄像头控制包。ViewfinderView 自定义的View,就是我们看见的拍摄时中间的框框了。新建另一个项目

  新建另一个项目将启动的Activity命名为CaptureActivity,并导入核心库。项目新建完成后我们打开 CaptureActivity 的布局文件,我这里为main。把里面的XML修改为:

?

  在修改的过程中,有很多是关于R 资源的问题,在此我们需要将Values ?里面的两个xml资源文件拷入项目中:colos.xml 和ids.xml 。 ctrl+b 一下看看error 是不是少了很多。在CameraManager中有些地方需要用到项目的配置,这里需要把配置直接写入代码中:

??? <uses-feature android:name="android.hardware.camera.autofocus" />
????? <uses-permission android:name="android.permission.VIBRATE"/>
? <uses-permission android:name="android.permission.FLASHLIGHT"/>

?

当View 和 camera 包里的错误修正完成后,我们继续来看CaptureActivity。

覆盖onResume方法初始化摄像头:

?

 简化后的ZXing 更加方便我们了解ZXing项目 是如何解码的。只要仔细查看源码,进行单点跟踪调试,相信大家很容易能理解。

读书人网 >Android

热点推荐