1. 程式人生 > >java 生成二維碼圖片

java 生成二維碼圖片

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.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: xiaogang
 * @date: 2018/8/1 0:27
 * To change this template use File | Settings | File Templates.
 * Description:
 */
public class GenerateQrCodeUtil {
    /**
     * 背景色
     */
    private static final int WHITE = 0xFFFFFFFF;

    /**
     * 前景色
     */
    private static final int BLACK = 0xFF000000;

    /**
     * 二維碼寬度
     */
    private static int width = 168;

    /**
     * 二維碼高度
     */
    private static int height = 168;

    /**
     * logo圖片所在的classpath下的路徑,如果該路徑下沒有圖片,則不會再二維碼中新增logo圖
     */
    private static String logoPath = "/static/logo.png";

    /**
     * 將指定資訊生成圖片物件
     * @param content 二維碼裡面的資訊
     * @return
     * @throws WriterException
     * @throws IOException
     */
    public static BufferedImage drawQrCode(String content) throws WriterException, IOException {
        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        BarcodeFormat format = BarcodeFormat.QR_CODE;
        Map<EncodeHintType, Object> hints = new HashMap<>();
        EncodeHintType key = EncodeHintType.CHARACTER_SET;
        hints.put(key,"utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.MARGIN,1);
        BitMatrix bitMatrix = multiFormatWriter.encode(content, format, width, height, hints);
        BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = bufferedImage.createGraphics();
        for (int i = 0; i < width; i++) {
            for (int y = 0; y < height; y++) {
                bufferedImage.setRGB(i,y,bitMatrix.get(i,y) ? BLACK : WHITE);
            }
        }

        if (logoPath != null) {
            InputStream resourceAsStream = GenerateQrCodeUtil.class.getResourceAsStream(logoPath);
            if (resourceAsStream != null && resourceAsStream.available() > 1) {
                BufferedImage logoImg = ImageIO.read(resourceAsStream);
                int logoWidth = 40;
                int logoHeight = 40;
                int x = (width - logoWidth) / 2;
                int y = (height - logoHeight) / 2;
                graphics.drawImage(logoImg,x,y,logoWidth,logoHeight,null);
            }else {
                System.out.println("指定路徑下沒有找到logo圖片");
            }
        }
        return bufferedImage;
    }

    /**
     * 將指定資訊生成二維碼圖片檔案
     * @param content 二維碼裡面的資訊
     * @param filePath 檔案路徑和檔名
     * @return
     * @throws IOException
     * @throws WriterException
     */
    public static Boolean generateQrCodeImage(String content,String filePath) throws IOException, WriterException {
        BufferedImage bufferedImage = drawQrCode(content);
        boolean write = ImageIO.write(bufferedImage, "png", new File(filePath));
        return write;
    }

    private GenerateQrCodeUtil(){}
}