在Android用ZXing.jar識別二維碼的精簡版(簡化了配置和代碼)
??????? 近期公司做了一款OTP令牌激活的產品,因為之前激活手機令牌須要輸入非常多的激活信息才幹進行激活。
經過一段使用後,發現易用性不是非常強,考慮假設添加二維碼的的掃碼功能豈不是大大添加了易用性。
???? 在網上搜了非常多資料,要不是配置太多,要不就是代碼量太大。最後沒辦法就硬著頭皮去寫和去精簡,最後壓縮至眼下的版本號。
假設有更好的版本號能夠聯系我。??
? ???
??????? 詳細DEMO能夠通過 下載http://download.csdn.net/detail/fugui6611634/7341277下載
??????? 本文代碼執行的結果例如以下:
??????
?
????? 代碼的結構例如以下:
?
??????? 以下主要來看一下配置文件:
????????
<?xml version="1.0" encoding="utf-8"?
> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ft" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".TwoCodeActivity" 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.ui.CaptureActivity" android:configChanges="keyboardHidden|orientation" android:screenOrientation="landscape" android:windowSoftInputMode="adjustResize|stateAlwaysHidden" /> </application> <uses-sdk android:minSdkVersion="3" /> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.DEVICE_POWER" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.FLASHLIGHT" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> </manifest>
?
?
???? 啟動類 TwoCodeActivity.java
???
package com.ft;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.google.zxing.ui.CaptureActivity;
public class TwoCodeActivity extends Activity {
public static final int SCAN_CODE = 1;
AlertDialog.Builder alertDialog = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)this.findViewById(R.id.scan);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(TwoCodeActivity.this, CaptureActivity.class);
startActivityForResult(intent, SCAN_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
TextView textView = (TextView)this.findViewById(R.id.mytext);
switch (requestCode) {
case SCAN_CODE:
if (resultCode == RESULT_OK) {
String result = data.getStringExtra("scan_result");
textView.setText("掃描成功:"+result);
} else if (resultCode == RESULT_CANCELED) {
textView.setText("掃描失敗:");
}
break;
default:
break;
}
}
}
?
在Android用ZXing.jar識別二維碼的精簡版(簡化了配置和代碼)