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

下载jar

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

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

maven配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.robinson</groupId>
    <artifactId>54bin</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>54bin</name>
    <description>54bin project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
                
        <dependency>
            <groupId>qrcode</groupId>
            <artifactId>qrcode</artifactId>
            <version>1.0.0</version>
            <scope>system</scope>
            <systemPath>${basedir}/lib/Qrcode.jar</systemPath>
        </dependency>



    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>false</overwrite>
                </configuration>

                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.38</version>
                    </dependency>
                </dependencies>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <!-- 过滤后缀为pem、pfx的证书文件 -->
                    <nonFilteredFileExtensions>
                        <nonFilteredFileExtension>pem</nonFilteredFileExtension>
                        <nonFilteredFileExtension>pfx</nonFilteredFileExtension>
                        <nonFilteredFileExtension>p12</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                </configuration>
            </plugin>

        </plugins>
       

    </build>


</project>

生成二维码工具类

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;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值