public class MainActivity extends AppCompatActivity { private TextView textView; private ImageView qrImgImageView ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //打开扫描界面扫描条形码或二维码 textView = (TextView) findViewById(R.id.result_button_view); qrImgImageView = (ImageView)findViewById(R.id.gen_id); } //扫描 public void scannner(View view){ Intent openCameraIntent = new Intent(this, CaptureActivity.class); startActivityForResult(openCameraIntent, 0); } public void gener_pic(View view){ String contentString = "222222"; if (!contentString.equals("")) { //根据字符串生成二维码图片并显示在界面上,第二个参数为图片的大小(350*350) Bitmap qrCodeBitmap = EncodingUtils.createQRCode(contentString, 350, 350, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); qrImgImageView.setImageBitmap(qrCodeBitmap); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { Bundle bundle = data.getExtras(); String scanResult = bundle.getString("result"); textView.setText(scanResult); } } //导入依赖 https://github.com/bwei1503d/xme 使用libZxing,依赖会报错删除manifest.xml 里面的android:theme="@style/Theme.AppCompat"
//布局
<ImageView android:layout_width="300dp" android:layout_height="300dp" android:id="@+id/gen_id"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/result_button_view"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="扫一扫" android:onClick="scannner" android:id="@+id/scanner_id"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="生成二维码" android:onClick="gener_pic" android:id="@+id/gener_pic"/>
ZXing二维码扫描
最新推荐文章于 2020-12-12 09:26:37 发布