zxing生成二维码添加图片和文字

本文展示了如何利用ZXing库在二维码中添加图片和文字,并提供了生成及读取二维码的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 直接上代码,以后有时间再添加说明

二维码的基本信息,包括要二维码内包含的文字,嵌入的图片,加到二维码下边的文字

/**
 * 
 */
package com.tong.util.code;

import java.io.File;

/**
 * @author zxm
 *
 */
public class CodeModel {
	private String contents;
	private int width = 400;
	private int height = 400;
	private String format = "gif";
	private String character_set = "utf-8";
	private int fontSize = 12;
	private File logoFile;
	private float logoRatio = 0.20f;
	private String desc;
	private int whiteWidth;//白边的宽度
	private int[] bottomStart;//二维码最下边的开始坐标
	private int[] bottomEnd;//二维码最下边的结束坐标

	public String getContents() {
		return contents;
	}

	public void setContents(String contents) {
		this.contents = contents;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public String getCharacter_set() {
		return character_set;
	}

	public void setCharacter_set(String character_set) {
		this.character_set = character_set;
	}

	public int getFontSize() {
		return fontSize;
	}

	public void setFontSize(int fontSize) {
		this.fontSize = fontSize;
	}

	public File getLogoFile() {
		return logoFile;
	}

	public void setLogoFile(File logoFile) {
		this.logoFile = logoFile;
	}

	public float getLogoRatio() {
		return logoRatio;
	}

	public void setLogoRatio(float logoRatio) {
		this.logoRatio = logoRatio;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public int getWhiteWidth() {
		return whiteWidth;
	}

	public void setWhiteWidth(int whiteWidth) {
		this.whiteWidth = whiteWidth;
	}

	public int[] getBottomStart() {
		return bottomStart;
	}

	public void setBottomStart(int[] bottomStart) {
		this.bottomStart = bottomStart;
	}

	public int[] getBottomEnd() {
		return bottomEnd;
	}

	public void setBottomEnd(int[] bottomEnd) {
		this.bottomEnd = bottomEnd;
	}
}

 生成二维码

/**
 * 
 */
package com.tong.util.code;

import java.awt.Color;
impor
### 如何使用 ZXing 生成二维码 #### 使用 Java ZXing生成二维码 为了在项目中集成 ZXing生成二维码,首先需要引入 ZXing 的依赖项。对于 Maven 用户,在 `pom.xml` 文件中添加如下依赖: ```xml <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> ``` 接着定义一个名为 `generateQRCodeImage` 的方法来创建二维码图像。此函数接收四个参数:要编码的文字内容 (`text`)、期望的宽度 (`width`)、高度 (`height`) 及保存位置 (`filePath`)。 ```java 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 java.io.IOException; import java.nio.file.FileSystems; import java.util.HashMap; import java.util.Map; public class QRCodeGenerator { private static final String QR_CODE_IMAGE_PATH = "./MyQRCode.png"; public static void main(String[] args) { String data = "https://www.example.com"; int width = 300; int height = 300; try { generateQRCodeImage(data, width, height); System.out.println("QR Code has been generated successfully."); } catch (WriterException | IOException e) { System.err.println(e.getMessage()); } } private static void generateQRCodeImage(String text, int width, int height) throws WriterException, IOException { Map<EncodeHintType, Integer> hints = new HashMap<>(); hints.put(EncodeHintType.MARGIN, 1); // 设置边距 QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height, hints); Path path = FileSystems.getDefault().getPath(QR_CODE_IMAGE_PATH); MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path); } } ``` 上述代码展示了如何利用 ZXing 库中的 `QRCodeWriter` 类构建位矩阵(`BitMatrix`) 并通过 `MatrixToImageWriter` 将其转换成 PNG 图像文件[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值