自己写的关于zxing二维码扫描的例子

来源:互联网 发布:mac卸载qq 编辑:程序博客网 时间:2024/06/05 06:27
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() {@Overridepublic 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 blocke.printStackTrace();}}});    }@Overrideprotected 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



源码下载













0 0
原创粉丝点击