二维码

在这里插入代码片
allprojects {
    repositories {
     .....
     //导依赖
        maven { url 'https://jitpack.io' }
    }
}


//布局
<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="horizontal"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="扫描二维码"
       />
    <Button
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="生成二维码"
        />

    <Button
        android:id="@+id/bt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="生成有图二维码"
        />
    <ImageView
        android:layout_width="wrap_content"
        android:id="@+id/iv"
        android:layout_height="wrap_content" />



</LinearLayout>


implementation 'com.github.jwkj:LibZXing:v1.0.4'
ublic class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button bt1;
    private Button bt2;
    private Button bt3;
    private ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt1 = findViewById(R.id.bt1);
        bt2 = findViewById(R.id.bt2);
        bt3 = findViewById(R.id.bt3);
        iv = findViewById(R.id.iv);
        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        bt3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt1:
                //扫描
                QRCodeManager.getInstance()
                        .with(this)
                        .setReqeustType(1)//可以不设置,默认是0
                        .scanningQRCode(new OnQRCodeScanCallback() {
                            @Override
                            public void onCompleted(String result) {//扫描成功之后回调,result就是扫描的结果
                            //    Log.append("\n\n(结果)" + result);
                            }

                            @Override
                            public void onError(Throwable errorMsg) {//扫描出错的时候回调
                            //    controlLog.append("\n\n(错误)" + errorMsg.toString());
                            }

                            @Override
                            public void onCancel() {//取消扫描的时候回调
                             //   controlLog.append("\n\n(取消)扫描任务取消了");
                            }
                        });

                break;
                //无图二维码
            case R.id.bt2:
                Bitmap bitmap = QRCodeManager.getInstance().createQRCode("http://www.baidu.com", 400, 400);
                iv.setImageBitmap(bitmap);

                break;
                //有图二维码
            case R.id.bt3:
                Bitmap resource = BitmapFactory.decodeResource(getResources(), R.drawable.q);
                Bitmap bt = QRCodeManager.getInstance().createQRCode("http://www.baidu.com", 400, 400,resource);
                iv.setImageBitmap(bt);
                break;




        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值