导包
<!-- 二维码生成工具 -->
<dependency>
<groupId>com.github.liuyueyi.media</groupId>
<artifactId>qrcode-plugin</artifactId>
<version>2.6.3</version>
</dependency>
工具类
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeGenWrapper;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeOptions;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class YcsQrBarCodeUtils {
public static final int LOGO_RATE = 10;
public static final String FORMAT = "png";
public static final int MIN_BARCODE_WIDTH = 98;
public static BufferedImage createQRCode(String content, int size, Color preColor, BufferedImage logoImg) {
try {
QrCodeGenWrapper.Builder builder = QrCodeGenWrapper.of(content)
.setW(size)
.setH(size)
.setDrawBgColor(0xffffffff)
.setDrawPreColor(preColor)
.setErrorCorrection(ErrorCorrectionLevel.M)
.setDrawStyle(QrCodeOptions.DrawStyle.CIRCLE)
.setDrawEnableScale(true);
if (logoImg != null) {
builder.setLogoBgColor(Color.LIGHT_GRAY)
.setLogo(logoImg)
.setLogoRate(LOGO_RATE)
.setLogoStyle(QrCodeOptions.LogoStyle.CIRCLE);
}
return builder.asBufferedImage();
} catch (Exception e) {
throw new RuntimeException("生成二维码失败!content = "
+ content + ", size = " + size + ", preColor = " + preColor + "\n" + e);
}
}
public static String parseQRCode(BufferedImage image) {
try {
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Map<DecodeHintType, Object> hints = new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
Result result = new MultiFormatReader().decode(bitmap, hints);
return result.getText();
} catch (Exception e) {
throw new RuntimeException("解析二维码失败!" + "\n" + e);
}
}
public static BufferedImage scale(BufferedImage srcImage, int height, int width, boolean hasFiller) {
Image destImage = srcImage.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH);
if ((srcImage.getHeight() > height) || (srcImage.getWidth() > width)) {
double ratio = 0.0;
if (srcImage.getHeight() > srcImage.getWidth()) {
ratio = (new Integer(height)).doubleValue() / srcImage.getHeight();
} else {
ratio = (new Integer(width)).doubleValue() / srcImage.getWidth();
}
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
destImage = op.filter(srcImage, null);
}
if (hasFiller) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphic = image.createGraphics();
graphic.setColor(Color.white);
graphic.fillRect(0, 0, width, height);
if (width == destImage.getWidth(null)) {
graphic.drawImage(destImage,
0, (height - destImage.getHeight(null)) / 2,
destImage.getWidth(null), destImage.getHeight(null),
Color.white, null);
} else {
graphic.drawImage(destImage,
(width - destImage.getWidth(null)) / 2, 0,
destImage.getWidth(null), destImage.getHeight(null),
Color.white, null);
}
graphic.dispose();
destImage = image;
}
return (BufferedImage) destImage;
}
public static void createBarcode(String content, int width, int height, OutputStream outputStream) {
width = Math.max(MIN_BARCODE_WIDTH, width);
try {
BitMatrix bitMatrix = new MultiFormatWriter()
.encode(content, BarcodeFormat.EAN_13, width, height, null);
MatrixToImageWriter.writeToStream(bitMatrix, FORMAT, outputStream);
} catch (Exception e) {
throw new RuntimeException("创建条形码失败!content = " + content + "\n" + e);
}
}
public static String parseBarcode(BufferedImage image) {
try {
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result = new MultiFormatReader().decode(bitmap, null);
return result.getText();
} catch (Exception e) {
throw new RuntimeException("解析条形码失败!" + "\n" + e);
}
}
}
测试
public class SelfTest {
private File desktopDir;
private String desktopPath;
@Before
public void desktopDirTest() {
desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
desktopPath = desktopDir.getAbsolutePath();
}
@Test
public void QrBarUtils() throws IOException {
BufferedImage logoImg = ImageIO.read(new File(desktopPath + "/base64.jpg"));
logoImg = YcsQrBarCodeUtils.scale(logoImg, 80, 80, true);
BufferedImage qrCode = YcsQrBarCodeUtils.createQRCode(
"http://localhost:8080/test", 500, Color.RED, logoImg);
ImageIO.write(qrCode, "png", new File(desktopPath + "/code.png"));
}
}