1,添加maven依赖
<!-- 生成二维码 -->
<dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.4.1</version>
</dependency>
2,微信支付宝图片

3,工具类代码
import cn.hutool.core.codec.Base64;
import com.google.common.collect.Maps;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Map;/*** @author guiJia* @date 2021/7/14 10:08*/
@Slf4j
@Component
public class ZxingUtils {private static final int WIDTH = 300;private static final int HEIGHT = 300;/*** 生成二维码* @param content 要写入二维码的内容* @param fileName 文件名*/public String createQrCode(String content, String fileName){// 定义二维码参数Map<EncodeHintType, Object> hints = Maps.newHashMap();hints.put(EncodeHintType.CHARACTER_SET, "utf-8");hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);//设置边距默认是5hints.put(EncodeHintType.MARGIN, 2);// 定义缓冲区图片BufferedImage bufferedImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);Graphics2D g = (Graphics2D) bufferedImage.getGraphics();try {// zxing的官方类BitMatrix bitMatrix=new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);BufferedImage image = new BufferedImage(bitMatrix.getWidth(), bitMatrix.getHeight(), BufferedImage.TYPE_INT_RGB);for (int x = 0; x < bitMatrix.getWidth(); x++) {for (int y = 0; y < bitMatrix.getHeight(); y++) {image.setRGB(x, y, bitMatrix.get(x, y) ? MatrixToImageConfig.BLACK : MatrixToImageConfig.WHITE);}}// 生成二维码g.drawImage(image, 0, 0, null);// 判断是否需要添加logo图片if(StringUtils.isNotBlank(fileName)) {InputStream inputStream = this.getClass().getResourceAsStream("/template/" + fileName);BufferedImage logo = ImageIO.read(inputStream);int width4 = WIDTH / 4;int width8 = width4 / 2;int height4 = HEIGHT / 4;int height8 = height4 / 2;g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));g.drawImage(logo, width4 + width8, height4 + height8, width4, height4, null);}g.dispose();// 结束内存图片bufferedImage.flush();
// response.setHeader("Cache-Control", "no-store, no-cache");
// response.setContentType("image/jpeg");
// ServletOutputStream os = response.getOutputStream();
// ImageIO.write(bufferedImage, "PNG",os);
// os.flush();
// os.close();ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();ImageIO.write(bufferedImage, "png", byteArrayOutputStream);return Base64.encode(byteArrayOutputStream.toByteArray());} catch (Exception e) {e.printStackTrace();log.info("生成二维码失败");}return StringUtils.EMPTY;}/*** 解析二维码(输入图片字节流)* @param input 二维码图片的字节流* @return 二维码内容*/public static Result readQrCode(InputStream input){// zxing的官方类MultiFormatReader reader = new MultiFormatReader();try {BufferedImage image = ImageIO.read(input);BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));Map<DecodeHintType, Object> hints = Maps.newHashMap();hints.put(DecodeHintType.CHARACTER_SET, "utf-8");return reader.decode(binaryBitmap, hints);} catch (Exception e) {e.printStackTrace();log.info("解析二维码失败");return null;}}
}
4,编写测试文件
@Testpublic void test3(){String content = "https://qr.alipay.com/bax01642biajcdxjsmfo553e";zxingUtils.createQrCode(content, "支付宝支付.png");}