轉載請注明出處: http://blog.csdn.net/guolin_blog/article/details/9526247
最近二維碼真是越來越火了,隨便電視上、網絡上、商場里,到處都是二維碼。而內嵌二維碼掃描功能的軟件也越來越多,QQ、微信、UC瀏覽器等等應用都可以對著二維碼掃一掃,感覺我們自己的應用里不加上二維碼掃描功能,都跟不上時代潮流了。所以今天我就將帶著大家一起,在我們自己的程序里加入二維碼掃描的功能。
不過,二維碼功能如果真要做起來還是非常復雜的,從零開始實現不太現實,比較好的做法就是借助現有的開源項目。目前在二維碼這一領域名氣最大的開源項目就是ZXing了(Zebra Crossing),它提供了多個平臺的二維碼掃描解決方案,開源項目地址是 https://code.google.com/p/zxing/ 。
雖說網上已經有現成的開源項目了,不過關于ZXing的文檔和教程好像還比較少,因此還是有不少朋友并不知道在項目中該如何引入ZXing的,這里我就帶著大家一步步地實現,相信每個人在看完本篇文章后都可以在自己的項目中實現二維碼掃描功能。
首先,我們需要下載ZXing項目所依賴的Jar包的源碼。
下載地址是 http://repo1.maven.org/maven2/com/google/zxing/core/2.2/core-2.2-sources.jar 。
然后我們再來下載ZXing項目,下載地址是 https://zxing.googlecode.com/files/ZXing-2.2.zip 。
建議使用迅雷下載,因為Google Code和Maven的訪問在國內不穩(wěn)定,經常出現斷聯的情況,使用迅雷可以保證文件的完整性。
另外,經過我的測試,在ZXing項目中直接導入core-2.2的Jar包是無法正常運行的,所以我們只能通過將core-2.2的源碼加入到ZXing項目中來實現。下載好以上兩個文件后,先解壓core-2.2-sources.jar文件,解壓之后的目錄結構如下圖所示:
然后解壓ZXing-2.2這個壓縮包,里面可以看到各種平臺下的ZXing項目源碼,我們進入到android文件夾的src目錄下,將core-2.2-sources中的源碼拷貝進來??截愔骯ndroid文件夾下的目錄結構如下圖所示:
這樣準備工作已經完成了,現在我們新建一個Android項目ScannerTest,項目使用Android 4.0的API。
然后將上圖中src目錄下的所有文件全部復制,粘貼到我們ScannerTest項目的src目錄下,完成后目錄結構如下圖所示:
拷貝完了代碼,現在該拷貝資源了,展開ZXing項目android文件夾下的res目錄,將drawable文件夾、layout文件夾、menu文件夾、raw文件夾、values文件夾以及xml文件夾中的內容都拷貝到ScannerTest項目的res目錄下,注意有沖突的部分要小心解決,比如兩個values文件夾中都有string.xml文件,要將它們的內容進行合并,不能只是簡單地覆蓋。
然后我們還需要將AndroidManifest中的內容進行合并,注意ZXing Android項目下的AndroidManifest在聲明Activity時用的都是簡寫,而現在由于項目包名變了,再使用簡寫會出現找不到活動的情況,因此所有的簡寫都要改成完整類名,例如.CaptureActivity要改成com.google.zxing.client.android.CaptureActivity。另外ZXing Android項目下的主活動是CaptureActivity,這里我們需要將主活動的聲明刪除掉,因為ScannerTest項目中主活動是MainActivity。合并后的AndroidManifest中的代碼如下所示:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.scannertest" android:versionCode="1" android:versionName="1.0" > <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.FLASHLIGHT" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" /> <uses-feature android:name="android.hardware.camera" android:required="false" /> <uses-feature android:name="android.hardware.camera.front" android:required="false" /> <uses-feature android:name="android.hardware.camera.autofocus" android:required="false" /> <uses-feature android:name="android.hardware.camera.flash" android:required="false" /> <uses-feature android:name="android.hardware.screen.landscape" /> <uses-feature android:name="android.hardware.wifi" android:required="false" /> <uses-feature android:name="android.hardware.touchscreen" /> <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:xlargeScreens="true" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.scannertest.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.google.zxing.client.android.CaptureActivity" android:clearTaskOnLaunch="true" android:configChanges="orientation|keyboardHidden" android:screenOrientation="landscape" android:stateNotNeeded="true" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:windowSoftInputMode="stateAlwaysHidden" > <intent-filter> <action android:name="com.google.zxing.client.android.SCAN" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:host="zxing.appspot.com" android:path="/scan" android:scheme="http" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:host="www.google.com" android:path="/m/products/scan" android:scheme="http" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:host="www.google.co.uk" android:path="/m/products/scan" android:scheme="http" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:host="scan" android:path="/" android:scheme="zxing" /> </intent-filter> </activity> <activity android:name="com.google.zxing.client.android.PreferencesActivity" android:label="@string/preferences_name" android:stateNotNeeded="true" > </activity> <activity android:name="com.google.zxing.client.android.encode.EncodeActivity" android:stateNotNeeded="true" > <intent-filter> <action android:name="com.google.zxing.client.android.ENCODE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/x-vcard" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/plain" /> </intent-filter> </activity> <activity android:name="com.google.zxing.client.android.book.SearchBookContentsActivity" android:configChanges="orientation|keyboardHidden" android:label="@string/sbc_name" android:screenOrientation="landscape" android:stateNotNeeded="true" > <intent-filter> <action android:name="com.google.zxing.client.android.SEARCH_BOOK_CONTENTS" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name="com.google.zxing.client.android.share.ShareActivity" android:screenOrientation="user" android:stateNotNeeded="true" android:theme="@android:style/Theme.Light" > <intent-filter> <action android:name="com.google.zxing.client.android.SHARE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name="com.google.zxing.client.android.history.HistoryActivity" android:label="@string/history_title" android:stateNotNeeded="true" > <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name="com.google.zxing.client.android.share.BookmarkPickerActivity" android:label="@string/bookmark_picker_name" android:stateNotNeeded="true" > <intent-filter> <action android:name="android.intent.action.PICK" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name="com.google.zxing.client.android.share.AppPickerActivity" android:configChanges="orientation" android:label="@string/app_picker_name" android:stateNotNeeded="true" > <intent-filter> <action android:name="android.intent.action.PICK" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name="com.google.zxing.client.android.HelpActivity" android:screenOrientation="user" > <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> </manifest>完成到這一步之后,你會發(fā)現項目中還是有很多的錯誤。不用擔心,剩下的錯誤全部都是由于找不到R文件所造成的。這是因為ZXing項目中所引用的R文件都是com.google.zxing.client.android包下的R,而現在我們拷貝到ScannerTest項目之后,應該引用com.example.scannertest包下的R文件。我們需要將有錯誤的文件一個個地修改過來,雖然工作量不少,但都是傻瓜式操作,只要大家有耐心,就一定可以完成。
現在ScannerTest項目中應該已經沒有任何錯誤了,然后我們還需要對ZXing的代碼進行稍微的定制。
打開CaptureActivity,這個類就是用于掃描二維碼的最主要的一個類,其中有一個handleDecode()方法,當二維碼掃描完成之后會把結果回調到這個方法中,我們現在不想使用默認的處理方式,于是修改handleDecode()中的代碼,如下所示:
public void handleDecode(Result rawResult, Bitmap barcode, float scaleFactor) { String result = rawResult.getText(); if (!TextUtils.isEmpty(result)) { Intent intent = new Intent(); intent.putExtra("scan_result", rawResult.getText()); setResult(RESULT_OK, intent); } else { setResult(RESULT_CANCELED); } finish(); }這里我們將掃描出來的結果借助Intent進行返回。
然后打開或新建activity_main.xml文件做為ScannerTest項目的主布局,在其中添加如下代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/scan_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="掃一掃" /> <TextView android:id="@+id/scan_result" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>這個布局文件很簡單,一個按鈕用于開啟二維碼掃描功能,一個TextView用于顯示掃描結果。
最后打開或新建MainActivity做為ScannerTest項目的主Activity,代碼如下所示:
public class MainActivity extends Activity { public static final int SCAN_CODE = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.scan_button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, CaptureActivity.class); startActivityForResult(intent, SCAN_CODE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case SCAN_CODE: TextView scanResult = (TextView) findViewById(R.id.scan_result); if (resultCode == RESULT_OK) { String result = data.getStringExtra("scan_result"); scanResult.setText(result); } else if (resultCode == RESULT_CANCELED) { scanResult.setText("掃描出錯"); } break; default: break; } } }這個類也很簡單,點擊按鈕時,我們通過startActivityForResult()方法啟動CaptureActivity,開始執(zhí)行二維碼掃描,掃描的結果將回調到onActivityResult()方法中,然后在這個方法中取出掃描的結果,并展示在TextView上。
這樣我們所有的編碼工作就已經完成了,可以嘗試運行一下了。首先看到程序的主界面如下圖所示:
點擊掃一掃后可以進行二維碼掃描,見下圖:
掃描完成后會將結果返回到主界面,如下圖所示:
不知道大家有沒有成功呢?這里我精心給大家準備了一張二維碼圖片,看看有多少朋友能夠成功掃出來。 ^_^
另外,ZXing項目是比較龐大的,里面還有很多復雜的功能我們并不需要,如果你有興趣深度鉆研ZXing源碼的話,其實還可以簡化非常多的代碼。 這里我就不帶著大家深入研究了,因為我自己都還沒完全搞明白呢
好了,今天的講解到此結束,有疑問的朋友請在下面留言。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
