package cn.ezhizao.framework.qrCode;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeException;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import cn.ezhizao.common.utils.WaterMakerUtils;
import com.google.zxing.BarcodeFormat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author zhushan
 */
@Service
@Slf4j
public class QrCodeService {
    @Resource
    private QrConfig config;

    @Resource
    private QrCodeDemo qrCoder;
    @Resource
    private BarCodeDemo barCodeDemo;

    @Resource
    private WaterMakerUtils waterMakerUtils;
    /**
     *
     * @param content
     * @param filePath
     */
    public void createCodeToFile(String content, String filePath) {
        try {
            QrCodeUtil.generate(content, config, FileUtil.file(filePath));
        } catch (QrCodeException e) {
            e.printStackTrace();
        }
    }

    public void createBarCodeToFile(String content, String filePath) {
        try {
            QrCodeUtil.generate(content, BarcodeFormat.CODE_39, config);
        } catch (QrCodeException e) {
            e.printStackTrace();
        }
    }

    public void createNewCodeToFile(String content, String filePath,String footer) {
        BufferedImage bufferedImage = qrCoder.createQrCode(null, null, content, footer);
        createImage(bufferedImage, filePath);
    }

    // 生成图片文件
    public static void createImage(BufferedImage image, String fileLocation) {
        if (image != null) {
            try {
                ImageIO.write(image, "png", new File(fileLocation));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
