原文:http://blog.csdn.net/qq_30379689/article/details/52411489android
新手剛開搞android一週,各類折騰了很久終於搞定了這個二維碼掃描,太蛋疼了。app
做爲Google開源框架Zxing,裏面的文件很大,這裏主要講的是精簡ZXing項目後只保留掃描功能的代碼,能夠縮小項目的大小,對於只要掃描功能的項目已經夠用了。掃描後的結果,只要經過WebView百度一下就出來了。簡單的說,能夠把Zxing這個二維碼掃描功能當作一個第三方服務來使用,本篇文章分爲兩部分,Zxing的集成和Zxing的使用框架
一:下載咱們所須要的Zxing精簡版,在Github上搜索Zxing,看到這條記錄,而後下載。ide
百度網盤下載:連接:http://pan.baidu.com/s/1c12ybh2 密碼:bpix測試
二:複製到項目中,在ZXingProj/src/com/dtr目錄下,複製這個zxing文件夾到咱們的項目中this
編譯一下就會發現一堆錯誤,下面咱們就去改錯誤。spa
3、複製zxing.jar包到libs.net
4、複製下載的res的layout文件、res的values的ids文件、raw文件夾、res的drawable-xhdpi文件夾到咱們項目的對應位置3d
5、從新導入文件,記得刪除原先的R包,換成本身項目的R包 code
修改:
所有修改完從新編譯無錯誤
六.聲明權限和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>
7、啓動
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); } });
8、Test
我擦,這是什麼鬼,回去一看權限也加了啊
9、修改,添加運行時權限
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); } } });
10、真機測試
結尾:這圖真尼瑪大。