Github项目解析(十)-->快速集成二维码扫描库

  

  

  xmlns:app="http://schemas.android.com/apk/res-auto"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent" >

  <surfaceview< p="">

  android:id="@+id/preview_view"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  />

  <com.uuzuche.lib_zxing.view.viewfinderview< p="">

  android:id="@+id/viewfinder_view"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  />

  

  可以发现其主要的区别就是在自定义的扫描控件中多了三个自定义的扫描框属性:inner_width、inner_height和inner_margintop,通过这三个属性可以控制扫描框的相对位置。

  执行效果

物联网

  当然了如果以上的以上,你还是对定制化UI方面不太满意,可以直接下载我的项目,然后引入lib-zxing module作为你的module,直接修改其代码。

  创建二维码库的流程:

  创建一个android studio项目,并创建zxingLibrary库;

物联网

  在zxingLibrary库中实现二维码的扫描操作;

  在实现过程中有以下几个注意点:

  (1)由于二维码扫描界面是一个Activity,所以需要在AndroidManifest.xml中定义,而我为了减少对项目的依赖,选择了在zxingLibrary中的AndroidManifest.xml中定义该Activity。

  

  >

  <activity< p="">

  android:configChanges="orientation|keyboardHidden"

  android:name="com.uuzuche.lib_zxing.activity.CaptureActivity"

  android:screenOrientation="portrait"

  android:windowSoftInputMode="stateAlwaysHidden"

  android:label="扫描二维码"

  android:theme="@style/Theme.AppCompat.NoActionBar"

  >

  

  (2)由于扫描操作需要使用摄像头等权限,也在zxingLibrary中的AndroidManifest.xml中申请了部分权限:

  

  

  

  

  

  

  

  

  (3)原来的项目中二维码扫面的部分是作为主项目实现的,部分代码使用了switch(id)的操作,而这样的代码在library中有问题,具体可参考: 在Android library中不能使用switch-case语句访问资源ID的原因分析及解决方案 ,所以我做了修改:

  修改之前:

[email protected]

  public void handleMessage(Message message) {

  switch (message.what) {

  case R.id.decode:

  decode((byte[]) message.obj, message.arg1, message.arg2);

  break;

  case R.id.quit:

  Looper.myLooper().quit();

  break;

  }

  }

  修改之后:

[email protected]

  public void handleMessage(Message message) {

  if (message.what == R.id.decode) {

  decode((byte[]) message.obj, message.arg1, message.arg2);

  } else if (message.what == R.id.quit) {

  Looper.myLooper().quit();

  }

  }

  将zxingLibrary库上传至Jcenter中

 

  为了使用compile,所以讲zxingLibrary库上传至了jCenter中,具体的上传过程可参考我的: Github项目解析(二)–>将Android项目发布至JCenter代码库