Java——二维码

使用zxing生成二维码

引入zxing依赖库:

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

生成二维码:

@Test
public void encode() {
	final int width = 300;
	final int height = 300;
	final String format = "png";
	String content = "com.tim.test.qrcode";
	
	final Map<EncodeHintType, Object> hints = new HashMap<>();
	hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
	hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
	hints.put(EncodeHintType.MARGIN, 2);

	try {
		final BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
		MatrixToImageWriter.writeToPath(matrix,format,new File("D:/my.png").toPath());
	} catch (WriterException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

解析二维码:

@Test
public void decode() {
	final MultiFormatReader reader = new MultiFormatReader();
	File image = new File("D:/my.png");
	try {
		final BufferedImage bufferedImage = ImageIO.read(image);
		final BinaryBitmap binaryBitmap = new BinaryBitmap(
				new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));
		final Map hints = new HashMap<>();
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		final Result result = reader.decode(binaryBitmap, hints);
		System.out.println(result.getText());
	} catch (NotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
}
1. 引入zxing库 在实现二维码识别之前,我们需要引入一个第三方库——zxing。zxing是一个开源的、高性能的二维码识别库,支持多种编程语言,包括Java。我们可以在maven中添加以下依赖: ``` <!-- zxing core library --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.4.1</version> </dependency> <!-- zxing javase library, including GUI and command line --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.4.1</version> </dependency> ``` 2. 加载二维码图片 我们需要使用Java的ImageIO类中的read方法,将二维码图片读入内存中。 ```java File file = new File("qrcode.png"); BufferedImage image = ImageIO.read(file); ``` 3. 解码二维码 使用zxing库中的解码方法,将内存中的二维码图片解码成字符串。 ```java // 定义二维码解码器 QRCodeReader reader = new QRCodeReader(); // 定义解码参数 Map<DecodeHintType, Object> hints = new HashMap<>(); hints.put(DecodeHintType.CHARACTER_SET, "UTF-8"); // 解码二维码 Result result = reader.decode(new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image))), hints); String text = result.getText(); ``` 4. 输出二维码内容 输出解码后的二维码内容。 ```java System.out.println(text); ``` 完整代码: ```java import java.awt.image.BufferedImage; import java.io.File; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import com.google.zxing.BinaryBitmap; import com.google.zxing.DecodeHintType; import com.google.zxing.MultiFormatReader; import com.google.zxing.Result; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.common.HybridBinarizer; public class QRCodeReader { public static void main(String[] args) { try { // 加载二维码图片 File file = new File("qrcode.png"); BufferedImage image = ImageIO.read(file); // 解码二维码 MultiFormatReader reader = new MultiFormatReader(); Map<DecodeHintType, Object> hints = new HashMap<>(); hints.put(DecodeHintType.CHARACTER_SET, "UTF-8"); Result result = reader.decode(new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image))), hints); String text = result.getText(); // 输出二维码内容 System.out.println(text); } catch (Exception e) { e.printStackTrace(); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值