一、加入maven依赖

  1. <!-- 谷歌zxing 二维码 -->
  2. <dependency>
  3. <groupId>com.google.zxing</groupId>
  4. <artifactId>core</artifactId>
  5. <version>3.3.3</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.google.zxing</groupId>
  9. <artifactId>javase</artifactId>
  10. <version>3.3.3</version>
  11. </dependency>

二、工具类代码

  1. package com.example.demo.utils;
  2.  
  3. import com.google.zxing.*;
  4. import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
  5. import com.google.zxing.common.BitMatrix;
  6. import com.google.zxing.common.HybridBinarizer;
  7. import com.google.zxing.qrcode.QRCodeReader;
  8. import com.google.zxing.qrcode.QRCodeWriter;
  9. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
  10.  
  11. import javax.imageio.ImageIO;
  12. import java.awt.*;
  13. import java.awt.image.BufferedImage;
  14. import java.io.*;
  15. import java.util.Hashtable;
  16.  
  17. /**
  18. * @author zsh
  19. * @company wlgzs
  20. * @create 2019-03-10 15:17
  21. * @Describe 二维码生成和读的工具类
  22. */
  23. public class QrCodeCreateUtil {
  24. /**
  25. * 生成包含字符串信息的二维码图片
  26. * @param outputStream 文件输出流路径
  27. * @param content 二维码携带信息
  28. * @param qrCodeSize 二维码图片大小
  29. * @param imageFormat 二维码的格式
  30. * @throws WriterException
  31. * @throws IOException
  32. */
  33. public static boolean createQrCode(OutputStream outputStream, String content, int qrCodeSize, String imageFormat) throws WriterException, IOException{
  34. //设置二维码纠错级别MAP
  35. Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
  36. hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 矫错级别
  37. QRCodeWriter qrCodeWriter = new QRCodeWriter();
  38. //创建比特矩阵(位矩阵)的QR码编码的字符串
  39. BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
  40. // 使BufferedImage勾画QRCode (matrixWidth 是行二维码像素点)
  41. int matrixWidth = byteMatrix.getWidth();
  42. BufferedImage image = new BufferedImage(matrixWidth-200, matrixWidth-200, BufferedImage.TYPE_INT_RGB);
  43. image.createGraphics();
  44. Graphics2D graphics = (Graphics2D) image.getGraphics();
  45. graphics.setColor(Color.WHITE);
  46. graphics.fillRect(0, 0, matrixWidth, matrixWidth);
  47. // 使用比特矩阵画并保存图像
  48. graphics.setColor(Color.BLACK);
  49. for (int i = 0; i < matrixWidth; i++){
  50. for (int j = 0; j < matrixWidth; j++){
  51. if (byteMatrix.get(i, j)){
  52. graphics.fillRect(i-100, j-100, 1, 1);
  53. }
  54. }
  55. }
  56. return ImageIO.write(image, imageFormat, outputStream);
  57. }
  58.  
  59. /**
  60. * 读二维码并输出携带的信息
  61. */
  62. public static void readQrCode(InputStream inputStream) throws IOException{
  63. //从输入流中获取字符串信息
  64. BufferedImage image = ImageIO.read(inputStream);
  65. //将图像转换为二进制位图源
  66. LuminanceSource source = new BufferedImageLuminanceSource(image);
  67. BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
  68. QRCodeReader reader = new QRCodeReader();
  69. Result result = null ;
  70. try {
  71. result = reader.decode(bitmap);
  72. } catch (ReaderException e) {
  73. e.printStackTrace();
  74. }
  75. System.out.println(result.getText());
  76. }
  77. /**
  78. * 测试代码
  79. * @throws WriterException
  80. */
  81. public static void main(String[] args) throws IOException, WriterException {
  82.  
  83. createQrCode(new FileOutputStream(new File("d:\\qrcode.jpg")),"WE1231238239128sASDASDSADSDWEWWREWRERWSDFDFSDSDF123123123123213123",900,"JPEG");
  84. readQrCode(new FileInputStream(new File("d:\\qrcode.jpg")));
  85. }
  86. }

效果图:

谷歌zxing 二维码生成工具的更多相关文章

  1. 二维码生成工具类java版

    注意:这里我不提供所需jar包的路径,我会把所有引用的jar包显示出来,大家自行Google package com.net.util; import java.awt.BasicStroke; im ...

  2. vue项目条形码和二维码生成工具试用

    项目开发需要,优惠券分不同类型,简单的使用id生成条形码供店铺使用,麻烦点的需要多个字段的就需要使用二维码来展示了,对应的效果如下 条形码(一维码)使用工具code128 需引入code128.js ...

  3. java二维码生成工具

    import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.ut ...

  4. ZXing二维码生成在Unity3D中出错,数组超出界限的解决办法

    错误截图: IndexOutOfRangeException: Array index is out of range.ZXing.Color32Renderer.Render (ZXing.Comm ...

  5. Google Zxing 二维码生成与解析

    生成二维码的开源项目可谓是琳琅满目,SwetakeQRCode.BarCode4j.Zxing...... 前端有JQuery-qrcode,同样能实现生成二维码. 选择Zxing的原因可能是对 Go ...

  6. Java 二维码生成工具类

    /** * 二维码 工具 * * @author Rubekid * */ public class QRcodeUtils { /** * 默认version */ public static fi ...

  7. 开发ASP.NET MVC 开发名片二维码生成工具 (原创)

    在网上找了很多,都只能生成网址,不能生成名片二维码,于是自己动手. 第一步,写视图界面,主要代码如下: <script type="text/javascript"> ...

  8. 谷歌api 二维码生成 实例

    谷歌提供的二维码生成器接口,非常实用!分享给大家 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  9. 二维码生成工具——QRCode

    下载QRCode的源代码:https://github.com/davidshimjs/qrcodejs 引入项目中:<script type="text/javascript&quo ...

随机推荐

  1. XML反序列化遇到数字型节点值为空导致反序列化异常

    实体类: [XmlRoot("stream")] public class _30320DuisiFukuanQueryResponseModel : ResponseModelB ...

  2. 第四章 HTML5概述

    HTML5概述1.HTML5优势:解决跨浏览器问题:部分代替原来的js更明确地语义支持:不再单纯使用div增强WEB应用程序地功能:拖拽API等 2.HTML5语法改变标签不再区分大小写元素可以省略结 ...

  3. c# 使用MS SqlServer,连接成功,但是还报异常A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0。。。。

    c# 使用MS SqlServer,连接成功,但是还报异常A connection was successfully established with the server, but then an ...

  4. train_val.prototxt文件和deploy.prototxt文件开头的区别

    1.开头不同 对train_val.prototxt文件来说,开头部分定义训练和测试的网络及参数 对deploy.prototxt文件来说,开头部分定义实际运用场景的配置文件,其参数不定义数据来源,仅 ...

  5. Unity shader学习之Forward Rendering Path

    Forward rendering path shader如下: // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObje ...

  6. django 设置不带后缀的访问路径

    在urls.py 设置空路径,并指向对应的html文件 url(r'^$', views.index),

  7. 使用js调用js

    直接上源码: <div class="choose"> choose a mode<br> <hr> <button type=" ...

  8. Linux环境变量和本地变量

    每一种编程语言中,我们都会碰到变量的作用域的问题.(比如在函数中定义的变量在函数外不能使用的) BASH 中也有类似的问题,局部变量和环境变量(全局变量). 局部变量是普通的变量,仅在创建它的Shel ...

  9. flask 文件上传(单文件上传、多文件上传)

    文件上传 在HTML中,渲染一个文件上传字段只需要将<input>标签的type属性设为file,即<input type=”file”>. 这会在浏览器中渲染成一个文件上传字 ...

  10. 算法提高 P0101

    一个水分子的质量是3.0*10-23克,一夸脱水的质量是950克.写一个程序输入水的夸脱数n(0 <= n <= 1e10),然后输出水分子的总数.输入 109.43输出 3.465283 ...