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.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import static java.util.Objects.nonNull;

public class QrCodeZXingUtils {
    private static final int QRCOLOR = 0xFF000000; // 默认是黑色
    private static final int BGWHITE = 0xFFFFFFFF; // 背景颜色

    private static final int WIDTH = 200; // 二维码宽
    private static final int HEIGHT = 200; // 二维码高

    // 用于设置QR二维码参数
    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的二维码图片
    /***
     *@param logoFile  logo图地址
     * @param codeFile  二维码生成地址
     * @param qrUrl 扫描二维码方位地址
     * */
    public static void drawLogoQRCode(File logoFile, File codeFile, String qrUrl) {
        try {
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            // 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
            BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
            BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
            // 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色
            for (int x = 0; x < WIDTH; x++) {
                for (int y = 0; y < HEIGHT; y++) {
                    image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
                }
            }
            int width = image.getWidth();
            int height = image.getHeight();
            if (nonNull(logoFile) && logoFile.exists()) {
                // 构建绘图对象
                Graphics2D g = image.createGraphics();
                // 读取Logo图片
                BufferedImage logo = ImageIO.read(logoFile);
                // 开始绘制logo图片
                g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null);
                g.dispose();
                logo.flush();
            }
            image.flush();
            ImageIO.write(image, "png", codeFile);
            zip(image);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // 添加边框,未成功,待测试
    private static BufferedImage addBorder(BufferedImage image) {
        int borderWidth = 10; // 边框宽度
        BufferedImage borderedImage = new BufferedImage(image.getWidth() + borderWidth * 2,
                image.getHeight() + borderWidth * 2,
                BufferedImage.TYPE_INT_RGB);
        // 使用白色填充边框区域
        for (int x = 0; x < borderedImage.getWidth(); x++) {
            for (int y = 0; y < borderedImage.getHeight(); y++) {
                if (x < borderWidth || x >= borderWidth + image.getWidth() ||
                        y < borderWidth || y >= borderWidth + image.getHeight()) {
                    borderedImage.setRGB(x, y, 0xFFFFFFFF); // 白色
                }
            }
        }
        // 将二维码图像复制到带有边框的图像上
        for (int x = borderWidth, y = borderWidth;
             x - borderWidth < image.getWidth() && y - borderWidth < image.getHeight(); x++, y++) {
            borderedImage.setRGB(x, y, image.getRGB(x - borderWidth, y - borderWidth));
        }
        return borderedImage;
    }
    public static void main(String[] args) throws  Exception {
        //头像logo
        File logoFile = new File("C:\\Users\\86181\\Desktop\\2.png");
        //生成二维码地址
        File QrCodeFile = new File("C:\\Users\\86181\\Desktop\\3.png");
        String url = "https://www.baidu.com/";
        drawLogoQRCode(logoFile, QrCodeFile, url);

        //mergeImage("C:\\Users\\86181\\Desktop\\66.png", "C:\\Users\\86181\\Desktop\\3.png", "C:\\Users\\86181\\Desktop\\test.png","30", "100");
    }



    /***
     * 二维码嵌套背景图的方法
     *@param bigPath 背景图 - 可传网络地址
     *@param smallPath 二维码地址 - 可传网络地址
     *@param newFilePath 生成新图片的地址
     * @param  x 二维码x坐标
     *  @param  y 二维码y坐标
     * */
    public static void mergeImage(String bigPath, String smallPath, String newFilePath,String x, String y) throws IOException {

        try {
            BufferedImage small;
            BufferedImage big;
            if (bigPath.contains("http://") || bigPath.contains("https://")) {
                URL url = new URL(bigPath);
                big = ImageIO.read(url);
            } else {
                big = ImageIO.read(new File(bigPath));
            }


            if (smallPath.contains("http://") || smallPath.contains("https://")) {

                URL url = new URL(smallPath);
                small = ImageIO.read(url);
            } else {
                small = ImageIO.read(new File(smallPath));
            }

            Graphics2D g = big.createGraphics();

            float fx = Float.parseFloat(x);
            float fy = Float.parseFloat(y);
            int x_i = (int) fx;
            int y_i = (int) fy;
            g.drawImage(small, x_i, y_i, small.getWidth(), small.getHeight(), null);
            Font font = new Font("SimSun", Font.BOLD, 36); // 使用宋体字体,设置字体大小为36
            g.setFont(font);
            g.setColor(Color.BLACK); // 设置文字颜色为红色
            String text = "你好,世界!"; // 要添加的文字内容
            g.drawString(text, 300, 400); // 在指定位置绘制文字
            g.dispose();
            ImageIO.write(big, "png", new File(newFilePath));

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

    public static void zip(BufferedImage image) {
        try {
            File outputFile = new File("output.png");
            ImageIO.write(image, "png", outputFile);

            FileOutputStream fos = new FileOutputStream("imageFolder.zip");
            ZipOutputStream zos = new ZipOutputStream(fos);
            ZipEntry zipEntry = new ZipEntry("output.png");
            zos.putNextEntry(zipEntry);

            FileInputStream fis = new FileInputStream(outputFile);

            byte[] buffer = new byte[1024];
            int length;
            while ((length = fis.read(buffer)) > 0) {
                zos.write(buffer, 0, length);
            }

            fis.close();
            zos.closeEntry();
            zos.close();

            // Delete the original file
            outputFile.delete();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}