zoukankan      html  css  js  c++  java
  • 生成二维码的三种方式

    一:二维码的概念
    二维条码(2-dimemsional bar code)是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的图形

    二: 二维码的分类
    通常分为三种类型:
    1.0 线性堆叠式二维码
    编码原理:建立在一维条码基础之上,按需要堆积成两行或者多行
    2.0 矩阵式二维码
    在一个矩阵空间通过黑白像素在矩阵中的不同分布进行编码,用点表示二进制的1,空白表示二进制的0
    3.0 邮政码
    通过不同长度的条进行编码,主要用于邮政编码,如POSTNET、BPO4-STATE

    三: 二维码的优缺点
    1.0 高密度编码,信息容量大
    2.0 编码范围广
    3.0 容错能力强
    4.0 译码可靠性高
    5.0 可引入加密措施
    6.0 成本低,易制作,持久耐用

    缺点:二维码成为手机病毒,钓鱼网站传播的新渠道、信息泄露

    四:QR Code
    1.0 目前流行的三大国际标准
    PDF417 : 不支持中文
    DM : 专利未公开,需支付专利费用
    QR Code : 专利公开,支持中文
    2.0 两种生成方式
    01 : 借助第三方的jar包,如zxing和qrcodejar
    02 : javascript,如jQuery.qrcode.js

    五 :实例讲解
    1.0: zxing (http://github.com/zxing/)
    1.1 : 进入该链接下载源码,新建java项目,导入core、javase包中的com文件,export为jar文件
    1.2: public class CreatQRCode {

    public static void main(String[] args) {

    int width = 300;
    int height = 300;
    String formart = "png";
    String content = "www.gongzifusu.com";

    //定义二维码的参数
    HashMap hints = new HashMap<>();
    hints.put(EncodeHintType.CHARACTER_SET,"utf-8"); //编码格式
    hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M); //容错等级
    hints.put(EncodeHintType.MARGIN,2); //边距

    //生成二维码
    try {

    BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
    Path file = new File("C:/Users/Administrator/Desktop/QRCode/img.png").toPath();
    MatrixToImageWriter.writeToPath(bitMatrix, formart, file);

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    }
    1.3: 解析二维码
    public static void main(String[] args) {

    try {
    MultiFormatReader multiFormatReader = new MultiFormatReader();
    File file = new File("C:/Users/Administrator/Desktop/QRCode/img.png");
    BufferedImage bufferedImage = ImageIO.read(file);
    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));

    //定义二维码的参数
    HashMap hints = new HashMap<>();
    hints.put(EncodeHintType.CHARACTER_SET,"utf-8"); //编码格式

    Result result = multiFormatReader.decode(binaryBitmap,hints);
    System.out.println("解析结果: "+result.toString());
    System.out.println("二维码格式: "+result.getBarcodeFormat());
    System.out.println("二维码文本内容: "+result.getText());
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    2.0 QRCode方式生成二维码(http://www.swetake.com/qrcode/index-e.html)/(https://osdn.jp/projects/qrcode/)
    2.1 :下载相关源码文件,创建java项目,导入jar包,
    2.2 : 生成二维码
    public class CreateQRCodeByQC {

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

    Qrcode x = new Qrcode();
    x.setQrcodeEncodeMode('M'); //纠错等级
    x.setQrcodeEncodeMode('B'); //N代表数字,A代表a_Z,B代表其他符号
    x.setQrcodeVersion(7); //版本
    String qrDate = "天降大任于斯人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,增益其所不能,所以动心忍性...";

    int width = 67 + 12 * (x.getQrcodeVersion()-1);
    int height = 67 + 12 * (x.getQrcodeVersion()-1);
    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 = qrDate.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("C:/Users/Administrator/Desktop/QRCode/qrcode.png"));

    }

    }
    2.3 解析二维码
    public class ReadQRCodeByQR {

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

    File file = new File("C:/Users/Administrator/Desktop/QRCode/qrcode.png");

    BufferedImage bufferedImage = ImageIO.read(file);
    QRCodeDecoder qrCodeDecoder = new QRCodeDecoder();
    String result = new String(qrCodeDecoder.decode(new MyQRCodeImage(bufferedImage)),"gb2312");

    System.out.println(result);

    }

    }

    public class MyQRCodeImage implements QRCodeImage{

    BufferedImage bufferedImage;


    public MyQRCodeImage(BufferedImage bufferedImage) {
    super();
    this.bufferedImage = bufferedImage;
    }

    @Override
    public int getHeight() {

    // TODO Auto-generated method stub
    return bufferedImage.getHeight();
    }

    @Override
    public int getPixel(int arg0, int arg1) {

    // TODO Auto-generated method stub
    return bufferedImage.getRGB(arg0, arg1);
    }

    @Override
    public int getWidth() {

    // TODO Auto-generated method stub
    return bufferedImage.getWidth();
    }

    }

    3.0 jQuery-qrcode(https://github.com/jeromeetienne/jquery-qrcode)
    3.1 新建web项目,导入jquery.min.js、jquery.qrcode.min.js
    3.2 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>页面生成二维码</title>
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery.min.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery.qrcode.min.js"></script>
    </head>
    <body>
    生成二维码如下:<br>
    <div id="qrcode"></div>

    <script type="text/javascript">
    jQuery('#qrcode').qrcode("www.STK.com");
    </script>
    </body>
    </html>

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    php No input file specified 错误提示
    yii 隐藏index.php
    phpstudy 配置 sqlserver
    xdebug3 配置信息
    composer 更新
    mysql 命令行 导入大型文件
    使用 DBExportDoc V1.0 For MySQL 导出数据库表结构说明文档
    聚合数据接口调用实例
    servlet
    JDBC
  • 原文地址:https://www.cnblogs.com/juniorjava/p/7760886.html
Copyright © 2011-2022 走看看