一、简单二维码生成

1.1、依赖

<!--二维码工具-->
<dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.4.1</version>
</dependency>

1.2、二维码生成工具

QRCodeUtil

package com.example.demo.utlis;import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;public class QRCodeUtil {private static Log log = LogFactory.getLog(QRCodeUtil.class);private static final int BLACK = 0xFF000000;private static final int WHITE = 0xFFFFFFFF;private static BufferedImage toBufferedImage(BitMatrix matrix) {int width = matrix.getWidth();int height = matrix.getHeight();BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);for (int x = 0; x < width; x++) {for (int y = 0; y < height; y++) {image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);}}return image;}private static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {BufferedImage image = toBufferedImage(matrix);if (!ImageIO.write(image, format, file)) {throw new IOException("Could not write an image of format " + format + " to " + file);}}/** 将内容contents生成长宽均为width的图片,图片路径由imgPath指定*/public static File getQRCodeImge(String contents, int width, String imgPath) {return getQRCodeImge(contents, width, width, imgPath);}/** 将内容contents生成长为width,宽为width的图片,图片路径由imgPath指定*/public static File getQRCodeImge(String contents, int width, int height, String imgPath) {try {Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);hints.put(EncodeHintType.CHARACTER_SET, "UTF8");BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);File imageFile = new File(imgPath);writeToFile(bitMatrix, "png", imageFile);return imageFile;} catch (Exception e) {log.error("create QR code error!", e);return null;}}
}

1.3、测试

@Test
void code() {// 需要修改为运行机器上的路径,就是本地路径String path = "E:\\java";String qrPath = String.format(path + "\\qr-%s.png", "111111111111111");String qrFileName = String.format("qr-%s.png", "111111111111111");QRCodeUtil.getQRCodeImge("https://blog.csdn.net/qq_45660133", 256, qrPath);File targetFile = new File(path, qrFileName);
}

二、生成二维码带logo

2.1、依赖

<!--二维码工具-->
<dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.4.1</version>
</dependency>
<dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.3.3</version>
</dependency>

2.2、Test测试类

public class Test{public static void main(String[] args) {try {QREncode();QRReader(new File("D:\\zxing1.gif"));} catch (WriterException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (NotFoundException e) {e.printStackTrace();}}/*** 生成二维码*/public static void QREncode() throws WriterException, IOException {String content = "个人博客:https://blog.csdn.net/qq_45660133";//二维码内容int width = 200; // 图像宽度int height = 200; // 图像高度String format = "gif";// 图像类型Map<EncodeHintType, Object> hints = new HashMap<>();//内容编码格式hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");// 指定纠错等级hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//设置二维码边的空度,非负数hints.put(EncodeHintType.MARGIN, 1);BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);MatrixToImageWriter.writeToPath(bitMatrix, format, new File("D:\\zxing.gif").toPath());// 输出原图片MatrixToImageConfig matrixToImageConfig = new MatrixToImageConfig(0xFF000001, 0xFFFFFFFF);/*问题:生成二维码正常,生成带logo的二维码logo变成黑白原因:MatrixToImageConfig默认黑白,需要设置BLACK、WHITE解决:https://ququjioulai.iteye.com/blog/2254382*/BufferedImage bufferedImage = LogoMatrix(MatrixToImageWriter.toBufferedImage(bitMatrix,matrixToImageConfig), new File("D:\\logo.png"));
//        BufferedImage bufferedImage = LogoMatrix(toBufferedImage(bitMatrix), new File("D:\\logo.png"));ImageIO.write(bufferedImage, "gif", new File("D:\\zxing1.gif"));//输出带logo图片System.out.println("输出成功.");}/*** 识别二维码*/public static void QRReader(File file) throws IOException, NotFoundException {MultiFormatReader formatReader = new MultiFormatReader();//读取指定的二维码文件BufferedImage bufferedImage =ImageIO.read(file);BinaryBitmap binaryBitmap= new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));//定义二维码参数Map  hints= new HashMap<>();hints.put(EncodeHintType.CHARACTER_SET, "utf-8");com.google.zxing.Result result = formatReader.decode(binaryBitmap, hints);//输出相关的二维码信息System.out.println("解析结果:"+result.toString());System.out.println("二维码格式类型:"+result.getBarcodeFormat());System.out.println("二维码文本内容:"+result.getText());bufferedImage.flush();}/*** 二维码添加logo* @param matrixImage 源二维码图片* @param logoFile logo图片* @return 返回带有logo的二维码图片*/public static BufferedImage LogoMatrix(BufferedImage matrixImage, File logoFile) throws IOException{/*** 读取二维码图片,并构建绘图对象*/Graphics2D g2 = matrixImage.createGraphics();int matrixWidth = matrixImage.getWidth();int matrixHeigh = matrixImage.getHeight();/*** 读取Logo图片*/BufferedImage logo = ImageIO.read(logoFile);//开始绘制图片g2.drawImage(logo,matrixWidth/5*2,matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5, null);//绘制BasicStroke stroke = new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);g2.setStroke(stroke);// 设置笔画对象//指定弧度的圆角矩形RoundRectangle2D.Float round = new RoundRectangle2D.Float(matrixWidth/5*2, matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5,20,20);g2.setColor(Color.white);g2.draw(round);// 绘制圆弧矩形//设置logo 有一道灰色边框BasicStroke stroke2 = new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);g2.setStroke(stroke2);// 设置笔画对象RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(matrixWidth/5*2+2, matrixHeigh/5*2+2, matrixWidth/5-4, matrixHeigh/5-4,20,20);g2.setColor(new Color(128,128,128));g2.draw(round2);// 绘制圆弧矩形g2.dispose();matrixImage.flush() ;return matrixImage ;}
}

SpringBoot利用ZXing工具来生成二维码(简单)相关推荐

  1. hutool工具类生成二维码案例

    hutool工具类生成二维码案例 一.环境: 添加hutool工具类依赖,hutool生成二维码是利用Google的zixing,而且不是强依赖,所以还需引入zxing依赖 <dependenc ...

  2. 基于ZXing Android实现生成二维码图片和相机扫描二维码图片即时解码的功能

    NextQRCode ZXing开源库的精简版 **基于ZXing Android实现生成二维码图片和相机扫描二维码图片即时解码的功能 原文博客 附源码下载地址** 与原ZXingMini项目对比 N ...

  3. Zxing与 Zbar生成二维码最简单的实现方式

    Zxing与 Zbar生成二维码最简单的实现方式 导入依赖 implementation 'cn.bingoogolapple:bga-qrcode-zbar:1.2.1' 或者 implementa ...

  4. SpringBoot Zxing _ Java 生成二维码(可内嵌图片)

    前提 jdk 要求:1.8:会 SpringBoot.Maven: 以下代码可以直接复制粘贴到项目中,可以直接使用~ 一.pom 准备 <?xml version="1.0" ...

  5. SpringBoot系列(22):Java生成二维码的几种实现方式(基于Spring Boot)

    原文链接:http://www.fightjava.com/web/index/blog/article/82 在一些企业级应用系统中,有时候需要为产品或者商品生成特定的专属二维码,以供一些硬件设备或 ...

  6. Java:利用qrcode工具类对二维码进行解析或生成

    1.引用jar包 <dependency><groupId>com.github.binarywang</groupId><artifactId>qrc ...

  7. 用Java写一个工具类生成二维码

    首先需要在pom.xml里添加zxing依赖 <dependency><groupId>com.google.zxing</groupId><artifact ...

  8. springboot微信支付pc页面生成二维码

    微信支付开发文档:https://pay.weixin.qq.com/wiki/doc/api/index.html 咱们这次采用的是Native支付 Native支付使用场景:用户打开"微 ...

  9. HTML利用当前网页地址生成二维码

    二维码分享扫描后进入对应网址 可以说非常的方便 今天就带大家看一种实现思路 首先,这个功能需要一个引入js依赖文件 大家可以直接下载我上传的资源 下载资源 下载后在html中引入这个 qrcode.j ...

最新文章

  1. 如何使用matlab的siso,MIMO-SISO-MATLAB-program 和 的 仿真源代码,非常详细,可以参考学习 267万源代码下载- www.pudn.com...
  2. leetcode004 Median_of_Two_Sorted_Arrays.py
  3. 清华大学施路平:双脑驱动的人工通用智能
  4. wtl单文档选项_Vue3 文档阅读 —— TypeScript 支持
  5. 如何直观地解释 back propagation 算法?
  6. AR的一些常见的操作
  7. DPDK网络处理模块划分
  8. MapReduce异常
  9. 《张居正》—— 读后总结
  10. http重定向到https
  11. 实际测试中,经常发现摄像头断线几分钟
  12. Vue后台管理通用模板
  13. 【转载】CC控制服务的设计和侦测方法综述
  14. qq邮箱如何在win10邮箱连接到服务器,win10自带邮箱如何使用?win10自带邮箱如何同步qq邮箱邮件?...
  15. 倍福--232/485通信
  16. mysql和knexjs,与KnexJS、TypeScript和Mariadb的事务问题
  17. ARM服务器编译安装ClickHouse
  18. Android 11.0 12.0蓝牙遥控器确认键弹不出输入法的解决方法
  19. 第六次网页前端培训(JavaScript)
  20. 小旋风asp服务器出错

热门文章

  1. 实际的机械臂控制(9)Moveit单独控制机械臂末端在XYZ三个轴的平移和旋转(基于python)
  2. sql中的coalesce
  3. 中国十大金牌游戏策划
  4. 设计师眼中的旅游LOGO设计——以小见大,一眼知世界
  5. Zookeeper实现服务注册发现
  6. 文章发表前的最后一步:仔细审查校对样本
  7. 上网使用随身wifi与路由器有何不同?
  8. 基于E-PUCK 2.0多智能体自主协同 高频投影定位系统
  9. 学计算机的演员,南开大学计算机系到演员 张桐回顾“不安分”的青春
  10. 中西入门哲学史差异记录