开源库分享__二维码生成(QRCode2)

news/2024/7/10 19:36:04 标签: 二维码, 开源,

本文由 Luzhuo 编写,请尊重个人劳动成果,转发请保留该信息.
原文: http://blog.csdn.net/Rozol/article/details/52831504


简介:

用于生成各种常见的二维码

导包

拷贝 qrcode2.1.0.jarzxing3.2.1.jar 到项目中, Build Pah 即可使用.

使用

public class EncodeMain {
    private static String contents = "作者:Luzhuo   博客:http://blog.csdn.net/Rozol   Github:https://github.com/lzluzhuo"; // 二维码内容 
    private static int width = 340; // 二维码图片宽度
    private static int height = 340; // 二维码图片高度
    private static File savefile = new File("C:\\我的电脑\\" + File.separator + System.currentTimeMillis() + ".png"); // 保存文件
    private static File basemapfile =  new File("C:\\我的电脑\\logo2.jpg"); // 底图
    private static File logofile = new File("C:\\我的电脑\\logo.jpg"); // logo

    public static void main(String[] args) throws WriterException, IOException {
        // 计算底图的尺寸, 可以根据底图的尺寸计算二维码的大小, 没有该需求则不需要
        int[] size = QRUtils.getImageFilePxSize(basemapfile);
        if(size == null) return;
        width = size[0]; height = size[1];

        // 1. 创建QRCode源数据组
        QRCode qrcode = new QRCode().createQRCode(contents);

        // 2. 样式类处理QRCode源数据组
        // QRStyle qrstyle = new QRStyle(qrcode, QRStyles.Default);
        // QRStyle qrstyle = new QRStyle(qrcode, QRStyles.OnlyBlack);
        // QRStyle qrstyle = new QRStyle(qrcode, QRStyles.DefaultTranslucent);
        // QRStyle qrstyle = new QRStyle(qrcode, QRStyles.DefaultTranslucentBorder);
        QRStyle qrstyle = new QRStyle(qrcode, QRStyles.DefaultPoint);

        // 3. 将QRCode生成图形数据阵
        QRMatrix qrMatrix = new QRMatrix().createMatrix(qrcode, width, height, qrstyle);

        // 4. 将图形数据阵生成图形
        // QRGraphical qrGraphical = new QRGraphical().decode(qrMatrix);
        QRGraphical qrGraphical = new QRGraphical().decode(qrMatrix, basemapfile);

        // 5. 将生成的图形写入文件
        new QRWriteToFile().writeToFile(qrGraphical, savefile);
        // new QRWriteToFile().writeToFile(qrGraphical, savefile, logofile);
    }

}

样式

  • 样式一:QRStyles.Default

    参考代码:
    QRCode qrcode = new QRCode().createQRCode(contents);
    QRStyle qrstyle = new QRStyle(qrcode, QRStyles.Default);
    QRMatrix qrMatrix = new QRMatrix().createMatrix(qrcode, width, height, qrstyle);
    QRGraphical qrGraphical = new QRGraphical().decode(qrMatrix);
    new QRWriteToFile().writeToFile(qrGraphical, savefile);
    
  • 样式二:QRStyles.Default + logo

    参考代码:
    QRCode qrcode = new QRCode().createQRCode(contents);
    QRStyle qrstyle = new QRStyle(qrcode, QRStyles.Default);
    QRMatrix qrMatrix = new QRMatrix().createMatrix(qrcode, width, height, qrstyle);
    QRGraphical qrGraphical = new QRGraphical().decode(qrMatrix);
    new QRWriteToFile().writeToFile(qrGraphical, savefile, logofile);
    
  • 样式三:QRStyles.OnlyBlack

    参考代码:
    QRCode qrcode = new QRCode().createQRCode(contents);
    QRStyle qrstyle = new QRStyle(qrcode, QRStyles.OnlyBlack);
    QRMatrix qrMatrix = new QRMatrix().createMatrix(qrcode, width, height, qrstyle);
    QRGraphical qrGraphical = new QRGraphical().decode(qrMatrix, basemapfile);
    new QRWriteToFile().writeToFile(qrGraphical, savefile);
    
  • 样式四:QRStyles.DefaultTranslucent

    参考代码:
    QRCode qrcode = new QRCode().createQRCode(contents);
    QRStyle qrstyle = new QRStyle(qrcode, QRStyles.DefaultTranslucent);
    QRMatrix qrMatrix = new QRMatrix().createMatrix(qrcode, width, height, qrstyle);
    QRGraphical qrGraphical = new QRGraphical().decode(qrMatrix, basemapfile);
    new QRWriteToFile().writeToFile(qrGraphical, savefile);
    
  • 样式五:QRStyles.DefaultTranslucentBorder

    参考代码:
    QRCode qrcode = new QRCode().createQRCode(contents);
    QRStyle qrstyle = new QRStyle(qrcode, QRStyles.DefaultTranslucentBorder);
    QRMatrix qrMatrix = new QRMatrix().createMatrix(qrcode, width, height, qrstyle);
    QRGraphical qrGraphical = new QRGraphical().decode(qrMatrix, basemapfile);
    new QRWriteToFile().writeToFile(qrGraphical, savefile);
    
  • 样式六:QRStyles.DefaultPoint

    参考代码:
    QRCode qrcode = new QRCode().createQRCode(contents);
    QRStyle qrstyle = new QRStyle(qrcode, QRStyles.DefaultPoint);
    QRMatrix qrMatrix = new QRMatrix().createMatrix(qrcode, width, height, qrstyle);
    QRGraphical qrGraphical = new QRGraphical().decode(qrMatrix, basemapfile);
    new QRWriteToFile().writeToFile(qrGraphical, savefile);
    

其他

可以将生成的二维码使用一些滤镜算法, 或者使用PS制作出精美的二维码(本主要用于生成而非美化):

Github: https://github.com/LZLuzhuo/QRCode2

版本更新

1.0

  1. QRCode2.1.0.jar 是版本1.0, 而不是2.1.0
  2. 第一版, 没啥可说明的

http://www.niftyadmin.cn/n/996720.html

相关文章

【源资讯 第11期】Torrent 巨人倒下后我们还能怎么玩?

为什么80%的码农都做不了架构师?>>> 东方财富Choice数据公布了中国上市公司市值 500 强:腾讯登顶,阿里第3,2016年,腾讯控股市值16081亿元,反超工商银行,排名第一。2016年&#xff0…

服务器芯片多线程有什么用,云服务器cpu是1核有多少线程

云服务器cpu是1核有多少线程 内容精选换一换磁盘有如下几种状态,每种状态的意义及支持的操作如表1所示。当磁盘状态为错误、删除失败、扩容失败,您可以参考出现错误或失败的磁盘该如何处理。当您发现云服务器的运行速度变慢或云服务器突然出现网络断开的…

小程序开发系列(三)数据交互与渲染

微信小程序的api中提供了网络交互的api,我们只要调用即可和后端进行数据交互,该api为wx.request.,具体代码如下。 //list.js //获取应用实例 var app getApp() Page({data: {list:[],hiddenLoading: true,url: },loadList: function () {var…

OSChina 周六乱弹 ——你们猜狗的舌头有多长

2019独角兽企业重金招聘Python工程师标准>>> 【今日歌曲】 Lailysh :  #毎日の歌# Its only the fairy tale — 宮村優子 看见评论,感觉喜感莫名。 英强 :这首歌听了有接近十年了,第一次听的时候,完全没意识到是英文…

《CCNP ROUTE 300-101认证考试指南》——8.2节 LSA和OSPF链路状态数据库

本节书摘来自异步社区《CCNP ROUTE 300-101认证考试指南》一书中的第8章,第8.2节 LSA和OSPF链路状态数据库,作者 【美】Kevin Wallace(凯文 华莱士),更多章节内容可以访问云栖社区“异步社区”公众号查看 8.2 LSA和OSP…

Python3 (入门2) 数据结构

Python3 (入门2) 数据结构 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog.csdn.net/rozol/article/details/68938984 以下代码以Python3.6.1为例 Less is more! #!/usr/bin/env python # codingutf-8 __author__ Luzhuo __date__ 2017/5/3 # datastructure.py …

meta 常用标签总结

声明:并非原创     meta元素工有3个可选属性(http-equiv、name、scheme)和一个必选属性(content),content定义与http-equiv或name属性相关的元信息 可选属性: 必选属性: name属性&#xff1…

《AutoCAD 2013中文版从入门到精通》——2.2 圆类命令

本节书摘来自异步社区《AutoCAD 2013中文版从入门到精通》一书中的第2章,第2.2节,作者: 李谨 , 康士廷 更多章节内容可以访问云栖社区“异步社区”公众号查看。 2.2 圆类命令 AutoCAD 2013中文版从入门到精通圆类命令主要包括“圆”、“圆弧”、“圆环”…