📜  如何在Android中使用Zxing库读取QR码?

📅  最后修改于: 2021-05-09 18:03:30             🧑  作者: Mango

Zxing代表Zebra Crossing,它是用于集成QR(快速响应)代码处理的最受欢迎的开源API之一。它是用Java实现的条形码图像处理库,带有其他语言的端口。它支持一维产品,一维工业和二维条形码。 Google通过网络搜索使用ZXing来获取可索引的Web上的数百万条码。它还为Android的条形码扫描仪应用程序奠定了基础,并与Google产品和图书搜索结合在一起。

二维码

它是“快速响应代码”的缩写。它是白色和黑色正方形的组合,可以使用QR扫描仪轻松读取。它通常使用四种编码模式

  • 数字
  • 字母数字
  • 字节/二进制
  • 汉子

它用于身份验证和在线支付。下面给出了一个示例GIF,以了解我们将在本文中做些什么。注意,我们将使用Java语言实现该项目。

使用Android示例GIF中的Zxing库读取QR代码

分步实施

在这个项目中,我们正在创建一个基本的QR扫描仪应用程序,该应用程序用于扫描QR码并在屏幕上显示结果。

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。

步骤2:添加依赖项

为了在我们的应用程序中使用Zxing库,我们需要在我们的应用程序gradle文件中添加它的依赖项。要添加依赖项,请转到Gradle脚本> build.gradle(Module:app)并添加以下依赖项。添加依赖项后,您需要单击立即同步

在继续之前,让我们添加一些颜色属性以增强应用程序栏。转到应用程序> res>值> colors.xml并添加以下颜色属性。

XML
 
    #0F9D58 
    #16E37F 
    #03DAC5 


XML


  
    
  
    
  
    


Java
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
  
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
  
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
  
// implements onClickListener for the onclick behaviour of button
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button scanBtn;
    TextView messageText, messageFormat;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // referencing and initializing
        // the button and textviews
        scanBtn = findViewById(R.id.scanBtn);
        messageText = findViewById(R.id.textContent);
        messageFormat = findViewById(R.id.textFormat);
  
        // adding listener to the button
        scanBtn.setOnClickListener(this);
  
    }
  
    @Override
    public void onClick(View v) {
        // we need to create the object 
        // of IntentIntegrator class
        // which is the class of QR library
        IntentIntegrator intentIntegrator = new IntentIntegrator(this);
        intentIntegrator.setPrompt("Scan a barcode or QR Code");
        intentIntegrator.setOrientationLocked(true);
        intentIntegrator.initiateScan();
    }
  
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        // if the intentResult is null then
        // toast a message as "cancelled"
        if (intentResult != null) {
            if (intentResult.getContents() == null) {
                Toast.makeText(getBaseContext(), "Cancelled", Toast.LENGTH_SHORT).show();
            } else {
                // if the intentResult is not null we'll set 
                // the content and format of scan message
                messageText.setText(intentResult.getContents());
                messageFormat.setText(intentResult.getFormatName());
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}


步骤3:创建布局文件activity_main.xml

在这一步中,我们将创建应用程序的布局,该布局具有一个用于扫描的按钮,两个TextView用于QR码的消息内容,第二个用于扫描消息的格式。转到应用程序> res>布局> activity_main.xml并添加以下代码段。

XML格式



  
    
  
    
  
    

步骤4:使用MainActivity。 Java文件

在这一步中,我们将使用MainActivity。我们首先在其中初始化按钮和两个TextView的Java文件。在按钮的onClick()行为中,我们创建IntentIntegrator类的对象,该类用于调用initialScan ()方法进行扫描过程。之后,在onActivityResult()方法中,我们将检查扫描的消息是否为null,然后将消息烘烤为“已取消”,否则我们将在TextViews上设置扫描的消息及其格式。

Java

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
  
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
  
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
  
// implements onClickListener for the onclick behaviour of button
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button scanBtn;
    TextView messageText, messageFormat;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // referencing and initializing
        // the button and textviews
        scanBtn = findViewById(R.id.scanBtn);
        messageText = findViewById(R.id.textContent);
        messageFormat = findViewById(R.id.textFormat);
  
        // adding listener to the button
        scanBtn.setOnClickListener(this);
  
    }
  
    @Override
    public void onClick(View v) {
        // we need to create the object 
        // of IntentIntegrator class
        // which is the class of QR library
        IntentIntegrator intentIntegrator = new IntentIntegrator(this);
        intentIntegrator.setPrompt("Scan a barcode or QR Code");
        intentIntegrator.setOrientationLocked(true);
        intentIntegrator.initiateScan();
    }
  
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        // if the intentResult is null then
        // toast a message as "cancelled"
        if (intentResult != null) {
            if (intentResult.getContents() == null) {
                Toast.makeText(getBaseContext(), "Cancelled", Toast.LENGTH_SHORT).show();
            } else {
                // if the intentResult is not null we'll set 
                // the content and format of scan message
                messageText.setText(intentResult.getContents());
                messageFormat.setText(intentResult.getFormatName());
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}

输出: