java生成二维码以及处理二维码工具类

1.生成二维码工具类

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 java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Hashtable;

public class QRCodeUtil {
    private static final String CHARSET = "utf-8";
    private static final String FORMAT_NAME = "PNG";
    // 二维码尺寸
    private static final int QRCODE_SIZE = 510;

    /**
     *
     * @param content 生成图片内容或者跳转地址
     * @param imgPath 合成图片的路径
     * @param des 生成图片存储的路径
     * @throws Exception
     */
    public static void createImage(String content, String imgPath, String des) throws Exception {
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE,
                hints);
        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);
            }
        }
        // 合成图片
        QRCodeUtil.insertImage(image, imgPath,des);
    }

    /**
     * 合成图片
     * @param image 生成的二维码图片
     * @param imgPath 背景图片的地址
     * @param destPath  //合成新图片的存储地址
     * @throws Exception
     */
    private static void insertImage(BufferedImage image, String imgPath,String destPath) throws Exception {
        File file = new File(imgPath);
        if (!file.exists()) {
            System.err.println("" + imgPath + "   该文件不存在!");
            return;
        }
        BufferedImage src = ImageIO.read(new File(imgPath));
        int width = src.getWidth(null);
        int height = src.getHeight(null);
        Graphics2D graph = src.createGraphics();
        int x = (width - QRCODE_SIZE) / 2+20;
        int y = (height - QRCODE_SIZE) / 2-80;
        graph.drawImage(image, x, y,image.getWidth(null), image.getHeight(null), null);
        graph.dispose();
        //创建文件路径
        mkdirs(destPath);
        /**
         * src 输出的图片
         * FORMAT_NAME 输出图片类型
         * destPath 输出的文件地址
         */
        ImageIO.write(src, FORMAT_NAME, new File(destPath));
    }


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


    /**
     * 直接生成并输出二维码图片
     * @param content  输出二维码的路径或者二维码跳转的路径
     * @param destPath 输出的二维码到本地磁盘的存储路径
     * @throws Exception
     */
    public static void qrCode(String content, String destPath) throws Exception{
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE,
                hints);
        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);
            }
        }
        File file = new File(destPath);
        // 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }
        // 插入图片
        ImageIO.write(image, FORMAT_NAME, new File(destPath));
    }

}

2.测试代码

 @Test
    public void codeTest() throws  Exception{
        String content="启凡科技";
        String des="f://images/"+DateUtils.dateTimeNow()+".png";
        QRCodeUtil.qrCode(content,des);
    }

你可能感兴趣的:(util)