Zxing是一个可以实现条形码和二维码扫描的开源代码,现在利用Android Studio可以很方便的集成该代码库。下面是详细的使用步骤:
一、通过Gradle将Zxing集成到你的项目中
打开项目中的主build.gradle文件,将以下代码添加到dependencies节点中,然后点击编辑器右上角的Sync Now按钮
compile 'com.journeyapps:zxing-android-embedded:3.0.2@aar'compile 'com.google.zxing:core:3.2.0'
二、为执行代码添加按钮
在你的Activity设计窗体中放置两个按钮,分别对应条码和二维码的操作,设置它们的onClick事件分别为onScanBarcode和onScanQrCode,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.bcoder.zxingtest.MainActivity"><Buttonandroid:text="条形码"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/button"android:layout_alignRight="@+id/button"android:layout_alignEnd="@+id/button"android:layout_marginTop="54dp"android:id="@+id/button2"android:onClick="onScanBarcode"/><Buttonandroid:text="二维码"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="59dp"android:id="@+id/button"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:onClick="onScanQrcode"/>
</RelativeLayout>
三、扫描代码的实现
调用Zxing很简单,创建一个IntentIntegrator对象并设置相应的属性,然后调用该实例的initiateScan方法即可打开手机摄像头开始扫描。示例代码如下
IntentIntegrator integrator = new IntentIntegrator(this);
// 设置要扫描的条码类型,ONE_D_CODE_TYPES:一维码,QR_CODE_TYPES-二维码
integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
integrator.setPrompt("扫描条形码");
integrator.setCameraId(0); // 使用默认的相机
integrator.setBeepEnabled(false); // 扫到码后播放提示音
integrator.setBarcodeImageEnabled(true);
integrator.initiateScan();
如果调用相机不成功,可以尝试在AndroidManifest.xml中添加相机的使用权限:
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
四、获取扫码结果
我们可以在onActivityResult中获得扫码的结果,通过IntentResult实例来接收扫描结果,代码如下:
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);if(result != null) {if(result.getContents() == null) {Toast.makeText(this, "扫码取消!", Toast.LENGTH_LONG).show();} else {Toast.makeText(this, "扫描成功,条码值: " + result.getContents(), Toast.LENGTH_LONG).show();}} else {// This is important, otherwise the result will not be passed to the fragmentsuper.onActivityResult(requestCode, resultCode, data);}}
五、该例的java全部java代码
package com.bcoder.zxingtest;import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void onScanBarcode(View v){IntentIntegrator integrator = new IntentIntegrator(this);integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);integrator.setPrompt("扫描条形码");integrator.setCameraId(0); integrator.setBeepEnabled(false);integrator.initiateScan();}public void onScanQrcode(View v){IntentIntegrator integrator = new IntentIntegrator(this);integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);integrator.setPrompt("扫描二维码");integrator.setCameraId(0); integrator.setBeepEnabled(false);integrator.initiateScan();}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);if(result != null) {if(result.getContents() == null) {Toast.makeText(this, "扫码取消!", Toast.LENGTH_LONG).show();} else {Toast.makeText(this, "扫描成功,条码值: " + result.getContents(), Toast.LENGTH_LONG).show();}} else {super.onActivityResult(requestCode, resultCode, data);}}
}
六、其他注意事项
IntentIntegrator有两个方法是forFragment和forSupportFragment应该是用于支持Fragment和支持库版本的Fragment,如果需要在Fragment中使用Zxing可以研究一下。