当前位置: 首页 > news >正文

Google zxing 生成带logo的二维码图片

环境准备

  • 开发环境

    • JDK 1.8
    • SpringBoot2.2.1
    • Maven 3.2+
  • 开发工具

    • IntelliJ IDEA
    • smartGit
    • Navicat15

添加maven配置

<dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.4.0</version>
</dependency>
<dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.4.0</version>
</dependency>

创建比特矩阵

先创建比特矩阵,设置默认的宽度、高度、后缀名等等

 private static final String DEFAULT_CHAR_SET = "UTF-8";private static final String DEFAULT_FORMAT_NAME = "JPG";// 二维码宽度
private static final int DEFAULT_QR_CODE_WIDTH = 300;
// 二维码高度
private static final int DEFAULT_QR_CODE_HEIGHT = 300;/*** 创建BitMatrix比特矩阵* @Date 2023/09/24 22:29* @Param contents 二维码里的内容* @Param width 二维码宽度* @param height 二维码高度* @return com.google.zxing.common.BitMatrix*/
public static  BitMatrix createBitMatrix(String contents , int width , int height) throws WriterException, IOException {if (ObjectUtil.isNull(width)) {width = DEFAULT_QR_CODE_WIDTH;}if (ObjectUtil.isNull(height)) {height = DEFAULT_QR_CODE_HEIGHT;}Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 纠错等级L,M,Q,Hhints.put(EncodeHintType.CHARACTER_SET, DEFAULT_CHAR_SET);// 编码utf-8hints.put(EncodeHintType.MARGIN, 1);  // 边距// 创建比特矩阵BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,BarcodeFormat.QR_CODE, width, height, hints);return bitMatrix;}

转换为BufferedImage

创建好比特矩阵后,转换为BufferedImage

 /*** 转换为BufferedImage* @Date 2023/09/24 22:32* @Param [bitMatrix]* @return java.awt.image.BufferedImage*/
public static BufferedImage toBufferedImage(BitMatrix bitMatrix) throws IOException, WriterException {MatrixToImageConfig matrixToImageConfig = new MatrixToImageConfig(0xFF000001, 0xFFFFFFFF);BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix, matrixToImageConfig);return bufferedImage;
}

加上二维码logo

给创建的二维码BufferedImage加上logo

 /*** 给二维码添加logo* @Date 2023/09/24 22:33* @Param [bufferedImage, logoFile]* @return java.awt.image.BufferedImage*/public static BufferedImage addQrCodeLogo(BufferedImage bufferedImage, File logoFile) throws IOException {Graphics2D graphics = bufferedImage.createGraphics();int matrixWidth = bufferedImage.getWidth();int matrixHeigh = bufferedImage.getHeight();// 读取logo图片文件BufferedImage logo = ImageIO.read(logoFile);int logoWidth = logo.getWidth();int logoHeight = logo.getHeight();//  计算logo放置位置int x = bufferedImage.getWidth()  / 5*2;int y = bufferedImage.getHeight() / 5*2;int width = matrixWidth / 5;int height = matrixHeigh / 5;// 开始绘制图片graphics.drawImage(logo, x, y, width, height, null);graphics.drawRoundRect(x, y, logoWidth, logoHeight, 15, 15);graphics.setStroke(new BasicStroke(5.0F, 1, 1));graphics.setColor(Color.white);graphics.drawRect(x, y, logoWidth, logoHeight);graphics.dispose();bufferedImage.flush();return bufferedImage;}

测试

public static void main(String[] args) throws Exception {BufferedImage bufferedImage = toBufferedImage(createBitMatrix("https://blog.csdn.net", 300, 300));ImageIO.write(bufferedImage, "png", new File("D:/qrcode.jpg"));System.out.println(decodeQrCode(bufferedImage));BufferedImage logoQrCode = addQrCodeLogo(bufferedImage, new File("D://logo.png"));ImageIO.write(logoQrCode, "png", new File("D:/logoQrcode.jpg"));}

创建不带logo的二维码图片
在这里插入图片描述
创建带logo的二维码图片
在这里插入图片描述

附录

package com.example.common.util.qrcode;import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;public class QrCodeGenerator {private static final String DEFAULT_CHAR_SET = "UTF-8";private static final String DEFAULT_FORMAT_NAME = "JPG";// 二维码宽度private static final int DEFAULT_QR_CODE_WIDTH = 300;// 二维码高度private static final int DEFAULT_QR_CODE_HEIGHT = 300;/*** 创建BitMatrix比特矩阵* @Date 2023/09/24 22:29* @Param contents 二维码里的内容* @Param width 二维码宽度* @param height 二维码高度* @return com.google.zxing.common.BitMatrix*/public static  BitMatrix createBitMatrix(String contents , int width , int height) throws WriterException, IOException {if (ObjectUtil.isNull(width)) {width = DEFAULT_QR_CODE_WIDTH;}if (ObjectUtil.isNull(height)) {height = DEFAULT_QR_CODE_HEIGHT;}Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 纠错等级L,M,Q,Hhints.put(EncodeHintType.CHARACTER_SET, DEFAULT_CHAR_SET);// 编码utf-8hints.put(EncodeHintType.MARGIN, 1);  // 边距// 创建比特矩阵BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,BarcodeFormat.QR_CODE, width, height, hints);return bitMatrix;}/*** 创建二维码,返回字节数组* @Date 2023/09/24 22:30* @Param contents 二维码里的内容* @Param imageFormat 图片后缀名* @Param width 二维码宽度* @param height 二维码高度* @return byte[]*/public static byte[] createQrCode(String contents , String imageFormat , int width , int height) throws WriterException, IOException {if (StrUtil.isBlank(imageFormat)){imageFormat = DEFAULT_FORMAT_NAME;}BitMatrix bitMatrix = createBitMatrix(contents , width, height);ByteArrayOutputStream os = new ByteArrayOutputStream();MatrixToImageWriter.writeToStream(bitMatrix, imageFormat, os);return os.toByteArray();}/*** 创建二维码,返回base64字符串* @Date 2023/09/24 22:30* @Param contents 二维码里的内容* @Param imageFormat 图片后缀名* @Param width 二维码宽度* @param height 二维码高度* @return byte[]*/public static String createQrCodeBase64(String contents , String imageFormat , int width , int height) throws WriterException, IOException {byte[] bytes =createQrCode(contents , imageFormat , width, height);return Base64.encode(bytes);}/*** 解码二维码* @Date 2023/09/24 22:32* @Param [image]* @return java.lang.String*/public static String decodeQrCode(BufferedImage image) throws Exception {if (image == null) return StrUtil.EMPTY;BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();hints.put(DecodeHintType.CHARACTER_SET, DEFAULT_CHAR_SET);Result result = new MultiFormatReader().decode(bitmap, hints);return result.getText();}/*** 转换为BufferedImage* @Date 2023/09/24 22:32* @Param [bitMatrix]* @return java.awt.image.BufferedImage*/public static BufferedImage toBufferedImage(BitMatrix bitMatrix) throws IOException, WriterException {MatrixToImageConfig matrixToImageConfig = new MatrixToImageConfig(0xFF000001, 0xFFFFFFFF);BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix, matrixToImageConfig);return bufferedImage;}/*** 给二维码添加logo* @Date 2023/09/24 22:33* @Param [bufferedImage, logoFile]* @return java.awt.image.BufferedImage*/public static BufferedImage addQrCodeLogo(BufferedImage bufferedImage, File logoFile) throws IOException {Graphics2D graphics = bufferedImage.createGraphics();int matrixWidth = bufferedImage.getWidth();int matrixHeigh = bufferedImage.getHeight();// 读取logo图片文件BufferedImage logo = ImageIO.read(logoFile);int logoWidth = logo.getWidth();int logoHeight = logo.getHeight();//  计算logo放置位置int x = bufferedImage.getWidth()  / 5*2;int y = bufferedImage.getHeight() / 5*2;int width = matrixWidth / 5;int height = matrixHeigh / 5;// 开始绘制图片graphics.drawImage(logo, x, y, width, height, null);graphics.drawRoundRect(x, y, logoWidth, logoHeight, 15, 15);graphics.setStroke(new BasicStroke(5.0F, 1, 1));graphics.setColor(Color.white);graphics.drawRect(x, y, logoWidth, logoHeight);graphics.dispose();bufferedImage.flush();return bufferedImage;}public static void main(String[] args) throws Exception {BufferedImage bufferedImage = toBufferedImage(createBitMatrix("https://blog.csdn.net", 300, 300));ImageIO.write(bufferedImage, "png", new File("D:/qrcode.jpg"));System.out.println(decodeQrCode(bufferedImage));BufferedImage logoQrCode = addQrCodeLogo(bufferedImage, new File("D://logo.png"));ImageIO.write(logoQrCode, "png", new File("D:/logoQrcode.jpg"));}}
http://www.lryc.cn/news/192709.html

相关文章:

  • 使用Python计算平面多边形间最短距离
  • 【Python】Python语言基础(中)
  • 观察者模式、订阅者发布者模式、vtk中的观察者模式
  • 关于element-ui中,页面上有多个el-table并通过v-if、v-else等控制是否显示时,type=selection勾选框失效或不显示的问题
  • Stewart六自由度正解、逆解计算-C#和Matlab程序
  • C语言 驼峰命名法和下划线命名法
  • 大数据学习(8)-hive压缩
  • [sqoop]hive导入mysql,其中mysql的列存在默认值列
  • Stream流中的常用方法(forEach,filter,map,count,limit,skip,concat)和Stream流的特点
  • 2023大联盟2比赛总结
  • Flutter笔记:电商中文货币显示插件Money Display
  • 腾讯云上创建 对象存储cos
  • 微信小程序生成海报
  • stm32学习笔记:EXIT中断
  • css 块元素、行内元素、行内块元素相互转换
  • 【JUC】多线程基础概述
  • Git 回退代码的两种方法对比
  • Avalonia常用小控件Charts
  • 【Hugging Face】管理 huggingface_hub 缓存系统
  • Python学习基础笔记六十六——对象的方法
  • 建立一个新的高阶数学教授模式,知其然,知其用,知其之所以然,知其所以然
  • AtCoder ABC324G 启发式合并
  • SpringBootCMS漏洞复现分析
  • iOS- flutter flavor 多环境Configurations配置
  • 【PyTorchTensorBoard实战】GPU与CPU的计算速度对比(附代码)
  • npm 常用指令总结
  • 布朗大学发现GPT-4存在新问题,可通过非常见语言绕过限制
  • ESP32网络编程-TCP客户端数据传输
  • 微信小程序入门级
  • 博客文档续更(二)