1.需要先导入外部库libzxing,添加依赖时一定要注意settings.gradle里
include ':app','libzxing'
2.只需要在点击事件里执行功能就行
//扫描 scanBarCodeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(CodeActivity.this, CaptureActivity.class); startActivityForResult(intent,0); } });
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode==RESULT_OK){ Bundle bundle = data.getExtras(); String result = bundle.getString("result"); resultTextView.setText(result); } }
//生成二维码 generateQRCodeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String contentString = qrStrEditText.getText().toString(); if (!TextUtils.isEmpty(contentString)){ Bitmap qrCodeBitmap = EncodingUtils.createQRCode(contentString, 350, 350,mCheckBox.isChecked() ? BitmapFactory.decodeResource(getResources(), R.drawable.umeng_socialize_qq) : null); qrImgImageView.setImageBitmap(qrCodeBitmap); }else{ Toast.makeText(CodeActivity.this, "Text can not be empty", Toast.LENGTH_SHORT).show(); } } });