生成二维码 Java实现二维码生成 Google-Zxing
转载 https://blog.csdn.net/gisboygogogo/article/details/86036656
篇文章将介绍java中如何生成二维码,二维码的展示主要包括两各方面:
1.直接生成图片(直接生成图片不需要web程序,maven工程即可)
2.将二维码转为字节数组,然后在web页面显示。生成二维码的功能主要是依赖Google的Zxing包。
项目添加依赖
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.0</version>
</dependency>
2.生成二维码图片
名为generateQRCodeImage方法,将字符串封装成二维码、设置二维码的宽度和高度、声明二维码保存的路径与图片名称。
package org.thinkingingis.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
public class QRCodeGenerator {
private static final String QR_CODE_IMAGE_PATH = "/Users/gisboy/Desktop/MyQRCode.png";
private static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
Path path = FileSystems.getDefault().getPath(filePath);
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
}
public static void main(String[] args) {
try {
generateQRCodeImage("This is my first QR Code", 350, 350, QR_CODE_IMAGE_PATH);
} catch (WriterException e) {
System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage());
} catch (IOException e) {
System.out.println("Could not generate QR Code, IOException :: " + e.getMessage());
}
}
}
上面的方法是将二维码保存为图片,如果你不想将二维码保存为图片,也可以将其保存为字节数组,可以用zxing 库提供的MatrixToImageWriter.writeToStream()方法:
public static byte[] getQRCodeImage(String text, int width, int height) throws WriterException, IOException {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
byte[] pngData = pngOutputStream.toByteArray();
return pngData;
}
@GetMapping("qrcode")
public String qrcode() {
return "/qrcode";
}
@GetMapping(value="/qrimage")
public ResponseEntity<byte[]> getQRImage() {
//二维码内的信息
String info = "This is my first QR Code";
byte[] qrcode = null;
try {
qrcode = QRCodeGenerator.getQRCodeImage(info, 360, 360);
} catch (WriterException e) {
System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.out.println("Could not generate QR Code, IOException :: " + e.getMessage());
}
// Set headers
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_PNG);
return new ResponseEntity<byte[]> (qrcode, headers, HttpStatus.CREATED);
}