zxing扫条形码简单使用实例

转载自:http://bcoder.com/java/integrate-zxing-to-implement-scan-barcode-and-qrcode-in-android-studio

Zxing是一个可以实现条形码和二维码扫描的开源代码,现在利用Android Studio可以很方便的集成该代码库。下面是详细的使用步骤:

一、通过Gradle将Zxing集成到你的项目中

  打开项目中的主build.gradle文件,将以下代码添加到dependencies节点中,然后点击编辑器右上角的Sync Now按钮

 

 

1

2

    compile 'com.journeyapps:zxing-android-embedded:3.0.2@aar'

    compile 'com.google.zxing:core:3.2.0'

 

二、为执行代码添加按钮

  在你的Activity设计窗体中放置两个按钮,分别对应条码和二维码的操作,设置它们的onClick事件分别为onScanBarcode和onScanQrCode,代码如下:

 

XHTML

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

    xmlns: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">

 

    

        android: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"/>

 

    

        android: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"/>

 

三、扫描代码的实现

  调用Zxing很简单,创建一个IntentIntegrator对象并设置相应的属性,然后调用该实例的initiateScan方法即可打开手机摄像头开始扫描。示例代码如下

 

 

1

2

3

4

5

6

7

8

9

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中添加相机的使用权限:

 

XHTML

 

1

     

 

四、获取扫码结果

  我们可以在onActivityResult中获得扫码的结果,通过IntentResult实例来接收扫描结果,代码如下:

 

Java

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

    @Override

    protected 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 fragment

            super.onActivityResult(requestCode, resultCode, data);

        }

    }

 

五、该例的java全部java代码

 

Java

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

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 {

 

    @Override

    protected 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();

    }

 

    @Override

    protected 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可以研究一下。

你可能感兴趣的:(Android开发)