原文:http://blog.csdn.net/qq_30379689/article/details/52411489
新手剛開搞android一周,各種折騰了好久終於搞定了這個二維碼掃描,太蛋疼了。
作為Google開源框架Zxing,里面的文件很大,這里主要講的是精簡ZXing項目后只保留掃描功能的代碼,可以縮小項目的大小,對於只要掃描功能的項目已經夠用了。掃描后的結果,只要通過WebView百度一下就出來了。簡單的說,可以把Zxing這個二維碼掃描功能當做一個第三方服務來使用,本篇文章分為兩部分,Zxing的集成和Zxing的使用
一:下載我們所需要的Zxing精簡版,在Github上搜索Zxing,看到這條記錄,然后下載。
百度網盤下載:鏈接:http://pan.baidu.com/s/1c12ybh2 密碼:bpix



二:復制到項目中,在ZXingProj/src/com/dtr目錄下,復制這個zxing文件夾到我們的項目中

編譯一下就會發現一堆錯誤,下面我們就去改錯誤。

三、復制zxing.jar包到libs



四、復制下載的res的layout文件、res的values的ids文件、raw文件夾、res的drawable-xhdpi文件夾到我們項目的對應位置




五、重新導入文件,記得刪除原先的R包,換成自己項目的R包

修改:

全部修改完重新編譯無錯誤

六.聲明權限和Activity


<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sdtimoniyor.ticketsystem"> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.FLASHLIGHT" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".Home"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Moitor.MonitorHome"></activity> <activity android:name=".zxing.activity.CaptureActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.Black.NoTitleBar" /> <activity android:name=".zxing.activity.ResultActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.Black.NoTitleBar" /> </application> </manifest>
七、啟動
Button qrcode=(Button)findViewById(R.id.qr_code); qrcode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MonitorHome.this, CaptureActivity.class); startActivity(intent); } });
八、Test

我擦,這是什么鬼,回去一看權限也加了啊
九、修改,添加運行時權限
Button qrcode = (Button) findViewById(R.id.qr_code); qrcode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (ContextCompat.checkSelfPermission(MonitorHome.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(MonitorHome.this, new String[]{Manifest.permission.CAMERA}, 1); } else { Intent intent = new Intent(MonitorHome.this, CaptureActivity.class); startActivity(intent); } } });


十、真機測試



結尾:這圖真尼瑪大。
