Android基于Zxing实现扫描和生成二维码
2016-06-23 20:44
801 查看
这方面的文章也不少,我就当做是一个笔记吧!
首先导入Zxing的Model,这个网上有很多,下载地址:http://download.csdn.net/detail/wangbin1992321/9557850
Android Studio直接导入然后添加以来即可
直接上代码
这个是布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clicl"
android:text="进入扫描二维码页面" />
<ImageView
android:id="@+id/butto"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
ImageView是Activity启动就会显示一个带有logo的二维码!Button点击进入扫描的Activity!够简单吧!
生成二维码的方法是
案例的源码地址:http://download.csdn.net/detail/wangbin1992321/9557843
首先导入Zxing的Model,这个网上有很多,下载地址:http://download.csdn.net/detail/wangbin1992321/9557850
Android Studio直接导入然后添加以来即可
直接上代码
这个是布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clicl"
android:text="进入扫描二维码页面" />
<ImageView
android:id="@+id/butto"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
ImageView是Activity启动就会显示一个带有logo的二维码!Button点击进入扫描的Activity!够简单吧!
生成二维码的方法是
EncodingUtils下的
createQRCode方法!有四个参数分别是1、二维码的原字符串;2、宽度;3。高度;4,就是logo的bitmap对象
如果不需要logo那么可以第四个蚕食为null即可!
动态获取摄像头权限从而进入扫描页面打开摄像头
public void clicl(View v) { //动态获取权限 if (Build.VERSION.SDK_INT >= 23) { //检查是否获得摄像头权限 int checkCamera = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA); //判断是否获得 如果没有请求该权限 if (checkCamera != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 123); return; } } startActivity(new Intent(this, CaptureActivity.class)); }这里需要注意一下不能用
permission_group 我只知道试了没有用,但是具体为什么也没注意,如果有人知道麻烦请告诉我一下啊,谢谢!
接下来处理获取权限的回调
@Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case 123: if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { startActivity(new Intent(this, CaptureActivity.class)); } else Toast.makeText(this, "对不起!请允许程序获得打开摄像头权限", Toast.LENGTH_LONG).show(); break; default: super.onRequestPermissionsResult(requestCode, permissions, grantResults); } }
案例的源码地址:http://download.csdn.net/detail/wangbin1992321/9557843
相关文章推荐
- android6.0中permission授予机制的改变
- Camera2进行扫码解析
- Android 6.0 动态权限 java反射
- 修改zxing扫描二维码区域大小
- Android6.0动态获取权限
- Android 6.0RunTime Permission完美解决方案
- iOS应用中打开 手电筒/闪光灯 的方法
- iOS扫描二维码方法
- android开发游记:二维码和条形码的识别和生成
- Android6.0动态权限
- Xamarin.android扫描二维码,条形码
- iOS7使用原生API进行二维码和条形码的扫描
- 扫描二维码
- Android6.0动态权限管理库RuntimePermission
- [swift3.0]-二维码扫描、生成二维码、识别二维码
- Android之动态申请权限(API23以上需求)
- Android进阶封装之一个类实现兼容Android 6.0权限、适配Android7.0 拍照!
- android 6.0运行时权限
- Android6.0权限的处理