java生成二维码(在图片上生成二维码(二维码带logo)并且在图片上添加文字标签)

1pom.xml

<!--生成二维码-->
<dependency>
   <groupId>cn.hutool</groupId>
   <artifactId>hutool-extra</artifactId>
   <version>5.4.3</version>
</dependency>
<dependency>
   <groupId>com.google.zxing</groupId>
   <artifactId>core</artifactId>
   <version>3.3.3</version>
</dependency>

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2.工具类

package com.example.QRcode.utils;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.ResourceUtils;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Description: 在图片上生成二维码并且在图片上添加文字response前端
 * @Auther: yjj
 * @Date: 2020/9/23 18:57
 */
public class QRcodeImageFont {

    /***
     * 在一张背景图上添加二维码
     */
    public static void drawString(String content, HttpServletResponse response) throws Exception {
        BufferedImage image = addWater(content);
        Graphics2D gd = image.createGraphics();
        // 3、设置对线段的锯齿状边缘处理
        gd.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        // 5、设置水印文字颜色
        gd.setColor(Color.red);
        // 6、设置水印文字Font
        gd.setFont(new Font("宋体", Font.BOLD, 20));
        // 8、第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y)
        gd.drawString("测试文字", 90, 260);
        gd.dispose();
        // 输出图片
        ServletOutputStream outputStream = response.getOutputStream();
        ImageIO.write(image, "png",outputStream );
        System.out.println("添加水印完成");
        outputStream.flush();
        outputStream.close();
    }

    /***
     * 在一张背景图上添加二维码
     */
    public static BufferedImage addWater(String content) throws Exception {
        // 读取原图片信息
        //得到文件
        File file = ResourceUtils.getFile("classpath:static/img/code.png");
        //文件转化为图片
        Image srcImg = ImageIO.read(file);
        //获取图片的宽
        int srcImgWidth = srcImg.getWidth(null);
        //获取图片的高
        int srcImgHeight = srcImg.getHeight(null);
        // 加水印
        BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bufImg.createGraphics();
        g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
        //使用工具类生成二维码
        Image image = createQrCode(content,200,200);
        //将小图片绘到大图片上,500,300 .表示你的小图片在大图片上的位置。
        g.drawImage(image, 70, 300, null);
        //设置颜色。
        g.setColor(Color.WHITE);
        g.dispose();
        return bufImg;
    }

    private static BufferedImage createQrCode(String content, int width, int height) throws IOException {
        QrConfig config = new QrConfig(width, height);

        Image image = ImageIO.read(resourceLoader("classpath:static/img/onlyLogo.png"));
        config.setImg(image);
        config.setErrorCorrection(ErrorCorrectionLevel.H);
        return QrCodeUtil.generate(
                content,
                config);
    }

    public static InputStream resourceLoader(String fileFullPath) throws IOException {
        ResourceLoader resourceLoader = new DefaultResourceLoader();
        return resourceLoader.getResource(fileFullPath).getInputStream();
    }
}

3.底图和logo

 

4.测试

package com.example.QRcode.controller;

import com.example.QRcode.utils.QRcodeImageFont;
import com.example.QRcode.utils.QRcodeImageUtils;
import com.example.QRcode.utils.QRcodeUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @Description:
 * @Auther: yjj
 * @Date: 2020/9/23 18:58
 */
@RestController
@RequestMapping(value = "/test")
public class TestController {

    @GetMapping("/createQrCode")
    public void createQrCode(@RequestParam("url") String url, HttpServletResponse response) throws Exception {
       
        //在图片上生成二维码,并在图片上添加文字描述
        QRcodeImageFont.drawString(url,response);
    }
}

5.展示

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
以下是Java生成二维码并保存图片的示例代码: ```java import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; 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; public class QRCodeGenerator { public static void main(String[] args) { String text = "https://www.csdn.net"; int width = 500; int height = 500; String format = "jpg"; String filePath = "d://test//out.jpg"; String logoPath = "E:\\test.jpg"; try { QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height); BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); } } File qrFile = new File(filePath); ImageIO.write(bufferedImage, format, qrFile); if (logoPath != null && !"".equals(logoPath.trim())) { QRCodeUtil.addLogo(qrFile, new File(logoPath), new LogoConfig()); } } catch (WriterException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } ``` 其中,QRCodeUtil是一个工具类,用于在二维码添加Logo。你可以在这里找到QRCodeUtil的完整代码:https://github.com/liuyueyi/quick-media/blob/master/quick-media/src/main/java/com/github/hui/quick/plugin/qrcode/util/QRCodeUtil.java
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

《小书生》

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

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

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

打赏作者

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

抵扣说明:

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

余额充值