生成二维码的开源项目有SwetakeQRCode、BarCode4j、Zxing......
前端有JQuery-qrcode,同样能实现生成二维码。下面我用的是Zxing
pom.xml的配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zhaomei.test</groupId> <artifactId>qrcode</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>qrcode</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.0.0</version> </dependency> </dependencies> </project> |
java代码生成二维码(content需要什么内容就写哪个内容跳转)
package com.zhaomei.test.qrcode; import java.io.File; import java.nio.file.Path; import java.util.HashMap; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public class QRCodeTest { //生成二维码 public static void main(String[] args) throws Exception { int width =300 ; int height =300 ; String format ="png"; String content="http://www.jthinking.com"; //定义二维码的参数 HashMap hints =new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); hints.put(EncodeHintType.MARGIN,2); try { BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints); Path file =new File("D:/img.png").toPath(); MatrixToImageWriter.writeToPath(bitMatrix, format, file); } catch (WriterException e) { e.printStackTrace(); } } } |
java代码解析二维码
package com.zhaomei.test.qrcode; import java.awt.image.BufferedImage; import java.io.File; import java.util.HashMap; import javax.imageio.ImageIO; import com.google.zxing.BinaryBitmap; import com.google.zxing.EncodeHintType; 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 ReadQRCode { public static void main(String[] args) throws Exception { MultiFormatReader formatReader =new MultiFormatReader(); File file =new File("D:/img.png"); BufferedImage image =ImageIO.read(file); //生成二维码参数 HashMap hints =new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BinaryBitmap binaryBitmap =new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image))); Result result= formatReader.decode(binaryBitmap,hints); System.out.println("解析结果是"+result.toString()); System.out.println("二维码文本内容"+result.getText()); System.out.println("二维码格式内容"+result.getBarcodeFormat()); } } |
解析结果是http://www.jthinking.com 二维码文本内容http://www.jthinking.com 二维码格式内容QR_CODE |
生成的二维码图: