package ink.wgink.util;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

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;

public class QRCodeUtil {

    public static BufferedImage createQrCode(int width, int height, String content, String logoPath) {
        Map<EncodeHintType, Object> qrParams = new HashMap<>(3);
        // 编码
        qrParams.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        // 纠错等级
        qrParams.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        // 边框
        qrParams.put(EncodeHintType.MARGIN, 1);
        // 生成二维码
        BufferedImage bufferedImage = null;
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,
                    width, height);
            bufferedImage = getBufferImage(bitMatrix);
            if (null != logoPath && !logoPath.isEmpty()) {
                addLogo(bufferedImage, logoPath);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bufferedImage;
    }

    private static BufferedImage getBufferImage(BitMatrix bitMatrix) {
        int bmWidth = bitMatrix.getWidth();
        int bmHeight = bitMatrix.getHeight();
        BufferedImage bufferedImage = new BufferedImage(bmWidth, bmHeight,
                BufferedImage.TYPE_INT_ARGB);
        for (int x = 0; x < bmWidth; x++) {
            for (int y = 0; y < bmHeight; y++) {
                bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
        return bufferedImage;
    }

    private static void addLogo(BufferedImage bufferedImage, String logoPath) throws IOException {
        Graphics2D graphics2d = bufferedImage.createGraphics();
        Image logo = ImageIO.read(new File(logoPath));
        // 设置比例
        int logoWidth = bufferedImage.getWidth() / 6;
        int logoHeight = bufferedImage.getHeight() / 6;

        // 填充logo
        int x = bufferedImage.getWidth() / 2 - logoWidth / 2;
        int y = bufferedImage.getHeight() / 2 - logoHeight / 2;
        // 设置抗锯齿
        graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        // 填充填充背景
        graphics2d.setColor(Color.WHITE);
        graphics2d.fillRoundRect(x, y, logoWidth, logoHeight, 10, 10);

        // 画边框
        graphics2d.setColor(new Color(220, 220, 220));
        graphics2d.drawRoundRect(x, y, logoWidth, logoHeight, 10, 10);

        // 画LOGO
        graphics2d.drawImage(logo, x + 2, y + 2, logoWidth - 4, logoHeight - 4, null);
        graphics2d.dispose();
        logo.flush();
    }

}
