扫描二维码 生成二维码

1.导入依赖

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

}


 

implementation 'com.github.jwkj:LibZXing:v1.0.4'

2.配置文件


3.xml



4.maactivity

public class MainActivity extends AppCompatActivity {

    private Button sao,sheng,nullbtn;
    private ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sao=findViewById(R.id.sao);
        sheng=findViewById(R.id.sheng);
        nullbtn=findViewById(R.id.nullbtn);
        img=findViewById(R.id.img);
        //点击扫描二维码
        sao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QRCodeManager.getInstance()
                        .with(MainActivity.this)
                        .setReqeustType(1)
                        .scanningQRCode(new OnQRCodeScanCallback() {
                            @Override
                            public void onCompleted(String result) {
                                Toast.makeText(MainActivity.this, "结果:" + result, Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onError(Throwable errorMsg) {
                                Toast.makeText(MainActivity.this, "扫描错误"+errorMsg.toString(),Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onCancel() {
                                Toast.makeText(MainActivity.this, "扫描任务取消了", Toast.LENGTH_SHORT).show();
                            }
                        });
            }
        });
        //生成二维码
        sheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmp = QRCodeManager.getInstance().createQRCode("ZXingCode", 400, 400);
               img.setImageBitmap(bitmp);
            }
        });
        nullbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fack);
                Bitmap logo_qRcode = QRCodeManager.getInstance().createQRCode("ZXingCode", 400, 400, bitmap);
                img.setImageBitmap(logo_qRcode);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //会掉结果
        QRCodeManager.getInstance().with(MainActivity.this).onActivityResult(requestCode, resultCode, data);
    }
}

你可能感兴趣的:(扫描二维码 生成二维码)