扫描二维码和生成二维码

1.依赖

 //二维码
    implementation 'cn.bingoogolapple:bga-qrcode-zxing:1.3.4'

2.权限


    
    
    

布局




    

扫一扫页面布局




    



在styles.xml中


在代码中

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.bt_scan)
    Button btScan;
    @BindView(R.id.Et_context)
    EditText EtContext;
    @BindView(R.id.bt_logo)
    Button btLogo;
    @BindView(R.id.bt_image)
    Button btImage;
    @BindView(R.id.image)
    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

    }

    @OnClick({R.id.bt_scan, R.id.bt_logo, R.id.bt_image})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_scan:
                startActivity(new Intent(MainActivity.this, ScanActivity.class));
                break;
                //生成带logo二维码
            case R.id.bt_logo:
                new AsyncTask(){

                    @Override
                    protected Bitmap doInBackground(String... strings) {
                        String m = EtContext.getText().toString().trim();
                        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dimension1);
                        return QRCodeEncoder.syncEncodeQRCode(m,150,Color.BLACK,bitmap);
                    }

                    @Override
                    protected void onPostExecute(Bitmap bitmap) {
                        super.onPostExecute(bitmap);
                        if (bitmap!=null) {
                            image.setImageBitmap(bitmap);
                        }else{
                            Toast.makeText(MainActivity.this, "扫描错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                }.execute();

                break;
            //生成不带logo二维码
            case R.id.bt_image:
               new AsyncTask() {
                    @Override
                    protected Bitmap doInBackground(Void... voids) {
                        String s = EtContext.getText().toString().trim();
                        return QRCodeEncoder.syncEncodeQRCode(s, 150);
                    }

                    @Override
                    protected void onPostExecute(Bitmap bitmap) {
                        super.onPostExecute(bitmap);
                        image.setImageBitmap(bitmap);
                    }
                }.execute();
                break;
        }
    }
}

在扫描页面代码

public class ScanActivity extends AppCompatActivity implements QRCodeView.Delegate {

    @BindView(R.id.zxing)
    ZXingView zxing;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);
        ButterKnife.bind(this);
        initZXingView();


    }

    private void initZXingView() {
        //扫描二维码代理
        zxing.setDelegate(this);
        //显示扫描框
        zxing.showScanRect();
        //开启相机
        zxing.startCamera();
        zxing.startSpotDelay(1000);
    }

    @Override
    public void onScanQRCodeSuccess(String result) {

    }

    @Override
    public void onCameraAmbientBrightnessChanged(boolean isDark) {

    }

    @Override
    public void onScanQRCodeOpenCameraError() {

    }
}

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