资讯专栏INFORMATION COLUMN

慕课网_《Java生成二维码》学习总结

QLQ / 425人阅读

摘要:时间年月日星期五说明本文部分内容均来自慕课网。线性堆叠式二维码示意图矩阵式二维码在一个矩形空间通过黑白像素在矩阵中的不同分布进行编码。

时间:2017年06月23日星期五
说明:本文部分内容均来自慕课网。@慕课网:http://www.imooc.com
教学示例源码:无
个人学习源码:https://github.com/zccodere/s...

第一章:二维码的概念 1-1 二维码概述

二维码示意图

使用场景

目录

二维码概念
二维码发展历史
二维码分类
二维码优缺点
QR Code
示例讲解

二维码概念

二维条码/二维码(2-dimensional bar code)是用某种特定的几何图形按一定规律在平面(二位方向上)分布的黑白相间的图形记录数据符号信息的图形。
第二章:二维码发展历史 2-1 二维码发展历史

发展历史

一维条码

一维条码是由一组粗细不同、黑白(或彩色)相间的条、空及其相应的字符(数字字母)组成的标记,即传统条码。

一维条码示意图

二维条码

二维条码是用某种特定的几何图形按一定规律在平面(二维方向)上分布的条、空相间的图形来记录数据符号信息。

二维条码示意图

第三章:二维码分类 3-1 二维码分类

二维条码也有许多不同的码制,就码制的编码原理而言,通常分为三种类型:

1.线性堆叠式二维码
2.矩阵式二维码
3.邮政码

线性堆叠式二维码

编码原理:建立在一维条码基础之上,按需要堆积成两行或多行。

线性堆叠式二维码示意图

矩阵式二维码

在一个矩形空间通过黑、白像素在矩阵中的不同分布进行编码。
在矩阵相应元素位置上,用点(方点、圆点或其他形状)的出现表示二进制“1”,点的不出现表示二进制的“0”

矩阵式二维码示意图

邮政码

邮政码通过不同长度的条进行编码,主要用于邮件编码,如:POSTNET、BPO 4-STATE
第四章:二维码优缺点 4-1 二维码优缺点

优点

高密度编码,信息容量大
    (多达1850个大写字母或2710数字或1108个字节或500个汉字)
编码范围广
    可以把图片、文字、指纹,可以数字化的信息都可以进行编码,用条码显示出来
容错能力强
译码可靠性高
可引入加密措施
成本低、易制作、持久耐用

缺点

二维码技术成为手机病毒、钓鱼网站传播的新渠道
信息泄露
第五章:QR Code 5-1 QR Code

目前流行的三大国际标准:

PDF417:不支持中文
DM:专利未公开,需支付专利费用
QR Code:专利公开,支持中文

QR Code比其他二维码相比具有的优势

识读速度快
数据密度大
占用空间小

QR Code是由日本Denso公司于1994年研制的一种矩阵二维码符号吗,全称是Quick Response Code

示意图

JSP生成二维码的方法

借助第三方jar,如zxing和qrcodejar
Javascript,如jquery.qrcode.js
第六章:实例讲解 6-1 实例讲解前准备

zxing地址

https://github.com/zxing/zxing

Maven坐标



    com.google.zxing
    core
    3.3.0



    com.google.zxing
    javase
    3.3.0
6-2 使用zxing生成二维码

代码演示:

package com.myimooc.zxing;

import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

/**
 * 生成 二维码
 * @author ZhangCheng on 2017-06-23
 */
public class CreateQRCode {

    public static void main(String[] args) {
        // 定义图片的宽度和高度
        int width = 300;
        int height = 300;
        // 定义图片的格式
        String format = "png";
        // 定义二维码的内容
        String contents = "www.imooc.com";
        
        Path file = new File("D:/img.png").toPath();
        // 定义二维码的参数
        HashMap hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 设置字符编码
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);// 设置容错等级
        hints.put(EncodeHintType.MARGIN, 2);// 设置边距(默认值5)
        
        // 生成二维码
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height,hints);
            MatrixToImageWriter.writeToPath(bitMatrix, format, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
6-3 使用zxing进行二维码解析

代码演示:

package com.myimooc.zxing;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import javax.imageio.ImageIO;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

/**
 * 读取 二维码
 * @author ZhangCheng on 2017-06-23
 */
public class ReadQRCode {

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void main(String[] args) {
        
        MultiFormatReader formatReader = new MultiFormatReader();
        
        // 定义二维码文件路径
        File file = new File("D:/img.png");
        // 读取图片文件识别为一个图片流
        BufferedImage image;
        try {
            image = ImageIO.read(file);

            BinaryBitmap binaryBitmap = 
                    new BinaryBitmap(
                    new HybridBinarizer(
                    new BufferedImageLuminanceSource(image)));
            // 定义二维码的参数
            HashMap hints = new HashMap();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 设置字符编码
            // 解析二维码
            Result result = formatReader.decode(binaryBitmap,hints);
            
            System.out.println("解析结果:"+result.toString());
            System.out.println("格式类型:"+result.getBarcodeFormat());
            System.out.println("文本内容:"+result.getText());
            
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotFoundException e) {
            e.printStackTrace();
        }
    }

}
6-4 使用QR Code方式生成和解析二维码

QRCode

生成:http://www.swetake.com/qrcode/index-e.html
读取:https://osdn.jp/projects/qrcode/

代码演示

1.生成二维码

package com.myimooc.qrcode;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

import com.swetake.util.Qrcode;

/**
 * 生成 二维码 通过 Qrcode
 * 
 * @author ZhangCheng on 2017-06-23
 */
public class CreateQRCode {

    public static void main(String[] args) throws Exception {

        Qrcode x = new Qrcode();

        // 二维码显示的内容
        String qrData = "www.imooc.com";
        int version = 7;
        int width = 67 + 12 * (version - 1);
        int height = 67 + 12 * (version - 1);
        // 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰
        x.setQrcodeErrorCorrect("M");// 纠错等级
        x.setQrcodeEncodeMode("B");// N代表数字,A代表z-Z,B代表其他字符
        // 设置设置版本号,取值范围1-40,值越大尺寸越大,可存储的信息越大
        x.setQrcodeVersion(version);

        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
        Graphics2D gs = bufferedImage.createGraphics();
        // 设置属性
        gs.setBackground(Color.WHITE);
        gs.setColor(Color.BLACK);
        gs.clearRect(0, 0, width, height);

        // 偏移量
        int pixoff = 2;

        byte[] d = qrData.getBytes("gb2312");
        if (d.length > 0 && d.length < 120) {
            boolean[][] s = x.calQrcode(d);

            for (int i = 0; i < s.length; i++) {
                for (int j = 0; j < s.length; j++) {
                    if (s[j][i]) {
                        gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
                    }
                }
            }
        }
        
        gs.dispose();
        bufferedImage.flush();
        
        ImageIO.write(bufferedImage, "png", new File("D:/qrcode.png"));
    }

}

2.读取二维码时,需实现 QRCodeImage 接口

package com.myimooc.qrcode;

import java.awt.image.BufferedImage;

import jp.sourceforge.qrcode.data.QRCodeImage;

/**
 * 读取 二维码 时,需实现 QRCodeImage 接口
 * 
 * @author ZhangCheng on 2017-06-23
 */
public class MyQRCodeImage implements QRCodeImage{
    
    BufferedImage bufferedImage;
    
    public MyQRCodeImage(BufferedImage bufferedImage){
        this.bufferedImage = bufferedImage;
    }
    
    @Override
    public int getHeight() {
        return bufferedImage.getHeight();
    }

    @Override
    public int getPixel(int arg0, int arg1) {
        return bufferedImage.getRGB(arg0, arg1);
    }

    @Override
    public int getWidth() {
        return bufferedImage.getWidth();
    }
    
    
    
}

3.读取二维码

package com.myimooc.qrcode;

import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

import jp.sourceforge.qrcode.QRCodeDecoder;

/**
 * 读取 二维码 通过 Qrcode
 * 
 * @author ZhangCheng on 2017-06-23
 */
public class ReadQRCode {

    public static void main(String[] args) throws Exception {
        
        File file = new File("D:/qrcode.png");
        
        BufferedImage bufferedImage = ImageIO.read(file);
        
        QRCodeDecoder codeDecoder = new QRCodeDecoder();
        
        String result = new String(codeDecoder.decode(new MyQRCodeImage(bufferedImage)),"gb2312");
        
        System.out.println(result);
    }

}
6-5 jquery-qrcode生成二维码

jquery-qrcode

地址:https://github.com/jeromeetienne/jquery-qrcode

代码演示:





生成二维码







生成的二维码如下:

效果如下:

6-6 其他形式的二维码

二维码还可以这样

6-7 二维码扩展

为什么我们的二维码扫描出来是文本而不是链接?

如何实现二维码扫描安装手机软件?以慕课网为例

如何实现二维码扫描名片?

VCard是标准通信薄基本格式

VCard规范

代码实现

第七章:课程总结 7-1 总结

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/67213.html

相关文章

  • 课网_Java图片验证码》学习总结

    时间:2017年07月09日星期日说明:本文部分内容均来自慕课网。@慕课网:http://www.imooc.com教学源码:无学习源码:https://github.com/zccodere/s... 第一章:概述 1-1 课程概述 主要内容 验证码历史 课程内容 不同方案对比 设计与实现 总结 1-2 验证码历史 验证码历史 无验证码:垃圾骚扰 Luis von Ahn:Captcha 不断...

    haitiancoder 评论0 收藏0
  • 课网_Java实现图片等比例缩略图》学习总结

    摘要:时间年月日星期六说明本文部分内容均来自慕课网。可以更加专注于业务逻辑开发,缩短项目开发周期,提高项目开发速度。 时间:2017年07月15日星期六说明:本文部分内容均来自慕课网。@慕课网:http://www.imooc.com教学源码:无学习源码:https://github.com/zccodere/s... 第一章:课程介绍 1-1 课程介绍 在用户进行信息概略浏览的时候,提供缩...

    raledong 评论0 收藏0
  • 课网_《初识Java微信公众号开发》学习总结

    摘要:时间年月日星期五说明本文部分内容均来自慕课网。本套课程介绍微信公众号开发,主要涉及公众号介绍编辑模式介绍开发模式介绍等。慕课网是垂直的互联网技能免费学习网站。 时间:2017年08月11日星期五说明:本文部分内容均来自慕课网。@慕课网:http://www.imooc.com教学源码:https://github.com/zccodere/s...学习源码:https://github...

    PrototypeZ 评论0 收藏0
  • 课网_Java实现对称加密》学习总结

    时间:2017年4月11日星期二说明:本文部分内容均来自慕课网。@慕课网:http://www.imooc.com教学示例源码:https://github.com/zccodere/s...个人学习源码:https://github.com/zccodere/s... 第一章:对称加密算法DES 1-1 JAVA对称加密算法DES 加密密钥=解密密钥 对称加密算法 初等 DES --3D...

    tomlingtm 评论0 收藏0
  • 课网_《JSON快速入门(Java版)》学习总结

    摘要:时间年月日星期日说明本文部分内容均来自慕课网。慕课网教学示例源码无个人学习源码第一章课程概述课程介绍课程须知本课程面向所有使用语言进行开发的小伙伴。 时间:2017年05月21日星期日说明:本文部分内容均来自慕课网。@慕课网:http://www.imooc.com教学示例源码:无个人学习源码:https://github.com/zccodere/s... 第一章:课程概述 1-1 ...

    shiina 评论0 收藏0

发表评论

0条评论

QLQ

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<