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"
/>
可以發(fā)現(xiàn)其主要的區(qū)別就是在自定義的掃描控件中多了三個自定義的掃描框?qū)傩裕篿nner_width、inner_height和inner_margintop,通過這三個屬性可以控制掃描框的相對位置。
執(zhí)行效果

當(dāng)然了如果以上的以上,你還是對定制化UI方面不太滿意,可以直接下載我的項目,然后引入lib-zxing module作為你的module,直接修改其代碼。
創(chuàng)建二維碼庫的流程:
創(chuàng)建一個android studio項目,并創(chuàng)建zxingLibrary庫;

在zxingLibrary庫中實現(xiàn)二維碼的掃描操作;
在實現(xiàn)過程中有以下幾個注意點:
(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)由于掃描操作需要使用攝像頭等權(quán)限,也在zxingLibrary中的AndroidManifest.xml中申請了部分權(quán)限:
(3)原來的項目中二維碼掃面的部分是作為主項目實現(xiàn)的,部分代碼使用了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項目發(fā)布至JCenter代碼庫