二维码生成

maven依赖

<!-- 二维码 -->
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.3.0</version>
</dependency>

代码

package com.qt.exercise;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import java.util.Hashtable;

import static org.apache.catalina.manager.Constants.CHARSET;

/**
 * @author: lsq
 * @date: 2019/7/11 11:45
 */
public class QRCodeUtil {

    private static final String FORMAT_NAME = "JPG";
    // 二维码尺寸
    private static final int QRCODE_SIZE = 275;

    /**
     * 生成二维码插入到背景图上
     * @param content
     * @param bgPath
     * @param QRCodePath
     * @param needCompress
     * @return
     * @throws Exception
     */
    public static String encodeBgImage(String content, String bgPath, String QRCodePath,boolean needCompress) throws Exception {
        BufferedImage image = QRCodeUtil.createImage(content, bgPath, needCompress);
        mkdirs(QRCodePath);
        ImageIO.write(image, FORMAT_NAME, new File(QRCodePath));
        return QRCodePath;
    }

    /**
     * 生成二维码
     * @param content
     * @param QRCodePath
     * @param needCompress
     * @return
     * @throws Exception
     */
    public static String encode(String content, String QRCodePath,boolean needCompress) throws Exception {
        BufferedImage image = QRCodeUtil.createImage(content, needCompress);
        mkdirs(QRCodePath);
        ImageIO.write(image, FORMAT_NAME, new File(QRCodePath));
        return QRCodePath;
    }
    private static BufferedImage createImage(String content, String bgPath, boolean needCompress) throws Exception {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE);
        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) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
        if (bgPath == null || "".equals(bgPath)) {
            return image;
        }
        // 插入图片
        image =  QRCodeUtil.insertImage(bgPath, image, needCompress);
        return image;
    }

    private static BufferedImage createImage(String content, boolean needCompress) throws Exception {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE);
        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) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
        return image;
    }

    //二维码插到背景图上
    private static BufferedImage insertImage(String imgPath, BufferedImage source,boolean needCompress) throws Exception {
        Image src = ImageIO.read(new File(imgPath));   //本地图片获取
//        Image src = ImageIO.read(new URL(imgPath));    //线上图片获取
        BufferedImage background = toBufferedImage(src);
        int width = source.getWidth(null);
        int height = source.getHeight(null);
        int backgroundWidth = background.getWidth(null);
        // 插入LOGO
        Graphics2D graph = background.createGraphics();
        int x = (backgroundWidth - width) / 2 - 8;
        int y = 500;
        graph.drawImage(source, x, y, width, height, null);
        Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
        graph.setStroke(new BasicStroke(3f));
        graph.draw(shape);
        graph.dispose();
        return background;
    }

    public static BufferedImage toBufferedImage(Image image) {
        if (image instanceof BufferedImage) {
            return (BufferedImage) image;
        }
        // This code ensures that all the pixels in the image are loaded
        image = new ImageIcon(image).getImage();

        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        BufferedImage bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);

        // Copy image to buffered image
        Graphics g = bimage.createGraphics();
        // Paint the image onto the buffered image
        g.drawImage(image, 0, 0, null);
        g.dispose();
        return bimage;
    }

    /**
     * 创建文件
     * @param destPath
     */
    public static void mkdirs(String destPath) {
        File file = new File(destPath);
        // 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }
    }

    public static void main(String[] args) throws Exception {
        System.out.println("begin");
        QRCodeUtil qrCodeUtil = new QRCodeUtil();
        String content = "http://www.baidu.com";
//        String imgPath = "D:/qrCode/123.jpg";
        String QRCodePath = "E:/qrcode/" + System.currentTimeMillis() + ".jpg";
//        System.out.println(qrCodeUtil.encodeBgImage(content,imgPath,QRCodePath,true));    //生成二维码插入到背景图上
        System.out.println(qrCodeUtil.encode(content,QRCodePath,true));   //直接生成二维码
        System.out.println("end");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值