开发Android二维码扫描功能

jopen 9年前

 

二维码开发主要依赖ZXing开源项目

项目地址:https://github.com/zxing/zxing

这个开源项目可以扫描一维,和二维码,

一维码指的是书后面的条形码

首先配置ZXing库和Android工程

项目本身非常大,我们只需使用精简版的库

导入之后

Android二维码扫描功能的集成开发

Android二维码扫描功能的集成开发

TestQRcode 项目作为库被其他项目使用。

BarCodeTestActivity

package com.ericssonlabs;  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 BarCodeTestActivity extends Activity {    /** Called when the activity is first created. */    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) {          //打开扫描界面扫描条形码或二维码          Intent openCameraIntent = new Intent(BarCodeTestActivity.this,CaptureActivity.class);          startActivityForResult(openCameraIntent, 0);        }      });      Button generateQRCodeButton = (Button) this.findViewById(R.id.btn_add_qrcode);      generateQRCodeButton.setOnClickListener(new OnClickListener() {        @Override        public void onClick(View v) {          try {            String contentString = qrStrEditText.getText().toString();            if (!contentString.equals("")) {              //根据字符串生成二维码图片并显示在界面上,第二个参数为图片的大小(350*350)              Bitmap qrCodeBitmap = EncodingHandler.createQRCode(contentString, 350);              qrImgImageView.setImageBitmap(qrCodeBitmap);            }else {              Toast.makeText(BarCodeTestActivity.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);      }    }  }

创建项目TestQRcode,来使用BarCodeTest库

xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/container"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.jikexueyuan.testqrcode.MainActivity"    tools:ignore="MergeRootFrame" >    <Button      android:id="@+id/scan"      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="开始扫描二维码" />    <TextView       android:id="@+id/text"      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="show"      />     <EditText        android:id="@+id/input"       android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:hint="请输入要编码的内容"       />     <Button        android:id="@+id/gen"       android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:text="生成二维码"       />     <ImageView        android:id="@+id/img"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:layout_gravity="center_horizontal"       />  </LinearLayout>

MainActivity
package com.jikexueyuan.testqrcode;  import junit.framework.Test;  import com.google.zxing.WriterException;  import com.zxing.activity.CaptureActivity;  import com.zxing.encoding.EncodingHandler;  import android.app.Activity;  import android.app.ActionBar;  import android.app.Fragment;  import android.content.Intent;  import android.graphics.Bitmap;  import android.os.Bundle;  import android.view.LayoutInflater;  import android.view.Menu;  import android.view.MenuItem;  import android.view.View;  import android.view.ViewGroup;  import android.widget.Button;  import android.widget.EditText;  import android.widget.ImageView;  import android.widget.TextView;  import android.widget.Toast;  import android.os.Build;  public class MainActivity extends Activity {    private Button scanButton;    private TextView text;    private EditText input;    private Button genButton;    private ImageView img;    @Override    protected void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);      scanButton = (Button) findViewById(R.id.scan);      text = (TextView) findViewById(R.id.text);      scanButton.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View arg0) {          Toast.makeText(MainActivity.this, "你写可以扫描条形码或者二维码",              Toast.LENGTH_SHORT).show();          Intent startScan = new Intent(MainActivity.this,              CaptureActivity.class);          // startActivity(startScan);          startActivityForResult(startScan, 0);        }      });      input = (EditText) findViewById(R.id.input);      genButton = (Button) findViewById(R.id.gen);      img = (ImageView) findViewById(R.id.img);      genButton.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View arg0) {          String in = input.getText().toString();          if (in.equals("")) {            Toast.makeText(MainActivity.this, "请输入文本",                Toast.LENGTH_SHORT).show();          } else {            try {              Bitmap qrcode = EncodingHandler.createQRCode(in, 400);              img.setImageBitmap(qrcode);            } catch (WriterException e) {              e.printStackTrace();            }          }        }      });    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {      super.onActivityResult(requestCode, resultCode, data);      if (resultCode == RESULT_OK) {        String result = data.getExtras().getString("result");        text.setText(result);      }    }  }

注册CaptureActivity.java
<activity    android:configChanges="orientation|keyboardHidden"    android:name="com.zxing.activity.CaptureActivity"    android:screenOrientation="portrait"    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"    android:windowSoftInputMode="stateAlwaysHidden" >  </activity>

另外还要添加两个权限,一个是振动,一个是照相机
<uses-permission android:name="android.permission.CAMERA"/>      <uses-permission android:name="android.permission.VIBRATE"/>

下载地址:链接:http://pan.baidu.com/s/1uJVII 密码:0uof