簡單的二維碼載入預設二維碼掃碼介面 + 騰訊Bugly基本操作
阿新 • • 發佈:2018-12-20
MainActivity
package com.qrcode.qr; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; import com.qrcode.qr.R; import com.uuzuche.lib_zxing.activity.CaptureActivity; import com.uuzuche.lib_zxing.activity.CodeUtils; /** * --------------- 二維碼掃描 -------------------- * 實現步驟 * 1. 在module中新增依賴 * 2.在application中進行初始化(清單檔案要註冊) * 3.在清單檔案中新增依賴 * 4.根據點選事件呼叫掃描二維碼的介面操作 * 5.在動態接收跳轉頁面傳過來的結果,進行吐司 */ public class MainActivity extends AppCompatActivity { private EditText editText; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void ZXing(View view) { Intent intent = new Intent(MainActivity.this, CaptureActivity.class); startActivityForResult(intent, 0); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 0) { //處理掃描結果(在介面上顯示) if (null != data) { Bundle bundle = data.getExtras(); if (bundle == null) { return; } if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) { String result = bundle.getString(CodeUtils.RESULT_STRING); Toast.makeText(this, "解析結果:" + result, Toast.LENGTH_LONG).show(); } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) { Toast.makeText(MainActivity.this, "解析二維碼失敗", Toast.LENGTH_LONG).show(); } } } } }
自定義類MyApplication 繼承 Application
package com.qrcode.qr; import android.app.Application; import com.tencent.bugly.crashreport.CrashReport; import com.uuzuche.lib_zxing.activity.ZXingLibrary; public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); //Zxing ZXingLibrary.initDisplayOpinion(this); //Bugly CrashReport.initCrashReport(getApplicationContext(), "5e578b64f0", true); } }
清單檔案要註冊 MyApplication 在清單檔案中新增許可權
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.qrcode.qr"> <!-- 二維碼掃描所需要的許可權--> <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" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="anBdroid.permission.INTERNET" /> <!-- 騰訊ugly 所需許可權--> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_LOGS" tools:ignore="ProtectedPermissions" /> <application android:name=".MyApplication" 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=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
在Gradle新增依賴
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.qrcode.qr"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '28.0.0'
}
}
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//二維碼 ZXing
implementation 'cn.yipianfengye.android:zxing-library:2.2'
//bugly依賴
implementation 'com.tencent.bugly:crashreport:latest.release'
}