com.google.zxing
core
3.3.3
com.google.zxing
javase
3.3.3
import cn.com.hwqh.user.util.tool.UuidUtil;
import cn.com.hwqh.user.util.utility.MyConfiguration;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
/**
* @ClassName: GenerateQrCodeUtil
* @version: 1.0
* @description: 二维码工具类
* @author: zhaonian
* @create: 2019-10-23 18:44
**/
public class GenerateQrCodeUtil {
/**
* 文件地址
*/
public static final String FILEPATH = MyConfiguration.create().getString("html.filepath");
/**
* 传入文字生成二维码
* @param content
* @return
*/
public static String GenerateQrCodeUtil(String content){
String s="";
System.out.println(1234);
final int width = 300;
final int height = 300;
final String format = "png";
// final String content = content;
//定义二维码的参数
HashMap hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hints.put(EncodeHintType.MARGIN, 2);
//生成二维码
try{
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
StringBuilder name=new StringBuilder().append(UuidUtil.get32UUID());
StringBuilder path =new StringBuilder();
path.append(FILEPATH);
path.append(name);
path.append(".png");
File f = new File(path.toString());
// 创建文件夹
if (!f.exists()) {
f.mkdirs();
}
Path file = new File(path.toString()).toPath();
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
//拼接图片地址
name.append(".png");
name.insert(0,"/static/inviteCode/");
s = name.toString();
return s;
}catch(Exception e){
e.printStackTrace();
}
return s;
}
public static void main(String[] args) {
//测试
String s = GenerateQrCodeUtil.GenerateQrCodeUtil("http://47.103.142.74/static/invite.html"+"?inviteCode="+"r7502");
System.out.println(s);
}
}
其中 html.filepath 是本地保存的地址
1.电脑本地地址
#===========二维码地址配置===========
#html
html.filepath=E:/huawen/huawen-cloud-parent/src/main/webapp/dev/videos/
2.线上linux地址配置
#===========二维码地址配置===========
#html
html.filepath=/root/huawen-cloud-parent/huawen-cloud-user/src/main/webapp/dev/images/
其中SwaggerConfigure配置类需要配置虚拟路径
registry.addResourceHandler("/static/inviteCode/**").addResourceLocations("file:"+htmlPath);
import cn.com.hwqh.user.loginsecurity.LoginInterceptor;
import com.google.common.base.Predicates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;
/**
* Swagger2 的配置文件
* Created by qhs
*/
@Configuration
@EnableSwagger2
@ConditionalOnProperty(
prefix = "spring.swagger",
name = {"enabled"},
matchIfMissing = true,
havingValue = "true"
)
public class SwaggerConfigure extends WebMvcConfigurationSupport {
@Autowired
LoginInterceptor loginInterceptor;
@Value("${html.filepath}")
private String htmlPath;
/**
* 配置拦截user/下的所有路径
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
// registry.addInterceptor(loginInterceptor).addPathPatterns("/user/**");
//不拦截接口
registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns("/login/**").excludePathPatterns("/generate/**").excludePathPatterns("/problem/**").excludePathPatterns("/inquireBack/**");
super.addInterceptors(registry);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/inviteCode/**").addResourceLocations("file:"+htmlPath);
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
@Bean
public Docket api(){
ParameterBuilder t = new ParameterBuilder();
ParameterBuilder t1 = new ParameterBuilder();
ParameterBuilder driveid = new ParameterBuilder();
ParameterBuilder h = new ParameterBuilder();
ParameterBuilder k = new ParameterBuilder();
List pars = new ArrayList();
t.name("t").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
t1.name("t1").description("临时令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
h.name("h").description("请求头信息").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
k.name("k").description("请求头信息").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
driveid.name("driveid").description("设备号id").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
pars.add(t.build());
pars.add(t1.build());
pars.add(h.build());
pars.add(k.build());
pars.add(driveid.build());
return new Docket(DocumentationType.SWAGGER_2)
// 选择那些路径和api会生成document
.select()
//对所有api进行监控
.apis(RequestHandlerSelectors.any())
//配置swagger扫描包路径
.apis(RequestHandlerSelectors.basePackage("com.user.controller"))
//错误路径不监控
.paths(Predicates.not(PathSelectors.regex("/error.*")))
//actuator路径不监控
.paths(Predicates.not(PathSelectors.regex("/actuator.*")))//actuator路径不监控
.build()
.globalOperationParameters(pars)
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//页面标题
.title("个人中心2.0")
//描述
.description("服务")
.termsOfServiceUrl("http://www.test.com")
//版本号
.version("1.0")
.build();
}
}
访问地址:
{ip}/static/inviteCode/165d656d8988d9898f8.jpg 访问