上代码
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.web.multipart.MultipartFile;
import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
public class QRCodeUtil {
public static String generateBase64QRCode(String qrCodeText, int size) {
return "data:image/png;base64," + generateBase64QRCode(qrCodeText, size, (String) null, 0);
}
public static String generateBase64QRCodeWithLogo(String qrCodeText, int size, String logoPath, int logoSize) {
return "data:image/png;base64," + generateBase64QRCode(qrCodeText, size, logoPath, logoSize);
}
public static String generateBase64QRCodeWithLogo(String qrCodeText, int size, MultipartFile logoFile, int logoSize) {
return "data:image/png;base64," + generateBase64QRCode(qrCodeText, size, logoFile, logoSize);
}
private static String generateBase64QRCode(String qrCodeText, int size, String logoPath, int logoSize) {
try {
Map<EncodeHintType, Object> hintMap = new HashMap<>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hintMap.put(EncodeHintType.MARGIN, 2);
hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix byteMatrix = qrCodeWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, size, size, hintMap);
MatrixToImageConfig config = new MatrixToImageConfig(0xFF000001, 0xFFFFFFFF);
BufferedImage image = MatrixToImageWriter.toBufferedImage(byteMatrix, config);
if (logoPath != null && !logoPath.isEmpty()) {
BufferedImage logo = ImageIO.read(new URL(logoPath));
addLogoToQRCode(image, logo, logoSize);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
baos.flush();
baos.close();
byte[] imageBytes = baos.toByteArray();
return Base64.getEncoder().encodeToString(imageBytes);
} catch (IOException | WriterException e) {
e.printStackTrace();
return null;
}
}
private static String generateBase64QRCode(String qrCodeText, int size, MultipartFile logoFile, int logoSize) {
try {
Map<EncodeHintType, Object> hintMap = new HashMap<>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hintMap.put(EncodeHintType.MARGIN, 2);
hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix byteMatrix = qrCodeWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, size, size, hintMap);
MatrixToImageConfig config = new MatrixToImageConfig(0xFF000001, 0xFFFFFFFF);
BufferedImage image = MatrixToImageWriter.toBufferedImage(byteMatrix, config);
if (logoFile != null && !logoFile.isEmpty()) {
InputStream logoInputStream = logoFile.getInputStream();
BufferedImage logo = ImageIO.read(logoInputStream);
if (logo.getColorModel().getColorSpace().getType() != ColorSpace.TYPE_RGB) {
throw new IllegalArgumentException("Logo image is not in expected color space");
}
addLogoToQRCode(image, logo, logoSize);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
baos.flush();
byte[] imageBytes = baos.toByteArray();
baos.close();
return Base64.getEncoder().encodeToString(imageBytes);
} catch (IOException | WriterException e) {
e.printStackTrace();
return null;
}
}
private static void addLogoToQRCode(BufferedImage qrcode, BufferedImage originalLogo, int logoSize) {
Graphics2D graphics = (Graphics2D) qrcode.getGraphics();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
int widthLogo = originalLogo.getWidth(null) > qrcode.getWidth() * 1 / 5 ? qrcode.getWidth() * 1 / 5 : originalLogo.getWidth(null);
int heightLogo = originalLogo.getHeight(null) > qrcode.getHeight() * 1 / 5 ? qrcode.getHeight() * 1 / 5 : originalLogo.getHeight(null);
if (logoSize > 0) {
widthLogo = logoSize;
heightLogo = logoSize;
}
Image scaledLogo = originalLogo.getScaledInstance(widthLogo, heightLogo, Image.SCALE_SMOOTH);
BufferedImage logo = new BufferedImage(widthLogo, heightLogo, BufferedImage.TYPE_INT_ARGB_PRE);
Graphics2D g2dLogo = logo.createGraphics();
g2dLogo.drawImage(scaledLogo, 0, 0, null);
g2dLogo.dispose();
int x = (qrcode.getWidth() - widthLogo) / 2;
int y = (qrcode.getHeight() - heightLogo) / 2;
int borderSize = 2;
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
graphics.setColor(Color.WHITE);
graphics.fillRect(x - borderSize, y - borderSize, widthLogo + 2 * borderSize, heightLogo + 2 * borderSize);
graphics.drawImage(logo, x, y, widthLogo, heightLogo, null);
graphics.dispose();
}
}