JAVA生成带LOGO二维码

来源:Java生成带logo二维码_多汁多味的博客-CSDN博客_java 生成带logo的二维码

实战可替换成阿里云存储

package com.jt.utils;

import com.github.pagehelper.util.StringUtil;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

public class QrCodeCreateUtil {

    private static final int QR_COLOR = 0xFF000000; // 默认是黑色
    private static final int BG_WHITE = 0xFFFFFFFF; // 背景颜色

    private static final int WIDTH = 400;
    private static final int HEIGHT = 400;

    private static final String ADDRESS = "D:/QR_code/";

    private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {
        private static final long serialVersionUID = 1L;

        {
            put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 设置QR二维码的纠错级别(H为最高级别)具体级别信息
            put(EncodeHintType.CHARACTER_SET, "utf-8");// 设置编码方式
            put(EncodeHintType.MARGIN, 0);
        }
    };

    // 生成带logo的二维码图片
    public static void drawLogoQRCode(File logoFile, String qrUrl, String note) {
        try {
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            // 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
            BitMatrix encode = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
            BufferedImage bufferedImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

            // 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色
            for (int i = 0; i < WIDTH; i++) {
                for (int j = 0; j < HEIGHT; j++) {
                    bufferedImage.setRGB(i, j, encode.get(i, j) ? QR_COLOR : BG_WHITE);
                }
            }

            int width = bufferedImage.getWidth();
            int height = bufferedImage.getHeight();

            if (Objects.nonNull(logoFile) && logoFile.exists()) {
                // 构建绘图对象
                Graphics2D graphics = bufferedImage.createGraphics();
                // 读取logo图片
                BufferedImage logo = ImageIO.read(logoFile);
                // 开始绘制logo图片
                graphics.drawImage(logo, width * 2 / 5,height * 2 / 5, width * 2 / 10, height * 2 / 10, null);
                graphics.dispose();
                logo.flush();
            }

            // 自定义文本描述
            if (StringUtil.isNotEmpty(note)) {
                bufferedImage = handleNote(bufferedImage, note, width, height);
            }

            bufferedImage.flush();
            ImageIO.write(bufferedImage, "png", handleFileName()); // TODO

        } catch (Exception e) {
            e.getMessage();
        }
    }

    public static BufferedImage handleNote(BufferedImage bufferedImage, String note, int width, int height) {
        // 新的图片,把带logo的二维码下面加上文字
        // 新的图片,把带logo的二维码下面加上文字
        BufferedImage outImage = new BufferedImage(400, 445, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D outg = outImage.createGraphics();
        // 画二维码到新的面板
        outg.drawImage(bufferedImage, 0, 0, bufferedImage.getWidth(), bufferedImage.getHeight(), null);
        // 画文字到新的面板
        outg.setColor(Color.BLACK);
        outg.setFont(new Font("楷体", Font.BOLD, 30)); // 字体、字型、字号
        int strWidth = outg.getFontMetrics().stringWidth(note);
        if (strWidth > 399) {
            //长度过长就截取前面部分
            // 长度过长就换行
            String note1 = note.substring(0, note.length() / 2);
            String note2 = note.substring(note.length() / 2, note.length());
            int strWidth1 = outg.getFontMetrics().stringWidth(note1);
            int strWidth2 = outg.getFontMetrics().stringWidth(note2);
            outg.drawString(note1, 200 - strWidth1 / 2, height + (outImage.getHeight() - height) / 2 + 12);
            BufferedImage outImage2 = new BufferedImage(400, 485, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D outg2 = outImage2.createGraphics();
            outg2.drawImage(outImage, 0, 0, outImage.getWidth(), outImage.getHeight(), null);
            outg2.setColor(Color.BLACK);
            outg2.setFont(new Font("楷体", Font.BOLD, 30)); // 字体、字型、字号
            outg2.drawString(note2, 200 - strWidth2 / 2, outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight()) / 2 + 5);
            outg2.dispose();
            outImage2.flush();
            outImage = outImage2;
        } else {
            outg.drawString(note, 200 - strWidth / 2, height + (outImage.getHeight() - height) / 2 + 12); // 画文字
        }
        outg.dispose();
        outImage.flush();
        return bufferedImage = outImage;
    }

    public static File handleFileName() {
        UUID uuid = UUID.randomUUID();
        String replace = uuid.toString().replace("-", "");
        return new File( ADDRESS + replace + ".jpg");
    }

    public static void main(String[] args) throws WriterException {
        File logoFile = new File("C:\\Users\\EDZ\\Desktop\\新建文件夹\\11\\6222dfe6e3cc28a5bc55aa1e0edda18.jpg");
        File qrCodeFile = new File("D:/QR_code/100.jpg");
        String url = "https://www.bilibili.com/video/BV1hN411o77q?spm_id_from=333.337.search-card.all.click&vd_source=68ef7e133c823eab77941ed26ac5ea79";
        String note = "Twistzz";
        drawLogoQRCode(logoFile, url, note);
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值