<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.github.liuyueyi.media</groupId>
<artifactId>qrcode-plugin</artifactId>
<version>2.5.2</version>
</dependency>
package com.example.util;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeGenWrapper;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeOptions;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.awt.*;
import java.io.InputStream;
public class QrCodeUtil {
public static String normal(String text) throws Exception {
return QrCodeGenWrapper.of(text).asString();
}
public static String color(String text) throws Exception {
return QrCodeGenWrapper.of(text)
.setDrawPreColor(Color.BLUE)
.asString();
}
public static String bg(String text, InputStream bgFile) throws Exception {
return QrCodeGenWrapper.of(text)
.setBgImg(bgFile)
.setBgStyle(QrCodeOptions.BgImgStyle.PENETRATE)
.setBgH(500)
.setBgW(500)
.setW(500)
.setH(500)
.asString();
}
public static String fill(String text, InputStream bgFile) throws Exception {
return QrCodeGenWrapper.of(text)
.setW(500)
.setH(500)
.setDrawEnableScale(true)
.setErrorCorrection(ErrorCorrectionLevel.H)
.setDrawStyle(QrCodeOptions.DrawStyle.IMAGE)
.addImg(1, 1, bgFile)
.asString();
}
public static String gif(String text, InputStream bgFile) throws Exception {
return QrCodeGenWrapper.of(text)
.setW(500)
.setH(500)
.setBgImg(bgFile)
.setBgOpacity(0.5f)
.setPicType("gif")
.asString();
}
public static String style(String text, InputStream bgFile) throws Exception {
return QrCodeGenWrapper.of(text)
.setBgH(500)
.setBgW(500)
.setW(500)
.setH(500)
.setDrawEnableScale(true)
.setDrawStyle(QrCodeOptions.DrawStyle.CIRCLE)
.asString();
}
}
- 前端请求代码省略(data是调用工具类返回的base64码)`
// 前端解析方式大概如下:
$("#view_photo").attr("src","data:image/jpeg;base64,"+data);`
|