安卓二维码生成以及简单实用

本文介绍如何在Android应用中使用LibZXing库进行二维码扫描和生成,包括库的导入、依赖配置及XML布局设置。演示了通过按钮触发扫描功能,以及生成普通和带有Logo的二维码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先在工程库

allprojects {
    repositories {
        google()
        jcenter()
			//加入此行
        maven { url 'https://jitpack.io' }
    }
}


导入依赖

  implementation 'com.github.jwkj:LibZXing:v1.0.4'
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/bt_scan"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="扫描" />

        <Button
            android:id="@+id/bt_create_one"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="生成不带logo的二维码" />

        <Button
            android:id="@+id/bt_create_two"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="生成带logo的二维码" />
    </LinearLayout>
    <ImageView
        android:layout_width="wrap_content"
        android:id="@+id/iv"
        android:layout_height="wrap_content" />

</LinearLayout>
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
		//扫描
        bt_scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QRCodeManager.getInstance().with(MainActivity.this).setReqeustType(0).scanningQRCode(new OnQRCodeScanCallback() {
                    @Override
                    public void onCompleted(String s) {
                        Toast.makeText(MainActivity.this, "结果:"+s, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(Throwable throwable) {

                    }

                    @Override
                    public void onCancel() {

                    }
                });
            }
        });
        //生成不带头像的二维码
        bt_create_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmap = QRCodeManager.getInstance().createQRCode("http://www.baidu.com", 400, 400);
                iv.setImageBitmap(bitmap);
            }
        });
          //生成带头像的二维码
        bt_create_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap resource = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round);
                Bitmap bt = QRCodeManager.getInstance().createQRCode("http://www.baidu.com", 400, 400,resource);
                iv.setImageBitmap(bt);
            }
        });


    }
    private void initView() {

        bt_scan = (Button) findViewById(R.id.bt_scan);
        bt_create_one = (Button) findViewById(R.id.bt_create_one);
        bt_create_two = (Button) findViewById(R.id.bt_create_two);
        iv = (ImageView) findViewById(R.id.iv);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Toast.makeText(this, "走了:", Toast.LENGTH_SHORT).show();
        //注册onActivityResult
        QRCodeManager.getInstance().with(this).onActivityResult(requestCode, resultCode, data);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值