二维码的扫描和生成二维码

时间:
2018/04/11 13:57:51
摘要:之前自己一直想要去实现一个二维码的扫描和生成,但是一直拖到现在,今天趁着夜色落幕,气氛还算可以(各种声音的夹杂中),完成了这个扫描和生成二维码的工具,在这里总结一下。 首先普及一下什么是二维码和二维码开源库
之前自己一直想要去实现一个二维码的扫描和生成,但是一直拖到现在,今天趁着夜色落幕,气氛还算可以(各种声音的夹杂中),完成了这个扫描和生成二维码的工具,在这里总结一下。
首先普及一下什么是二维码和二维码开源库

QR Code

QRCode简介:
QRCode全称Quick Response Code
通过在一个矩形区域内使用黑白像素来进行编码
高纠错性、高可用性、高识别性
ZXing简介:

ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条形图像处理库,它包含了联系到其他语言的端口
ZXing可以实现使用手机的内置的摄像头完成条形码的扫描和解码
ZXing项目地址:
https://github.com/zxing/zxing
项目开始前

我们可以看下这个zxing,我们不可能去下载整个项目然后让我们的项目去依赖他,因为他足足有125MB左右啊,再说我们只需要实现可以在手机上实现扫描和生成二维码就行了,所以就有大神们从里面抽取了部分有关这方面的类和方法,这里我们可以找一下资料,这样就会大大减少,不到1MB。
这里先上项目地址(在这里可以看到这个依赖库):https://github.com/wuyinlei/LearnZxing

项目第一步:扫描二维码功能实现

完成工作的其实就是依赖库中的CaptureActivity.java,这里面已经为我们封装好了调用相机,解析二维码信息的类,并提供了返回值,我们只需要去调用,使用
       startActivityForResult(new Intent(MainActivity.this, CaptureActivity.class), 0);

    然后重写onActivityResult(int requestCode, int resultCode, Intent data)这个方法,在这个方法中我们获取到扫描后获得的数据

if (resultCode == RESULT_OK) {
            //接受返回的二维码数据
            Bundle bundle = data.getExtras();
            //这个key是在CaptureActivity中的this.setResult(RESULT_OK, resultIntent)查到的
            String result = bundle.getString(\"result\");
            tvResult.setText(result);
        }
这个时候我们来看下实现的效果图:
这个是我用在线二维码生成器生成的一个二维码,这个时候我们看下扫描的结果。



扫描之后的结果:



好了,这个时候扫描的功能实现了

项目第二步:生成二维码功能实现

其实这个也是很简单的,因为library里面已经提供了一个工具类EncodingUtils.java(二维码生成工具),这里面提供的这个方法

EncodingUtils.createQRCode(input, 500, 500, mCheckBox.isChecked() ? BitmapFactory.decodeResource(getResources(), R.mipmap.kenan) : null)

这个方法传入四个参数
/**
     * 创建二维码
     *
     * @param content   content     创建的二维码中包含的信息
     * @param widthPix  widthPix   宽度
     * @param heightPix heightPix  高度
     * @param logoBm    logoBm    是否在二维码中间加入自定义的图片
     * @return 二维码
     */
我们来看下布局文件吧,也是很简答的:

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout
    xmlns:android=\"http://schemas.android.com/apk/res/android\"
    xmlns:tools=\"http://schemas.android.com/tools\"
    android:layout_width=\"match_parent\"
    android:layout_height=\"match_parent\"
    android:orientation=\"vertical\"
    tools:context=\"com.example.wuyin.learnzxing.MainActivity\">

    <Button
        android:id=\"@+id/btn_start_scan\"
        android:layout_width=\"match_parent\"
        android:layout_height=\"wrap_content\"
        android:text=\"开始扫描\"/>

    <TextView
        android:layout_width=\"match_parent\"
        android:layout_height=\"wrap_content\"
        android:text=\"Result : \"
        android:textSize=\"16sp\"/>

    <TextView
        android:id=\"@+id/result\"
        android:layout_width=\"match_parent\"
        android:layout_height=\"wrap_content\"
        android:layout_marginTop=\"5dp\"
        android:textSize=\"20sp\"/>

    <EditText
        android:id=\"@+id/et_zxing\"
        android:layout_width=\"match_parent\"
        android:layout_height=\"wrap_content\"/>

    <Button
        android:id=\"@+id/make_qrcode\"
        android:layout_width=\"match_parent\"
        android:layout_height=\"wrap_content\"
        android:text=\"创建二维码\"/>

    <ImageView
        android:id=\"@+id/showQrcode\"
        android:layout_gravity=\"center\"
        android:background=\"@mipmap/ic_launcher\"
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"/>
    <CheckBox
        android:id=\"@+id/checkbox\"
        android:text=\"是否在二维码中间生成图片\"
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"/>

</LinearLayout>

因为就一个方法调用,这里就不多做介绍了,大家有兴趣可以下载源码看下,这里不多做解释,直接来看下生成的二维码:
不需要生成中间自定义图片的二维码:
二维码生成器_二维码生成_二维码_免费二维码生成?_在线二维码?

400-8858205

service@mabao.co

上海市松江区方泗公路18号掌安大厦

QQ客服:
15173768
QQ客服:
879685504
Copyright © 2009-2017 Mabao.com All Rights Reserved 沪ICP备15032505号-6
Power by Souyisi