maven引用外部qrcode.jar,实现二维码生成并且上传至阿里云OSS

下载jar

https://files.cnblogs.com/files/bigroc/QRCode.zip

放置项目一个目录下面,我的是lib下

maven引用外部qrcode.jar,实现二维码生成并且上传至阿里云OSS_第1张图片

maven配置



    4.0.0

    com.robinson
    54bin
    0.0.1-SNAPSHOT
    jar

    54bin
    54bin project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.9.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
                
        
            qrcode
            qrcode
            1.0.0
            system
            ${basedir}/lib/Qrcode.jar
        



    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.5
                
                    true
                    false
                

                
                    
                        mysql
                        mysql-connector-java
                        5.1.38
                    
                
            

            
                org.apache.maven.plugins
                maven-resources-plugin
                
                    UTF-8
                    
                    
                        pem
                        pfx
                        p12
                    
                
            

        
       

    



生成二维码工具类

package com.robinson.common.utils;

import com.robinson.common.exception.BusinessException;
import com.swetake.util.Qrcode;
import org.apache.commons.lang3.StringUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * @author wangbo
 */
public class CreateQRcodeUtil {

    /**
     * 根据数据创建二维码并返回阿里云文件路径
     * @param qrData
     * @return
     * @throws Exception
     */
    public String createQRcode(String qrData) throws Exception {
        if (StringUtils.isBlank(qrData)){
            throw new BusinessException("塞入数据不能为空");
        }
        Qrcode x = new Qrcode();
        //纠错等级
        x.setQrcodeErrorCorrect('M');
        //N代表数字,A代表a-Z,B代表其他字符
        x.setQrcodeEncodeMode('B');
        //版本
        x.setQrcodeVersion(7);
        int width = 67 + 12 * (7 - 1);
        int height = 67 + 12 * (7 - 1);
        BufferedImage bufferedImage = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = bufferedImage.createGraphics();
        graphics2D.setBackground(Color.WHITE);
        graphics2D.setColor(Color.black);
        graphics2D.clearRect(0, 0, width, height);
        //偏移量
        int pixoff = 2;

        //向画板内存放数据
        byte[] d = new byte[0];
        try {
            d = qrData.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (d.length > 0 && d.length < 120) {
            boolean[][] s = x.calQrcode(d);

            for (int i = 0; i < s.length; i++) {
                for (int j = 0; j < s.length; j++) {
                    if (s[j][i]) {
                        graphics2D.fillRect(j * 3 + pixoff, i * 3, 3, 3);
                    }
                }
            }
        }
        graphics2D.dispose();
        bufferedImage.flush();
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
            OSSAPI ossapi = new OSSAPI();
            ByteArrayInputStream parse = parse(byteArrayOutputStream);
            String url = ossapi.saveImg(parse, 6);
            return url;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private ByteArrayInputStream parse(OutputStream out) throws Exception {
        ByteArrayOutputStream baos = (ByteArrayOutputStream) out;
        ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
        return swapStream;
    }
}

 

你可能感兴趣的:(小技巧)