package com.dayou.utils;

import com.dayou.configuration.DfsConfig;
import com.dayou.enums.MainBusinessEnum;
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.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

/**
 * 类说明：
 *
 * @author: wucl
 * @since: 2023/11/8
 * created with IntelliJ IDEA.
 */
@Slf4j
public class QRCodeUtil {


    /**
     * 生成二维码输出到指定的文件
     *
     * @param url   二维码的内容
     * @param width 二维码的宽度/高度
     * @param file  文件
     */
    public static void write(String url, int width, File file) throws IOException, WriterException {
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.MARGIN, 2);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, width, hints);
        MatrixToImageWriter.writeToFile(bitMatrix,"png",file);
    }


//    public static void main(String[] args) {
//        try {
//            String dateIndexFileName = FileUploadUtils.createDateIndexFileName("validate_code.png");
//            File absoluteFile = FileUploadUtils.getAbsoluteFile("G:\\code\\", dateIndexFileName);
//            write("http://localhost:9518/#/prod/validate?businessType=PERSONAL_BUSINESS&targetId=1",200,absoluteFile);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        } catch (WriterException e) {
//            throw new RuntimeException(e);
//        }
//    }

}
