Java之~hutool二维码工具类自定义QrCodeUtils,图片输入输出流转换

二维码工具类:

import cn.hutool.extra.qrcode.QrConfig;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.qzg.enums.room.QrCodeEnum;
import org.jeecg.modules.qzg.service.QrCodeService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * 二维码生成工具类
 */
@Slf4j
public class QrCodeUtils {
    /**
     * 二维码的横坐标
     */
    public static final int QR_CODE_X = 0;
    /**
     * 二维码的纵坐标
     */
    public static final int QR_CODE_Y = 0;

    /**
     * 透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字
     */
    public static final float ALPHA = 1.0f;


    @Autowired
    private QrCodeService qrCodeService;

    /**
     * 二维码基础设置
     * @param width 宽
     * @param height 长
     * @return
     */
    public static QrConfig getConfig(int width, int height){
        QrConfig config = new QrConfig(width, height);
        // 设置边距,既二维码和背景之间的边距
        config.setMargin(QrCodeEnum.IntEnum.QR_CODE_MARGIN.getValue());
        // 设置前景色,既二维码颜色(青色)
        config.setForeColor(Color.BLACK);
        // 设置背景色(灰色)
        config.setBackColor(Color.WHITE);
        /**高纠错级别 :像素块小,允许遮挡一定范围,但是像素块更密集*/
        config.setErrorCorrection(ErrorCorrectionLevel.H);
        return config;
    }


    /**
     * 将BufferedImage转换为InputStream
     * @param image
     * @return
     */
    public static InputStream bufferedImageToInputStream(BufferedImage image){
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, QrCodeEnum.CodeEnum.PNG.getDesc(), os);
            InputStream input = new ByteArrayInputStream(os.toByteArray());
            return input;
        } catch (IOException e) {
            log.error("将BufferedImage转换为InputStream 异常提示:",e);
        }
        return null;
    }


    /**
     * 将图片转为输入流
     * @param image
     * @return
     */
    public static InputStream stringToInputStream(String image){
        URL urlfile = null;
        try {
            urlfile = new URL(image);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        InputStream inStream = null;
        try {
            inStream = urlfile.openStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return inStream;
    }

    /**
     * 将图片转为输入流
     * @param image
     * @return
     */
    public static BufferedImage stringTobufferedImage(String image){
        URL urlfile = null;
        try {
            urlfile = new URL(image);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        InputStream inStream = null;
        BufferedImage bimg= null;
        try {
            inStream = urlfile.openStream();
            bimg = ImageIO.read(inStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bimg;
    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值