• Android ZXing 二维码/条形码 开源库的简单应用


    ZXing ( Zebra Crossing )

    ZXing 是一个开源的,使用 Java 实现的多种格式的 1D/2D 条码图像处理库,可以使用该库利用手机内置的摄像头完成条码的扫描与解码。

    ZXing 支持一下几种格式

    1D product1D industrial2D
    UPC-ACode 39QR Code
    UPC-ECode 93Data Matrix
    EAN-8Code 128Aztec
    EAN-13CodabarPDF 417
    UPC/EAN Extension 2/5ITFMaxiCode
    RSS-14
    RSS-Expanded

    build.gradle 的设置

    需要添加依赖

    dependencies {
        implementation 'com.journeyapps:zxing-android-embedded:3.5.0'
    }
    
    • 1
    • 2
    • 3

    注意,这里版本至少为 3.5.0 版本,否则会出现问题。

    gradle.properties 的设置

    android.enableJetifier=true
    android.useAndroidX=true
    
    • 1
    • 2

    请添加上述两行代码,否则会出现如下的报错

    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.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    界面设计 activity_main.xml

    
    <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(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();
            }
        }
    }
    
    • 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
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81

    Capture

    import com.journeyapps.barcodescanner.CaptureActivity;
    
    public class Capture extends CaptureActivity {
    }
    
    • 1
    • 2
    • 3
    • 4

    实现效果

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

  • 相关阅读:
    Maven 官网查找依赖包
    小红书账号管理软件,批量账号发布笔记!
    XML配置文件的解析
    小米妙享无法正常启动,用非管理员权限启动
    mybatis-plus 扩展、插件
    MyBatis从入门到精通真没那么难!跟着我带你深入实践Mybatis技术原理与实战!
    Mythical Beings里的DAO治理指南——Mythical DAO使用方法浅析
    技术分享 | 抓包分析 TCP 协议
    解决Spring Boot启动异常:未配置数据源的问题
    Coder OSS Enterprise 2.3.3 Crack
  • 原文地址:https://blog.csdn.net/qq_17790209/article/details/126768839