欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Android二維碼功能實(shí)現(xiàn),在程序內(nèi)嵌入ZXing項(xiàng)目

系統(tǒng) 2272 0

轉(zhuǎn)載請注明出處: http://blog.csdn.net/guolin_blog/article/details/9526247

最近二維碼真是越來越火了,隨便電視上、網(wǎng)絡(luò)上、商場里,到處都是二維碼。而內(nèi)嵌二維碼掃描功能的軟件也越來越多,QQ、微信、UC瀏覽器等等應(yīng)用都可以對(duì)著二維碼掃一掃,感覺我們自己的應(yīng)用里不加上二維碼掃描功能,都跟不上時(shí)代潮流了。所以今天我就將帶著大家一起,在我們自己的程序里加入二維碼掃描的功能。

不過,二維碼功能如果真要做起來還是非常復(fù)雜的,從零開始實(shí)現(xiàn)不太現(xiàn)實(shí),比較好的做法就是借助現(xiàn)有的開源項(xiàng)目。目前在二維碼這一領(lǐng)域名氣最大的開源項(xiàng)目就是ZXing了(Zebra Crossing),它提供了多個(gè)平臺(tái)的二維碼掃描解決方案,開源項(xiàng)目地址是 https://code.google.com/p/zxing/

雖說網(wǎng)上已經(jīng)有現(xiàn)成的開源項(xiàng)目了,不過關(guān)于ZXing的文檔和教程好像還比較少,因此還是有不少朋友并不知道在項(xiàng)目中該如何引入ZXing的,這里我就帶著大家一步步地實(shí)現(xiàn),相信每個(gè)人在看完本篇文章后都可以在自己的項(xiàng)目中實(shí)現(xiàn)二維碼掃描功能。

首先,我們需要下載ZXing項(xiàng)目所依賴的Jar包的源碼。

下載地址是 http://repo1.maven.org/maven2/com/google/zxing/core/2.2/core-2.2-sources.jar

然后我們再來下載ZXing項(xiàng)目,下載地址是 https://zxing.googlecode.com/files/ZXing-2.2.zip

建議使用迅雷下載,因?yàn)镚oogle Code和Maven的訪問在國內(nèi)不穩(wěn)定,經(jīng)常出現(xiàn)斷聯(lián)的情況,使用迅雷可以保證文件的完整性。

另外,經(jīng)過我的測試,在ZXing項(xiàng)目中直接導(dǎo)入core-2.2的Jar包是無法正常運(yùn)行的,所以我們只能通過將core-2.2的源碼加入到ZXing項(xiàng)目中來實(shí)現(xiàn)。下載好以上兩個(gè)文件后,先解壓core-2.2-sources.jar文件,解壓之后的目錄結(jié)構(gòu)如下圖所示:

Android二維碼功能實(shí)現(xiàn),在程序內(nèi)嵌入ZXing項(xiàng)目

然后解壓ZXing-2.2這個(gè)壓縮包,里面可以看到各種平臺(tái)下的ZXing項(xiàng)目源碼,我們進(jìn)入到android文件夾的src目錄下,將core-2.2-sources中的源碼拷貝進(jìn)來。拷貝之后android文件夾下的目錄結(jié)構(gòu)如下圖所示:

Android二維碼功能實(shí)現(xiàn),在程序內(nèi)嵌入ZXing項(xiàng)目

這樣準(zhǔn)備工作已經(jīng)完成了,現(xiàn)在我們新建一個(gè)Android項(xiàng)目ScannerTest,項(xiàng)目使用Android 4.0的API。

然后將上圖中src目錄下的所有文件全部復(fù)制,粘貼到我們ScannerTest項(xiàng)目的src目錄下,完成后目錄結(jié)構(gòu)如下圖所示:

Android二維碼功能實(shí)現(xiàn),在程序內(nèi)嵌入ZXing項(xiàng)目

拷貝完了代碼,現(xiàn)在該拷貝資源了,展開ZXing項(xiàng)目android文件夾下的res目錄,將drawable文件夾、layout文件夾、menu文件夾、raw文件夾、values文件夾以及xml文件夾中的內(nèi)容都拷貝到ScannerTest項(xiàng)目的res目錄下,注意有沖突的部分要小心解決,比如兩個(gè)values文件夾中都有string.xml文件,要將它們的內(nèi)容進(jìn)行合并,不能只是簡單地覆蓋。

然后我們還需要將AndroidManifest中的內(nèi)容進(jìn)行合并,注意ZXing Android項(xiàng)目下的AndroidManifest在聲明Activity時(shí)用的都是簡寫,而現(xiàn)在由于項(xiàng)目包名變了,再使用簡寫會(huì)出現(xiàn)找不到活動(dòng)的情況,因此所有的簡寫都要改成完整類名,例如.CaptureActivity要改成com.google.zxing.client.android.CaptureActivity。另外ZXing Android項(xiàng)目下的主活動(dòng)是CaptureActivity,這里我們需要將主活動(dòng)的聲明刪除掉,因?yàn)镾cannerTest項(xiàng)目中主活動(dòng)是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>
  
完成到這一步之后,你會(huì)發(fā)現(xiàn)項(xiàng)目中還是有很多的錯(cuò)誤。不用擔(dān)心,剩下的錯(cuò)誤全部都是由于找不到R文件所造成的。這是因?yàn)閆Xing項(xiàng)目中所引用的R文件都是com.google.zxing.client.android包下的R,而現(xiàn)在我們拷貝到ScannerTest項(xiàng)目之后,應(yīng)該引用com.example.scannertest包下的R文件。我們需要將有錯(cuò)誤的文件一個(gè)個(gè)地修改過來,雖然工作量不少,但都是傻瓜式操作,只要大家有耐心,就一定可以完成。

現(xiàn)在ScannerTest項(xiàng)目中應(yīng)該已經(jīng)沒有任何錯(cuò)誤了,然后我們還需要對(duì)ZXing的代碼進(jìn)行稍微的定制。

打開CaptureActivity,這個(gè)類就是用于掃描二維碼的最主要的一個(gè)類,其中有一個(gè)handleDecode()方法,當(dāng)二維碼掃描完成之后會(huì)把結(jié)果回調(diào)到這個(gè)方法中,我們現(xiàn)在不想使用默認(rèn)的處理方式,于是修改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();
}
  
這里我們將掃描出來的結(jié)果借助Intent進(jìn)行返回。

然后打開或新建activity_main.xml文件做為ScannerTest項(xiàng)目的主布局,在其中添加如下代碼:

    <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>
  
這個(gè)布局文件很簡單,一個(gè)按鈕用于開啟二維碼掃描功能,一個(gè)TextView用于顯示掃描結(jié)果。

最后打開或新建MainActivity做為ScannerTest項(xiàng)目的主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("掃描出錯(cuò)");
			}
			break;
		default:
			break;
		}
	}

}
  
這個(gè)類也很簡單,點(diǎn)擊按鈕時(shí),我們通過startActivityForResult()方法啟動(dòng)CaptureActivity,開始執(zhí)行二維碼掃描,掃描的結(jié)果將回調(diào)到onActivityResult()方法中,然后在這個(gè)方法中取出掃描的結(jié)果,并展示在TextView上。

這樣我們所有的編碼工作就已經(jīng)完成了,可以嘗試運(yùn)行一下了。首先看到程序的主界面如下圖所示:

Android二維碼功能實(shí)現(xiàn),在程序內(nèi)嵌入ZXing項(xiàng)目

點(diǎn)擊掃一掃后可以進(jìn)行二維碼掃描,見下圖:

Android二維碼功能實(shí)現(xiàn),在程序內(nèi)嵌入ZXing項(xiàng)目

掃描完成后會(huì)將結(jié)果返回到主界面,如下圖所示:

Android二維碼功能實(shí)現(xiàn),在程序內(nèi)嵌入ZXing項(xiàng)目

不知道大家有沒有成功呢?這里我精心給大家準(zhǔn)備了一張二維碼圖片,看看有多少朋友能夠成功掃出來。 ^_^

Android二維碼功能實(shí)現(xiàn),在程序內(nèi)嵌入ZXing項(xiàng)目

另外,ZXing項(xiàng)目是比較龐大的,里面還有很多復(fù)雜的功能我們并不需要,如果你有興趣深度鉆研ZXing源碼的話,其實(shí)還可以簡化非常多的代碼。 這里我就不帶著大家深入研究了,因?yàn)槲易约憾歼€沒完全搞明白呢

好了,今天的講解到此結(jié)束,有疑問的朋友請?jiān)谙旅媪粞浴?

源碼下載,請點(diǎn)擊這里

Android二維碼功能實(shí)現(xiàn),在程序內(nèi)嵌入ZXing項(xiàng)目


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 日韩欧美一区二区三区 | 国产精品人妻一区夜夜爱 | 国产日韩综合 | 天天亚洲 | 欧美a级在线观看 | 亚洲成人精品在线 | 九九热在线视频 | 欧美在线亚洲 | 日韩福利视频在线 | 波多野结衣中文在线播放 | 97国产精品视频人人做人人爱 | 欧美视频在线视频 | 奇米影视亚洲春色 | 亚洲一区中文字幕在线观看 | 一区二区中文字幕 | 国产精品成人亚洲一区二区 | 日韩色视频 | 精品综合 | 天天舔天天射天天操 | 日韩精品久久久久久 | 国产网址在线 | 波多野结衣中文在线播放 | 一本一本久久α久久精品66 | 私房色播| 精品在线一区二区三区 | 欧美巨尻| 日韩在线免费播放 | 亚州久久 | 狠狠躁躁夜夜躁波多野结依 | 91精品国产免费久久 | 欧美a在线看 | 亚洲婷婷综合中文字幕第一页 | 91精品午夜 | 综合色在线 | 精品美女 | 久久一本日韩精品中文字幕屁孩 | 国产精品成人一区二区三区 | 人人爽久久涩噜噜噜蜜桃 | av在线第一页 | 国产精品综合色区在线观看 | 一级毛片视频免费 |