栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2021-10-04

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021-10-04

生成七种二维码形状

1、普通二维码
2、logo二维码
3、彩色二维码
4、背景二维码
5、特殊形状二维码
6、图片填充二维码
7、gif二维码

1、创建一个工程

2、添加依赖

        
            org.springframework.boot
            spring-boot-starter-web
            2.3.2.RELEASE
        
        
            org.springframework.boot
            spring-boot-autoconfigure
            2.3.2.RELEASE
        
        
            com.google.zxing
            core
            2.3.0
        
        
            com.google.zxing
            javase
            2.3.0
        
        
            com.github.liuyueyi.media
            qrcode-plugin
            2.5.2
        
        
            commons-lang
            commons-lang
            2.6
        
    
3、创建启动类
@SpringBootApplication
public class qrcode {
    public static void main(String[] args) {
        SpringApplication.run(qrcode .class,args);
    }
}
4、创建static静态文件包,写html以及核心配置文件application.yml




    
    
    
    单文件上传




    选择图片
    
    
    
    
5、创建controller包,创建QrcodeController类
package com.huan.controller;

import com.huan.util.QrCodeUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


@RestController
public class QrcodeController {

    @PostMapping("/create")
    public String createQrcode(@RequestParam(value = "logoFile",required = false) MultipartFile file,
                               @RequestParam(value = "text")String text,
                               @RequestParam(value = "flag")String flag){
        try {
            if (file == null){

                if ("normal".equals(flag)){
                    return QrCodeUtil.normal(text); //生成普通二维码
                }else if ("color".equals(flag)){
                    return QrCodeUtil.color(text);  //彩色二维码
                }else if ("style".equals(flag)){
                    return QrCodeUtil.style(text);  //特殊形状二维码
                }
            }else {
                if ("logo".equals(flag)){
                    return QrCodeUtil.logo(text,file.getInputStream());//生成logo二维码
                }
                if ("background".equals(flag)){
                    return QrCodeUtil.bg(text,file.getInputStream()); //背景色二维码
                }
                if ("imageFill".equals(flag)){
                    return QrCodeUtil.fill(text,file.getInputStream()); //图片填充二维码
                }
                if ("gif".equals(flag)){
                    return QrCodeUtil.gif(text,file.getInputStream()); //动态二维码
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

6、application.yml配置文件配置信息可以配置上传文件大小,如下:
spring:
  servlet:
    multipart:
      max-file-size: 50MB
      max-request-size: 100MB
7、创建util包,创建QrCodeUtil类
package com.huan.util;

import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeGenWrapper;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeOptions;
import com.google.zxing.WriterException;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.awt.*;
import java.io.IOException;
import java.io.InputStream;


public class QrCodeUtil {

    //普通二维码
    public static String normal(String text) throws IOException, WriterException {
        return QrCodeGenWrapper.of(text)
                .asString();
    }

    //logo二维码
    public static String logo(String text, InputStream logoFile) throws IOException, WriterException {
        return QrCodeGenWrapper.of(text)
                .setLogo(logoFile)
                .setLogoRate(7)
                .setLogoStyle(QrCodeOptions.LogoStyle.ROUND)
                .asString();
    }

    //彩色二维码
    public static String color(String text) throws IOException, WriterException {
        return QrCodeGenWrapper.of(text)
                .setDrawPreColor(Color.RED)
                .asString();
    }

    //背景色二维码
    public static String bg(String text,InputStream bgFile) throws IOException, WriterException {
        return QrCodeGenWrapper.of(text)
                .setBgImg(bgFile)
                .setBgStyle(QrCodeOptions.BgImgStyle.PENETRATE)
                .setBgH(500)
                .setBgW(500)
                .setW(500)
                .setH(500)
                .asString();
    }

    //特殊二维码
    public static String style(String text) throws IOException, WriterException {
        return QrCodeGenWrapper.of(text)
                .setBgH(500)
                .setBgW(500)
                .setW(500)
                .setH(500)
                .setDrawEnableScale(true)
                .setDrawStyle(QrCodeOptions.DrawStyle.CIRCLE)
                .asString();
    }

    //图片填充二维码
    public static String fill(String text,InputStream bgFile) throws IOException, WriterException {
        return QrCodeGenWrapper.of(text)
                .setW(500)
                .setH(500)
                .setDrawEnableScale(true)
                .setErrorCorrection(ErrorCorrectionLevel.H)
                .setDrawStyle(QrCodeOptions.DrawStyle.IMAGE)
                .addImg(1,1,bgFile)
                .asString();
    }

    //gif二维码
    public static String gif(String text,InputStream bgFile) throws IOException, WriterException {
        return QrCodeGenWrapper.of(text)
                .setW(500)
                .setH(500)
                .setBgImg(bgFile)
                .setBgOpacity(0.5f)
                .setPicType("gif")
                .asString();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/293204.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号