package com.ruoyi.common.utils;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;

import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;


/**
 * 二维码
 */
public class QRCodeUtils {

    /**
     * 实验二维码
     *
     * @param shareId 实验分享ID
     * @param isDesign 是否设计：0-未设计，1-已设计
     * @return 七牛云图片链接
     */
    public static String createQRCode(String shareId, String isDesign) {
        // String url = "https://www.baidu.com";
        String url = "https://mytestyanshi.wucunfang.com?id=" + shareId;
        String qrCodePath = "";
        ByteArrayOutputStream bos = null;
        try {
            QrConfig config = new QrConfig();
            // 高纠错级别
            config.setErrorCorrection(ErrorCorrectionLevel.H);
            config.setHeight(300);
            config.setWidth(300);
            //二维码
            BufferedImage generate = QrCodeUtil.generate(url, config);
            bos = new ByteArrayOutputStream();  //获得图片地址
            //获得其byte数组
            ImageIO.write(generate, "jpg", bos);
            byte[] bytes = bos.toByteArray();
            return new ImgUpload().upload(bytes, "ORDER_QR_CODE");
        } catch (Exception e) {
            e.printStackTrace();
            return qrCodePath;
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
