Android二维码的生成与扫码-zxing示例代码
由于GitHub上面的zxing功能太多,有的用不到,我就抽取了重要的出来使用,这个可以生成二维码,扫描二维码和相册中的二维码
Demo效果:
1.在project的build.gradle添加如下代码:
allprojects { repositories { maven { url 'https://jitpack.io' } } }
2.在build.gradle添加依赖:
dependencies { compile 'com.github.goodboy321:Scan-Zxing:1.0' }
布局:
<EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入内容,生成二维码" android:text="http://www.baidu.com" /> <Button android:background="@color/colorAccent" android:id="@+id/btn2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="生成二维码" /> <Button android:layout_marginTop="10dp" android:background="@color/colorPrimaryDark" android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="扫码(识别相册中二维码)" /> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> <ImageView android:id="@+id/image_callback" android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal" />
主方法:
public void onClick(View view) { switch (view.getId()) { case R.id.btn1: Intent intent = new Intent(mContext, CaptureActivity.class); startActivityForResult(intent, REQUEST); break; case R.id.btn2: image.setVisibility(View.VISIBLE); //隐藏扫码结果view imageCallback.setVisibility(View.GONE); String content = et.getText().toString().trim(); Bitmap bitmap = null; try { bitmap = BitmapUtils.create2DCode(content);//根据内容生成二维码 tvResult.setVisibility(View.GONE); image.setImageBitmap(bitmap); } catch (Exception e) { e.printStackTrace(); } break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST) { image.setVisibility(View.GONE); imageCallback.setVisibility(View.VISIBLE); String result = data.getStringExtra(CaptureActivity.SCAN_QRCODE_RESULT); Bitmap bitmap = data.getParcelableExtra(CaptureActivity.SCAN_QRCODE_BITMAP); if(bitmap != null){ imageCallback.setImageBitmap(bitmap);//现实扫码图片 } }
具体需求可修改源码
Demo源码下载:Zxing_jb51.net.rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)