栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

使用Hutool的QrCodeUtil工具类生成二维码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Hutool的QrCodeUtil工具类生成二维码

文章目录
  • java 生成二维码
    • 使用Hutool的QrCodeUtil工具类
    • 简单生成二维码
    • 具有头部信息、底部信息、logo的二维码
      • 构建 CreateCodeUtil工具
    • 拓展

java 生成二维码 使用Hutool的QrCodeUtil工具类

项目依赖

  • 注意: QrCodeUtil里的QrConfig依赖于zxing,所以需要com.google.zxing依赖,若无关紧要的二维码,请先考虑前端生成.
    
        
            com.google.zxing
            core
            3.5.0
        
        
       
            cn.hutool
            hutool-all
            5.7.20
        
简单生成二维码
QrCodeUtil.generate(context, 100, 100, ImgUtil.IMAGE_TYPE_JPEG, response.getOutputStream());
  • QrCodeUtil 描述
具有头部信息、底部信息、logo的二维码

效果图

构建 CreateCodeUtil工具
  • y轴取数类似操作word文档的光标,对于文字就是光标在底部,对于图片光标在顶部。
package cn.prohup.xiao.crm.util;

import cn.hutool.extra.qrcode.QrConfig;
import org.apache.commons.lang3.StringUtils;

import java.awt.*;
import java.awt.image.BufferedImage;

public class CreateCodeUtil {

    //模板宽度
    public static final int modelImgWidth = 150;
    //模板高度
    public static final int modelHeight = 300;

    //二维码宽度
    public static final int codeWidth = 100;
    //二维码高度
    public static final int codeHeight = 100;

    //模块间距
    public static final int distance = 20;

    //文字大小
    public static final int FontSize = 15;


    
    public static BufferedImage encode(String context, String titleHeader, String titleFooter, String imgLogoPath) {

        // 创建主模板图片
        BufferedImage image = new BufferedImage(modelImgWidth, modelHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2DModel = image.createGraphics();
        // 设置图片的背景色
        graphics2DModel.setColor(Color.white);
        graphics2DModel.fillRect(0, 0, modelImgWidth, modelHeight);

        //描绘的y轴
        int y = 0;

        //头部文字
        y = getY(titleHeader, image, y, true);

        //中间二维码
        if (StringUtils.isNotBlank(context)) {
            Graphics2D graphics2DCode = image.createGraphics();

            //hutool的QrConfig配置
            QrConfig qrConfig = new QrConfig();
            qrConfig.setHeight(codeWidth);
            qrConfig.setWidth(codeHeight);
            qrConfig.setBackColor(Color.WHITE);
            if (StringUtils.isNotBlank(imgLogoPath)) {
                qrConfig.setImg(imgLogoPath);
            }
            BufferedImage bufferedImage = cn.hutool.extra.qrcode.QrCodeUtil.generate(context, qrConfig);
            int x = (modelImgWidth - qrConfig.getWidth()) / 2;
            graphics2DCode.drawImage(bufferedImage, x, y, qrConfig.getWidth(), qrConfig.getHeight(), null);
            //间距20
            y +=qrConfig.getHeight()+distance;
        }


        //尾部字体
        y = getY(titleFooter, image, y, false);

        return image;
    }

    
    public static int getY(String title, BufferedImage image, int y, boolean checkAdd) {
        if (StringUtils.isNotBlank(title)) {
            //获取y轴(类似光标所在处)
            y += FontSize;
            Graphics2D graphics2DHead = image.createGraphics();
            //设置字体
            graphics2DHead.setColor(Color.black);
            Font font = new Font("宋体", Font.PLAIN, FontSize);
            graphics2DHead.setFont(font);
            //文字生成--抗锯齿
            graphics2DHead.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_DEFAULT);

            //文字长度--获取该文字的在该模板的总长度
            int textLenght = graphics2DHead.getFontMetrics().stringWidth(title);
            //居中=(模板总长度-文字长度)/2
            int x = (modelImgWidth - textLenght) / 2;
            graphics2DHead.drawString(title, x, y);
            //间距+20
            if (checkAdd) {
                y += distance;
            }
        }
        return y;
    }
}

拓展
  • 通过BufferedImage 获取byte[]进行Base64加密传输
		String context = "https://www.baidu.com/";
        BufferedImage bufferedImage = CreateCodeUtil.encode(context, "喜洋洋", "灰太狼", "G:\study\crm\src\main\resources\static\picture\8102759514c24f2a92e01c8e88476a78.jpg");
        
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, ImgUtil.IMAGE_TYPE_PNG, os);
        byte[] bytes = os.toByteArray();
        BASE64Encoder encoder = new BASE64Encoder();
        String data = encoder.encode(bytes);
  • 通过ImageIO 响应页面
    String context = "https://www.baidu.com/";
    BufferedImage bufferedImage = CreateCodeUtil.encode(context, "喜洋洋", "灰太狼", "G:\study\crm\src\main\resources\static\picture\8102759514c24f2a92e01c8e88476a78.jpg");
  
    ImageIO.write(bufferedImage, ImgUtil.IMAGE_TYPE_PNG, os);
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1023990.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号