自己寫的關於zxing二維碼掃描的例子

package com.qrcode;


import com.google.zxing.WriterException;
import com.zxing.activity.CaptureActivity;
import com.zxing.encoding.EncodingHandler;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private TextView resultTextView;
	private EditText qrStrEditText;
	private ImageView qrImgImageView;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        //返回的值
        resultTextView = (TextView) this.findViewById(R.id.tv_scan_result);
      
        //輸入值,用來生成二維碼圖片
        qrStrEditText = (EditText) this.findViewById(R.id.et_qr_string);
      
        //展示生成的二維碼圖片
        qrImgImageView = (ImageView) this.findViewById(R.id.iv_qr_image);
      
        //點擊會打開照相機,掃描二維碼
        Button scanBarCodeButton = (Button) this.findViewById(R.id.btn_scan_barcode);
        scanBarCodeButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
					//CaptureActivity是主要的一個類,用來處理掃描結果
				Intent openCameraIntent = new Intent(MainActivity.this,CaptureActivity.class);
				//掃描結果會返回,所以用這個方法
				startActivityForResult(openCameraIntent, 0);
			}
		});
        
        //點擊會產生一個新的二維碼
        Button generateQRCodeButton = (Button) this.findViewById(R.id.btn_add_qrcode);
        generateQRCodeButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				try {
					String contentString = qrStrEditText.getText().toString();
					if (!contentString.equals("")) {
						//這句子會把得到的text文字,變成二維碼
						Bitmap qrCodeBitmap = EncodingHandler.createQRCode(contentString, 350);
					   //展示圖片
						qrImgImageView.setImageBitmap(qrCodeBitmap);
					}else {
						Toast.makeText(MainActivity.this, "Text can not be empty", Toast.LENGTH_SHORT).show();
					}
					
				} catch (WriterException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
    }

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	
		if (resultCode == RESULT_OK) {
			Bundle bundle = data.getExtras();
			String scanResult = bundle.getString("result");
			resultTextView.setText(scanResult);
		}
	}
}

最重要的是使用了裏面的CaptureActivity的類,同時xml佈局如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_scan_barcode"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="Open camera" />
    
    <LinearLayout 
        android:orientation="horizontal"
        android:layout_marginTop="10dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        
        <TextView 
       	android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:text="Scan result:" />
        
        <TextView 
        android:id="@+id/tv_scan_result"
       	android:layout_width="fill_parent"
       	android:textSize="18sp"
       	android:textColor="@android:color/black"
        android:layout_height="wrap_content" />
    </LinearLayout>
    
    <EditText 
        android:id="@+id/et_qr_string"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:hint="Input the text"/>
    
    <Button
        android:id="@+id/btn_add_qrcode"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Generate QRcode" />
    
    <ImageView 
        android:id="@+id/iv_qr_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_gravity="center"/>

</LinearLayout>


源代碼下載


--------------分割線-------------------

11月27日,今天又查看了一個二維碼掃描的例子,參考的文章如下

http://blog.csdn.net/xiaanming/article/details/14450809

這裏的例子,最大的特點就是模仿了微信掃描的外觀,而且可以直接掃描手機裏圖片的二維碼,同時,掃描的二維碼圖片可以展示出來,但是沒有生成二維碼的功能。



關鍵的代碼如下

  //處理返回的掃描結果數據
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
		case SCANNIN_GREQUEST_CODE:
			if(resultCode == RESULT_OK){
				Bundle bundle = data.getExtras();
				//顯示掃描到的內容,注意下這裏是從bundle裏面拿到文本
				mTextView.setText(bundle.getString("result"));
				//顯示圖片,注意下這裏是從intent裏面拿到圖片
				mImageView.setImageBitmap((Bitmap) data.getParcelableExtra("bitmap"));
			}
			break;
		}
    }	

源代碼下載



-----------------------------分割線-----------------------------------

關於二維碼,還有一個叫zBar的,這個特點是識別率高,速度快,但是外觀是黃色的,同時這個項目比較精簡,沒有zxing那麼多的文件。

文章裏的項目,掃描的外觀不是很美觀,而且掃描後沒有振動和聲音提示功能,我自己添加了上去

參考文章http://blog.csdn.net/chillax_li/article/details/39693327



源碼下載













發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章