1、首先需要下载Z-Xing 库项目下载文档,可在http://download.csdn.net/detail/catchingsun/8903065进行下载;
2、解析二维码,并跳转至新建Activity,新建Activity放在和CaptureActivity.java同在的文件夹下,在源码的AndroidManifest.xml文件中存在需要获取的权限
3、新建Activity,并在新建Activity中载入CaptureActivity,并对扫描结果进行处理,将扫描得到的文本信息进行显示,如果为URL信息则调用默认浏览器打开
package com.zxing.activity;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.ericssonlabs.R;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.Result;
import com.google.zxing.qrcode.QRCodeReader;
public class AnalyseQRcode extends Activity {
private final static int SCANNIN_GREQUEST_CODE = 1;
static Activity analyseqrcode = null;//定义Activity变量,在主CaptureActivity中进行关闭
private ImageView mResultiv;
private TextView mLabeltv;
private TextView mResulttv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.analyseqrcode);
analyseqrcode = this;
mLabeltv = (TextView) findViewById(R.id.labeltv);
mResulttv = (TextView) findViewById(R.id.resulttv);
mResultiv = (ImageView) findViewById(R.id.resultiv);
Toast.makeText(AnalyseQRcode.this, "请将取景框对准二维码,准备开始扫描", 100).show();
Intent startScan = new Intent(AnalyseQRcode.this, CaptureActivity.class);
startActivityForResult(startScan, 0);//进入二维码解析界面
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bundle bundle = data.getExtras();
String scanResult = bundle.getString("result");
mResulttv.setText(scanResult);
if (resultCode == RESULT_OK && !scanResult.equals("")) {
if(scanResult.contains("http://")||((scanResult.contains(".com"))||(scanResult.contains(".cn")))){//判断是否为URL信息,如果是则打开连接
Intent i = new Intent(Intent.ACTION_VIEW);//打开连接
String url = scanResult;
i.setData(Uri.parse(url));
startActivity(i);
}
}
}
若在其他工程中使用CaptureActivity可直接调用刚新建的Activity,解析结果将会返回给新建的Activity;如果要直接调用CaptureActivity并将结果返回当前Activity,可直接加入以下语句:
Toast.makeText(AnalyseQRcode.this, "请将取景框对准二维码,准备开始扫描", Toast.LENGTH_SHORT).show();
Intent startScan = new Intent(AnalyseQRcode.this, CaptureActivity.class);
startActivityForResult(startScan, 0);//进入二维码解析界面,扫描结果返回当前Activity
4、新建Activity的.xml文件
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff">
<TextView
android:id="@+id/labeltv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="扫描结果:"
android:textSize="18dp"
/>
<TextView
android:id="@+id/resulttv"<!--显示扫描结果的文本信息-->
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
/>
<ImageView
android:id="@+id/resultiv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
注意:在使用多个工程作为Library时,如果出现错误,可能是 多个工程中R.layout文件夹中.xml文件同名,并且不能多个工程的libs文件夹下有同样的.jar包
5、生成二维码
package com.dachuang.daohang.qrcode;
import com.dachuang.daohang.MainActivity;
import com.dachuang.daohang.start.R;
import com.dachuang.daohang.start.SelectLanguage;
import com.google.zxing.WriterException;
import com.zxing.encoding.EncodingHandler;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.http.util.EncodingUtils;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
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 SetQRcode<span> extends Activity {
private ImageView setqrimg;
private EditText setqret;
private Bitmap setqrb;
private Bitmap setqrb1;
private Button setqrbtn;
//private MIDIPlayer mIDIPlayer=null;
private boolean mbMusic=false;
private TextView mTextView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setqrcode);
setqrbtn =(Button)findViewById(R.id.setqrbutton);
setqrbtn.setOnClickListener( new setqrOnClickListener());
setqrimg = (ImageView) findViewById(R.id.setqrimg);
setqret = (EditText) findViewById(R.id.setqrtext);
}
//生成二维码
public class setqrOnClickListener implements OnClickListener{
@SuppressLint("SdCardPath") @Override
public void onClick(View v){
String in = setqret.getText().toString();
if(in.equals("")){
Toast.makeText(SetQRcode.this, "请输入文本", Toast.LENGTH_SHORT).show();
}
else{
try {
setqrb=EncodingHandler.createQRCode(in,400);
setqrimg.setImageBitmap(setqrb);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
6、生成二维码的 .xml文件
</pre><pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
tools:context="com.dachuang.daohang.MainActivity"
tools:ignore="MergeRootFrame">
<EditText
android:id="@+id/setqrtext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginTop="30dp"
android:hint="请输入要编码的内容"
/>
<Button
android:id="@+id/setqrbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="生成二维码"
/>
<ImageView
android:id="@+id/setqrimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>