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

java 根据URL链接生成二维码中间添加logo

要在 Java 中根据 URL 生成二维码并在二维码中间添加 logo,可以使用 ZXing 库来生成二维码,并使用图像处理库(如 Java 的 BufferedImage)来添加 logo。以下是详细的步骤和示例代码:
步骤
生成二维码:使用 ZXing 库生成二维码。
加载 logo 图片:加载需要添加到二维码中间的 logo 图片。
合并图片:将 logo 图片合并到二维码图片中。
示例代码
首先,确保你已经添加了 ZXing 库的依赖。如果你使用 Maven,可以在 pom.xml 文件中添加以下依赖:

<dependencies><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.4.1</version></dependency>
</dependencies>

接下来是生成二维码并添加 logo 的完整示例代码:

package com.ruoyi.common.utils.file;import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;/*** @author biypm* @version 1.0* @title* @date 2024/10/5/周六 10:24*/
public class QRCodeUtils {public static void main(String[] args) {String url = "https://www.baidu.com";int width = 300;int height = 300;String format = "png";String logoPath = System.getProperty("user.dir") + "src/assets/logo/logo.jpg"; // 你的 logo 图片路径try {BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, width, height, getHints());BufferedImage qrCodeImage = matrixToImage(bitMatrix);BufferedImage logoImage = ImageIO.read(new File(logoPath));BufferedImage finalImage = addLogoToQRCode(qrCodeImage, logoImage);ImageIO.write(finalImage, format, new File("qrcode_with_logo.png"));} catch (WriterException | IOException e) {e.printStackTrace();}}private static Map<EncodeHintType, ErrorCorrectionLevel> getHints() {Map<EncodeHintType, ErrorCorrectionLevel> hints = new HashMap<>();hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);return hints;}private static BufferedImage matrixToImage(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) ? Color.BLACK.getRGB() : Color.WHITE.getRGB());}}return image;}private static BufferedImage addLogoToQRCode(BufferedImage qrCodeImage, BufferedImage logoImage) {if (qrCodeImage == null || logoImage == null) {throw new IllegalArgumentException("qrCodeImage and logoImage must not be null");}int qrWidth = qrCodeImage.getWidth();int qrHeight = qrCodeImage.getHeight();// 减小Logo大小int newLogoWidth = qrWidth / 8;int newLogoHeight = qrHeight / 8;BufferedImage resizedLogo = new BufferedImage(newLogoWidth, newLogoHeight, BufferedImage.TYPE_INT_ARGB);Graphics2D g2 = resizedLogo.createGraphics();g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);g2.drawImage(logoImage, 0, 0, newLogoWidth, newLogoHeight, null);g2.dispose();// 创建最终的图像BufferedImage finalImage = new BufferedImage(qrWidth, qrHeight, BufferedImage.TYPE_INT_ARGB);Graphics2D g = finalImage.createGraphics();g.drawImage(qrCodeImage, 0, 0, null);// 计算Logo在二维码中的位置int x = (qrWidth - newLogoWidth) / 2;int y = (qrHeight - newLogoHeight) / 2;// 绘制Logo到二维码中心g.drawImage(resizedLogo, x, y, null);// 设置透明度g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.3f));  // 降低透明度// 绘制圆角矩形边框g.setColor(Color.WHITE);g.setStroke(new BasicStroke(3f));  // 减小边框宽度g.drawRoundRect(x, y, newLogoWidth, newLogoHeight, 15, 15);  // 减小圆角半径g.dispose();return finalImage;
}}

http://www.mrgr.cn/news/43182.html

相关文章:

  • 【C++打怪之路Lv7】-- 模板初阶
  • Linux基于CentOS学习【进程状态】【进程优先级】【调度与切换】【进程挂起】【进程饥饿】
  • 20241005给荣品RD-RK3588-AHD开发板刷Rockchip原厂的Android12时使用iperf3测网速
  • Java Supplier和Consumer接口
  • Arthas(阿尔萨斯)
  • 企业数字化转型中的常见挑战与TOGAF解决方案:从架构理论到实践的指导
  • 【WSL】解决‘wsl:检测到localhost代理配置,但未镜像到WSL‘问题
  • 信号用wire类型还是reg类型定义
  • PostgreSQL 字段使用pglz压缩测试
  • C语言之文件操作
  • 插画共享系统小程序的设计
  • Redis基础二(spring整合redis)
  • GEE教程:MODIS/006/MOD16A2数据计算蒸散发数据的时序图表和下载
  • Pikachu-xxe-xxe漏洞
  • Nacos理论知识+应用案例+高级特性剖析
  • 字符串数学专题
  • 华为 HCIP-Datacom H12-821 题库 (31)
  • Three.js基础内容(一)
  • 数据库习题简例
  • 【JavaEE】JVM