springboot生成二维码到海报模板上

springboot生成二维码到海报模板上

QRCodeController

package com.ruoyi.web.controller.app;

import com.google.zxing.WriterException;
import com.ruoyi.app.domain.Opportunity;
import com.ruoyi.app.tool.QRCodeGenerator;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/app/qrcCode")
@Api(tags = "推广海报接口")
public class QRCodeController {

    @Autowired
    private QRCodeGenerator qrCodeGenerator;

    @ApiOperation(value = "获取推广海报", notes = "获取推广海报")
    @GetMapping("/qrImage")
    public void generateQRCode(HttpServletResponse response, @RequestParam Map<String, String> params) throws Exception {
        String openid = params.get("openid");
        String xh = params.get("xh");
        if (openid == null || openid.isEmpty()) {
            throw new Exception("openid不能为空");
        }
        if (xh == null || xh.isEmpty()) {
            xh = "1";
        }

		// 改为自己的链接
        String url = "http://*******.cn/app/index/?parentId=" + openid;
        byte[] qrCodeBytes = qrCodeGenerator.generateQRCode(url, 290, 290, xh);

        response.setContentType(MediaType.IMAGE_PNG_VALUE);
        response.setHeader("Content-Disposition", "inline; filename=qrCode.png");
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Pragma", "no-cache");
        response.setContentLength(qrCodeBytes.length);

        try (OutputStream out = response.getOutputStream()) {
            out.write(qrCodeBytes);
        }
    }
}

QRCodeGenerator

package com.ruoyi.app.tool;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class QRCodeGenerator {
    public byte[] generateQRCode(String url, int qrCodeWidth, int qrCodeHeight, String xh) throws WriterException, IOException {
        // 生成二维码
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 设置容错级别为高
        BitMatrix bitMatrix = qrCodeWriter.encode(url, BarcodeFormat.QR_CODE, qrCodeWidth, qrCodeHeight, hints);
        BufferedImage qrCodeImage = bitMatrixToImage(bitMatrix);

        // 加载背景图片
        ClassPathResource backgroundImageResource = new ClassPathResource("static/" + xh + ".jpg");
        BufferedImage backgroundImage = ImageIO.read(backgroundImageResource.getInputStream());

        // 创建包含背景图片的画布
        int combinedImageWidth = backgroundImage.getWidth();
        int combinedImageHeight = backgroundImage.getHeight();
        BufferedImage combinedImage = new BufferedImage(combinedImageWidth, combinedImageHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D combinedGraphics = combinedImage.createGraphics();
        combinedGraphics.drawImage(backgroundImage, 0, 0, null);

        // 将二维码放在背景图的左下角
        int qrCodeX = 930; // 二维码在背景图片中的X坐标
        int qrCodeY = combinedImageHeight - qrCodeHeight - 20; // 二维码在背景图片中的Y坐标

        // 添加红色边框
        int borderWidth = 2; // 边框宽度
        Color borderColor = Color.decode("#B11E4C"); // 边框颜色
        combinedGraphics.setColor(new Color(borderColor.getRed(), borderColor.getGreen(), borderColor.getBlue(), 0)); // 设置填充颜色为透明色
        combinedGraphics.fillRoundRect(qrCodeX - borderWidth, qrCodeY - borderWidth, qrCodeWidth + 2 * borderWidth, qrCodeHeight + 2 * borderWidth, 5, 5);
        combinedGraphics.setColor(borderColor); // 设置边框颜色
        combinedGraphics.drawRoundRect(qrCodeX - borderWidth, qrCodeY - borderWidth, qrCodeWidth + 2 * borderWidth, qrCodeHeight + 2 * borderWidth, 5, 5); // 绘制边框
        combinedGraphics.drawImage(qrCodeImage, qrCodeX, qrCodeY, null);

        // 将合成的图片转为字节数组
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ImageIO.write(combinedImage, "png", byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
    }

    private BufferedImage bitMatrixToImage(BitMatrix bitMatrix) {
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? Color.BLACK.getRGB() : Color.WHITE.getRGB());
            }
        }
        return image;
    }
}

海报模板存放(不带二维码的图片):
在这里插入图片描述

把地址放到白名单中,无需token可以访问,然后就得到:
在这里插入图片描述
在这里插入图片描述

要在Spring Boot生成二维码,你可以使用ZXing库。ZXing是一个开放源码的条形码和二维码生成库,它支持多种编程语言,包括Java。 首先,你需要在你的项目中添加ZXing依赖。你可以在Maven中添加以下依赖: ```xml <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.4.1</version> </dependency> ``` 然后,你可以使用以下代码在Spring Boot生成二维码: ```java import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import javax.imageio.ImageIO; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; 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; @RestController public class QRCodeController { @GetMapping(value = "/qrcode/{text}", produces = MediaType.IMAGE_PNG_VALUE) public ResponseEntity<BufferedImage> generateQRCode(@PathVariable String text) throws WriterException { int width = 300; int height = 300; QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height); BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ImageIO.write(bufferedImage, "png", baos); } catch (Exception e) { e.printStackTrace(); } return ResponseEntity.ok().body(bufferedImage); } } ``` 在上面的代码中,我们使用QRCodeWriter类生成二维码,然后将其转换为BufferedImage并返回给客户端。请注意,我们还设置了响应的MediaType为“image/png”。 你可以通过以下URL在浏览器中访问该REST API生成二维码: ``` http://localhost:8080/qrcode/your-text-here ``` 你可以将“your-text-here”替换为你要生成二维码的文本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星月前端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值