Android ZXing 二维码/条形码 开源库的简单应用
ZXing ( Zebra Crossing )
ZXing 是一个开源的,使用 Java 实现的多种格式的 1D/2D 条码图像处理库,可以使用该库利用手机内置的摄像头完成条码的扫描与解码。
ZXing 支持一下几种格式
1D product | 1D industrial | 2D |
---|---|---|
UPC-A | Code 39 | QR Code |
UPC-E | Code 93 | Data Matrix |
EAN-8 | Code 128 | Aztec |
EAN-13 | Codabar | PDF 417 |
UPC/EAN Extension 2/5 | ITF | MaxiCode |
RSS-14 | ||
RSS-Expanded |
build.gradle 的设置
需要添加依赖
dependencies {
implementation 'com.journeyapps:zxing-android-embedded:3.5.0'
}
注意,这里版本至少为 3.5.0
版本,否则会出现问题。
gradle.properties 的设置
android.enableJetifier=true
android.useAndroidX=true
请添加上述两行代码,否则会出现如下的报错
Duplicate class android.support.v4.app.INotificationSideChannel found in modules core-1.8.0-runtime (androidx.core:core:1.8.0) and support-compat-25.1.0-runtime (com.android.support:support-compat:25.1.0)
Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules core-1.8.0-runtime (androidx.core:core:1.8.0) and support-compat-25.1.0-runtime (com.android.support:support-compat:25.1.0)
Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy found in modules core-1.8.0-runtime (androidx.core:core:1.8.0) and support-compat-25.1.0-runtime (com.android.support:support-compat:25.1.0)
Duplicate class android.support.v4.os.IResultReceiver found in modules core-1.8.0-runtime (androidx.core:core:1.8.0) and support-compat-25.1.0-runtime (com.android.support:support-compat:25.1.0)
Duplicate class android.support.v4.os.IResultReceiver$Stub found in modules core-1.8.0-runtime (androidx.core:core:1.8.0) and support-compat-25.1.0-runtime (com.android.support:support-compat:25.1.0)
Duplicate class android.support.v4.os.IResultReceiver$Stub$Proxy found in modules core-1.8.0-runtime (androidx.core:core:1.8.0) and support-compat-25.1.0-runtime (com.android.support:support-compat:25.1.0)
Duplicate class android.support.v4.os.ResultReceiver found in modules core-1.8.0-runtime (androidx.core:core:1.8.0) and support-compat-25.1.0-runtime (com.android.support:support-compat:25.1.0)
Duplicate class android.support.v4.
os.ResultReceiver$1 found in modules core-1.8.0-runtime (androidx.core:core:1.8.0) and support-compat-25.1.0-runtime (com.android.support:support-compat:25.1.0)
Duplicate class android.support.v4.os.ResultReceiver$MyResultReceiver found in modules core-1.8.0-runtime (androidx.core:core:1.8.0) and support-compat-25.1.0-runtime (com.android.support:support-compat:25.1.0)
Duplicate class android.support.v4.os.ResultReceiver$MyRunnable found in modules core-1.8.0-runtime (androidx.core:core:1.8.0) and support-compat-25.1.0-runtime (com.android.support:support-compat:25.1.0)
Go to the documentation to learn how to Fix dependency resolution errors.
界面设计 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/Scan_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:text="Scan"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Sl9Jgqbk-1662629225209)(Readme.assets/截屏2022-09-08 16.54.47.png)]
MainActivity
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public class MainActivity extends AppCompatActivity {
Button btScan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btScan = findViewById(R.id.Scan_btn);
btScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 初始化 intent integrator
IntentIntegrator intentIntegrator = new IntentIntegrator(
MainActivity.this
);
// 设置扫描界面的提示词
intentIntegrator.setPrompt("For Flash use volume up key");
// 设置蜂鸣器效果
intentIntegrator.setBeepEnabled(true);
// 停止界面旋转
intentIntegrator.setOrientationLocked(true);
// 设置扫描时用到的 Activity,在这个界面调用 Camera
intentIntegrator.setCaptureActivity(Capture.class);
// 初始化扫描程序
intentIntegrator.initiateScan();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
IntentResult intentResult = IntentIntegrator.parseActivityResult(
// 初始化 Intent 结果
requestCode,resultCode,data
);
// 检查状态
if (intentResult.getContents() != null){
// 当扫描结果不为空的时候,初始化提示框
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this
);
// 设置标题
builder.setTitle("Result");
// 设置要显示的信息内容
builder.setMessage(intentResult.getContents());
// 设置一个可以按下的按钮
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// 按下按钮之后对话框消失
dialogInterface.dismiss();
}
});
// 展示对话框
builder.show();
}else {
// 当扫描结果为空的时候
// 展示 Toast 信息
Toast.makeText(getApplicationContext(),"You did not scan anything",Toast.LENGTH_LONG)
.show();
}
}
}
Capture
import com.journeyapps.barcodescanner.CaptureActivity;
public class Capture extends CaptureActivity {
}