导包
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.4.0</version>
</dependency>
工具类
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 com.google.zxing.oned.Code128Writer;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.util.StringUtils;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class CodeUtil {
//二维码
public static String getQRCode(String content, int width, int height) {
if (!StringUtils.isEmpty(content)) {
ServletOutputStream stream = null;
HashMap<EncodeHintType, Comparable> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.MARGIN, 0);
try {
QRCodeWriter writer = new QRCodeWriter();
BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", os);
BASE64Encoder encoder = new BASE64Encoder();
String resultImage = new String("data:image/png;base64," + encoder.encode(os.toByteArray()));
return resultImage;
} catch (Exception e) {
} finally {
if (stream != null) {
try {
stream.flush();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return null;
}
//条形码
public static String getBarCode(String content, int width, int height) {
try {
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.MARGIN, 0);
int realWidth = getBarCodeNoPaddingWidth(width, content, width);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.CODE_128, realWidth, height, hints);
BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", os);
BASE64Encoder encoder = new BASE64Encoder();
String resultImage = new String("data:image/png;base64," + encoder.encode(os.toByteArray()));
return resultImage;
} catch (Exception e) {
return null;
}
}
private static int getBarCodeNoPaddingWidth(int expectWidth, String contents, int maxWidth) {
boolean[] code = new Code128Writer().encode(contents);
int inputWidth = code.length;
double outputWidth = (double) Math.max(expectWidth, inputWidth);
double multiple = outputWidth / inputWidth;
//优先取大的
int returnVal = 0;
int ceil = (int) Math.ceil(multiple);
if (inputWidth * ceil <= maxWidth) {
returnVal = inputWidth * ceil;
} else {
int floor = (int) Math.floor(multiple);
returnVal = inputWidth * floor;
}
return returnVal;
}
}
使用
String QRCode = CodeUtil.getQRCode(random, QRWidth, QRHeight);//二维码
String BarCode = CodeUtil.getBarCode(random, BarWidth, BarHeight);//条形码
random ——自己生成的随机数,即二维码的内容
QRWidth —— 二维码的宽度
QRHeight —— 二维码的高度
QRCode —— 生成的Base64 字符串,返给前端使用
前端使用
<img src="QRCode" />
直接引入即可