package com.gk.hotwork.specialWork.util;
|
|
import com.google.zxing.BarcodeFormat;
|
import com.google.zxing.EncodeHintType;
|
import com.google.zxing.MultiFormatWriter;
|
import com.google.zxing.client.j2se.MatrixToImageWriter;
|
import com.google.zxing.common.BitMatrix;
|
|
import javax.imageio.ImageIO;
|
import java.awt.image.BufferedImage;
|
import java.io.ByteArrayOutputStream;
|
import java.io.File;
|
import java.io.IOException;
|
import java.io.OutputStream;
|
import java.util.Base64;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* 二维码生成工具
|
*/
|
public class QrCodeUtil {
|
//编码
|
private static final String CHARSET = "UTF-8";
|
//文件格式
|
private static final String FORMAT_NAME = "JPG";
|
|
// 宽度
|
private static final int WIDTH = 80;
|
// 高度
|
private static final int HEIGHT = 80;
|
|
public QrCodeUtil() {
|
}
|
|
public static BitMatrix createQrCode(String text) {
|
try {
|
Map<EncodeHintType, String> hints = new HashMap();
|
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
|
hints.put(EncodeHintType.MARGIN, "0");//二维码空白区域,最小为0也有白边,只是很小,最小是6像素左右
|
BitMatrix bitMatrix = (new MultiFormatWriter()).encode(text, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
|
return bitMatrix;
|
} catch (Exception var3) {
|
var3.printStackTrace();
|
return null;
|
}
|
}
|
|
static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
|
BufferedImage image = toBufferedImage(matrix);
|
if (!ImageIO.write(image, format, file)) {
|
throw new IOException("Could not write an image of format " + format + " to " + file);
|
}
|
}
|
|
static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {
|
BufferedImage image = toBufferedImage(matrix);
|
if (!ImageIO.write(image, format, stream)) {
|
throw new IOException("Could not write an image of format " + format);
|
}
|
}
|
|
private static BufferedImage toBufferedImage(BitMatrix matrix) {
|
int width = matrix.getWidth();
|
int height = matrix.getHeight();
|
BufferedImage image = new BufferedImage(width, height, 1);
|
|
for (int x = 0; x < width; ++x) {
|
for (int y = 0; y < height; ++y) {
|
image.setRGB(x, y, matrix.get(x, y) ? -16777216 : -1);
|
}
|
}
|
return image;
|
}
|
|
/**
|
* 生成二维码字节流
|
*/
|
public static ByteArrayOutputStream generate(String text) throws IOException {
|
//获取一个二维码图片
|
BitMatrix bitMatrix = QrCodeUtil.createQrCode(text);
|
//以流的形式输出到前端
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
MatrixToImageWriter.writeToStream(bitMatrix, FORMAT_NAME, outputStream);
|
return outputStream;
|
}
|
|
|
public static void main(String[] args) throws IOException {
|
//获取一个二维码图片
|
BitMatrix bitMatrix = QrCodeUtil.createQrCode("测试");
|
|
|
//以流的形式输出到前端
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", outputStream);
|
|
//转换为base64
|
Base64.Encoder encoder1 = Base64.getEncoder();
|
String advUrl = "data:image/jpeg;base64,"
|
+ encoder1.encodeToString(outputStream.toByteArray());
|
//打印base64结果
|
System.out.println(advUrl);
|
|
}
|
}
|