Android二维码的生成与扫码-zxing示例代码
2017-03-10 08:25
816 查看
由于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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- android两行代码生成二维码示例
- Android平台利用ZXING生成二维码图片
- Android学习之 google Zxing 二维码条形码生成与解析
- Android:使用ZXing生成二维码(支持添加Logo图案)
- Android使用ZXing生成带图片的二维码
- 【ZXing】Android使用zxing生成二维码图片
- Android平台利用ZXING生成二维码图片
- Android -- 采用zxing生成二维码
- Android 使用zxing包生成中间有logo的二维码图片
- Android使用二维码之_zxing生成二维码
- Android之zxing二维码生成与识别
- Android小技巧/二维码生成Zxing
- 在Android用ZXing.jar识别二维码的精简版(简化了配置和代码)
- Android平台利用Zxing生成二维码与解析图片中的二维码
- Android平台利用ZXING生成二维码图片
- ZXing 为Android应用生成二维码
- ZPL 打印条码、二维码及小票(中文/汉字),生成条码、二维码图片【Asp.Net】-含示例代码
- Android二维码开源项目zxing用例简化和生成二维码、条形码
- android 使用zxing生成中间嵌套logo的二维码
- Android平台利用ZXING生成二维码图片