嗨森逛服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hai-server/hai-service/src/main/java/com/hai/common/QRCodeGenerator.java

45 lines
1.4 KiB

package com.hai.common;
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;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
/**
* @Auther: 胡锐
* @Description: 生成二维码
* @Date: 2021/3/27 12:07
*/
public class QRCodeGenerator {
public 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);
File file = new File(filePath);
if(!file.exists()){
file.mkdirs();
}
//Path path = FileSystems.getDefault().getPath(filePath);
MatrixToImageWriter.writeToFile(bitMatrix, "PNG", file);
}
public static void main(String[] args) {
try {
generateQRCodeImage("This is my first QR Code", 350, 350, "D:\\/ss/qr1.png");
} 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());
}
}
}