在Android用ZXing.jar识别二维码的精简版(简化了配置和代码)

时间:2014-05-15 03:12:38   收藏:0   阅读:521

        最近公司做了一款OTP令牌激活的产品,由于之前激活手机令牌需要输入很多的激活信息才能进行激活。经过一段使用后,发现易用性不是很强,考虑如果加入二维码的的扫码功能岂不是大大增加了易用性。     在网上搜了很多资料,要不是配置太多,要不就是代码量太大,最后没办法就硬着头皮去写和去精简,最后压缩至目前的版本。如果有更好的版本可以联系我。  

     

        具体DEMO可以通过 下载http://download.csdn.net/detail/fugui6611634/7341277下载

        本文代码运行的结果如下:

       bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣

 

      代码的结构如下:

bubuko.com,布布扣

 

        下面主要来看一下配置文件:

        

<?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识别二维码的精简版(简化了配置和代码),布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!