1. 程式人生 > >一個好用的二維碼外掛

一個好用的二維碼外掛

新增maven依賴

  <!--二維碼-->
        <dependency>
            <groupId>org.iherus</groupId>
            <artifactId>qrext4j</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>
package com.demo.action;

import org.apache.commons.io.IOUtils;
import org.iherus.codegen.qrcode.QrcodeConfig;
import org.iherus.codegen.qrcode.QreyesFormat;
import org.iherus.codegen.qrcode.SimpleQrcodeGenerator;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 二維碼服務
 *
 * @author noob
 */
@RestController
@RequestMapping("/qrcode")
public class QrCodeController {

    private static QrcodeConfig config = new QrcodeConfig()
            .setBorderSize(2)
            .setPadding(10)
            .setMasterColor("#00BFFF")
            .setLogoBorderColor("#B0C4DE")
            .setCodeEyesPointColor("#BC8F8F")
            .setCodeEyesFormat(QreyesFormat.DR2_BORDER_C_POINT);

    @GetMapping("/getCode")
    public void qrcodeStream(@RequestParam("content") String content,
                             HttpServletRequest request,
                             HttpServletResponse response) {
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            new SimpleQrcodeGenerator().generate(content).toStream(out);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(out);
        }
    }

    /**
     * 生成二維碼[彩色]
     *
     * @param content
     * @param request
     * @param response
     */
    @GetMapping("/getColorCode")
    public void qrcodeConfigStream(@RequestParam("content") String content,
                                   HttpServletRequest request,
                                   HttpServletResponse response) {

        OutputStream out = null;
        try {
            out = response.getOutputStream();
            new SimpleQrcodeGenerator(config).generate(content).toStream(out);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(out);
        }
    }

    /**
     * 生成帶logo的二維碼[黑色]
     *
     * @param content
     * @param logo
     * @param request
     * @param response
     */
    @GetMapping("/getLogoCode")
    public void qrcodeLogoStream(@RequestParam("content") String content,
                                 @RequestParam("logo") String logo,
                                 HttpServletRequest request,
                                 HttpServletResponse response) {

        OutputStream out = null;
        try {
            out = response.getOutputStream();
            new SimpleQrcodeGenerator().setRemoteLogo(logo).generate(content).toStream(out);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(out);
        }
    }

    /**
     * 自定義二維碼
     *
     * @param content
     * @param logo
     * @param request
     * @param response
     */
    @GetMapping("/get/config/logo/code")
    public void qrcodeConfigStream(@RequestParam("content") String content,
                                   @RequestParam("logo") String logo,
                                   HttpServletRequest request,
                                   HttpServletResponse response) {
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            new SimpleQrcodeGenerator(config).setRemoteLogo(logo).generate(content).toStream(out);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(out);
        }
    }
}