package com.bohuikeji.frame.module.admin.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
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.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

/**
 * 条形码，二维码工具类
 */
public class ZxingUtils {

    /**
     * 生成条形码
     * @param content
     * @param width
     * @param height
     * @return
     */
    public static File createBarCode(String content, int width, int height){
        BitMatrix bitMatrix = null;
        File file=null;
        try {
            bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.CODE_128, width, height);
            //FileOutputStream out=new FileOutputStream(File.createTempFile("temp","png"));
            file=File.createTempFile("temp",".png");
            MatrixToImageWriter.writeToFile(bitMatrix,"png",file);
        } catch (WriterException | IOException e) {
            e.printStackTrace();
        }
        return file;
    }
    /**
     * 生成二维码
     */
    public static File createQREncode(String content, int width, int height){
        String format = "png";// 图像类型
        Map<EncodeHintType, Object> hints = new HashMap<>();
        //内容编码格式
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        // 指定纠错等级
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        //设置二维码边的空度，非负数
        hints.put(EncodeHintType.MARGIN, 0);
        File file=null;
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            file=File.createTempFile("temp",".png");
            MatrixToImageWriter.writeToFile(bitMatrix,"png",file);
        }catch (WriterException | IOException e) {
            e.printStackTrace();
        }
        return file;

    }

    /**
     * 获取二维码image对象
     *
     * @param content
     * @param qrCodeSize
     * @return
     */
    public static BufferedImage getQrCodeImage(String content, int qrCodeSize) throws WriterException {
        //设置二维码纠错级别ＭＡＰ
        Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);  // 矫错级别
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        //创建比特矩阵(位矩阵)的QR码编码的字符串
        BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
        // 使BufferedImage勾画QRCode  (matrixWidth 是行二维码像素点)
        int matrixWidth = byteMatrix.getWidth();
        BufferedImage image = new BufferedImage(matrixWidth-80 , matrixWidth-80 , BufferedImage.TYPE_INT_RGB);
        image.createGraphics();
        Graphics2D graphics = (Graphics2D) image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, matrixWidth, matrixWidth);
        // 使用比特矩阵画并保存图像
        graphics.setColor(Color.BLACK);
        for (int i = 0; i < matrixWidth; i++) {
            for (int j = 0; j < matrixWidth; j++) {
                if (byteMatrix.get(i, j)) {
                    graphics.fillRect(i - 40, j - 40, 1, 1);
                }
            }
        }
        return image;
    }

}
