Android 生成二维码

Android 生成二维码

在根版本导入

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

添加依赖 implementation ‘com.github.jwkj:LibZXing:v1.0.4’

布局

<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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <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>
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.jwkj.libzxing.OnQRCodeScanCallback;
import com.jwkj.libzxing.QRCodeManager;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button bt_scan = findViewById(R.id.bt_scan);
        Button bt_create_one = findViewById(R.id.bt_create_one);
        Button bt_create_two = findViewById(R.id.bt_create_two);
        imageView = findViewById(R.id.iv);

        bt_scan.setOnClickListener(this);
        bt_create_one.setOnClickListener(this);
        bt_create_two.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_scan:
                QRCodeManager.getInstance().with(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() {

                    }
                });
                break;
            case R.id.bt_create_one:
                Bitmap bitmap=QRCodeManager.getInstance().createQRCode("https://user.qzone.qq.com/731177635/infocenter",400,400);
                imageView.setImageBitmap(bitmap);
                break;
            case R.id.bt_create_two:
                Bitmap bitmap1=BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher_round);
                Bitmap bt=QRCodeManager.getInstance().createQRCode("http://www.baidu.com", 400, 400,bitmap1);
                imageView.setImageBitmap(bt);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Toast.makeText(this, "走了", Toast.LENGTH_SHORT).show();
        QRCodeManager.getInstance().with(this).onActivityResult(requestCode,resultCode,data);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值