1、普通二维码
2、logo二维码
3、彩色二维码
4、背景二维码
5、特殊形状二维码
6、图片填充二维码
7、gif二维码
3、创建启动类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
@SpringBootApplication
public class qrcode {
public static void main(String[] args) {
SpringApplication.run(qrcode .class,args);
}
}
4、创建static静态文件包,写html以及核心配置文件application.yml
单文件上传
选择图片
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();
}
}



