Java 实现二维码生成

package com.epoint.cs.xmzyyyzinfo.action;

import cn.hutool.extra.qrcode.QrConfig;
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class CreateCodeUtil {



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

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

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

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


    public static BufferedImage encode(String context) {

        int intModelImgWidth= modelImgWidth;
        int intMmodelHeight = modelHeight;
        int intCodeWidth = codeWidth;


        int intCodeHeight = codeHeight;


        int intDistance = distance;

        // 创建主模板图片
        BufferedImage image = new BufferedImage(intModelImgWidth, intMmodelHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2DModel = image.createGraphics();
        // 设置图片的背景色
        graphics2DModel.setColor(Color.white);
        graphics2DModel.fillRect(0, 0, intModelImgWidth, intMmodelHeight);
        //描绘的y轴
        int y = 0;
        //头部文字
        //中间二维码
        if (StringUtils.isNotBlank(context)) {
            Graphics2D graphics2DCode = image.createGraphics();
            //hutool的QrConfig配置
            QrConfig qrConfig = new QrConfig();
            qrConfig.setHeight(intCodeWidth);
            qrConfig.setWidth(intCodeHeight);
            qrConfig.setBackColor(Color.WHITE);
            BufferedImage bufferedImage = cn.hutool.extra.qrcode.QrCodeUtil.generate(context, qrConfig);
            int x = (intModelImgWidth - qrConfig.getWidth()) / 2;
            graphics2DCode.drawImage(bufferedImage, x, y, qrConfig.getWidth(), qrConfig.getHeight(), null);
            //间距20
            y += qrConfig.getHeight() + intDistance;
        }
        return image;
    }

    /**
     * 获取y轴高度
     *
     * @param title
     * @param image
     * @param y
     * @param checkAdd
     * @return
     */
    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 编码转换为 base64
     *
     * @param bufferedImage
     * @return
     */
    public static String BufferedImageToBase64(BufferedImage bufferedImage) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
        try {
            ImageIO.write(bufferedImage, "png", baos);//写入流中
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] bytes = baos.toByteArray();//转换成字节
        BASE64Encoder encoder = new BASE64Encoder();
        String png_base64 = encoder.encodeBuffer(bytes).trim();//转换成base64串
        png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n
        return "data:image/jpg;base64," + png_base64;
    }

    /**
     * base64 编码转换为 BufferedImage
     *
     * @param base64
     * @return
     */
    public static BufferedImage base64ToBufferedImage(String base64) {
        BASE64Decoder decoder = new sun.misc.BASE64Decoder();
        try {
            byte[] bytes1 = decoder.decodeBuffer(base64);
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
            return ImageIO.read(bais);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

代衡_Monster

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

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

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

打赏作者

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

抵扣说明:

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

余额充值