Java生成二维码
官方文档:
https://liuyueyi.github.io/quick-media
1.导入依赖
<!--二维码-->
<dependency>
<groupId>com.github.liuyueyi.media</groupId>
<artifactId>qrcode-plugin</artifactId>
<version>2.6.3</version>
</dependency>
2.简单实现
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeGenWrapper;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeOptions;
import com.google.zxing.*;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import lombok.extern.slf4j.Slf4j;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
/**
* @Author liqy
* @Date 2021/12/3 15:34
* @describe 生成二维码示例,比较简洁的,要复杂的需自己追加
* @Version 1.0
*/
@Slf4j
public class QRCodeUtil {
/**
* 普通的二维码,增加其他的需自己增加
* @param content 内容
* @return base64 格式的图片
* @throws IOException
* @throws WriterException
*/
public static String normalQR(String content) throws IOException, WriterException {
return QrCodeGenWrapper.of(content).asString();
}
/**
* 普通的中间有log的二维码,增加或改变其他的需自己扩展,例如当前的比例为7
* @param content 内容
* @param loginFile 中间的logo
* @return base64 格式的图片
* @throws IOException
* @throws WriterException
*/
public static String logoQR(String content,InputStream loginFile) throws IOException, WriterException {
return QrCodeGenWrapper.of(content)
.setLogo(loginFile)
.setLogoRate(7)
.setLogoStyle(QrCodeOptions.LogoStyle.ROUND)
.asString();
}
/**
* 带颜色的二维码,增加或改变其他的需自己扩展,例如当前颜色是红色
* @param content 内容
* @return base64 格式的图片
* @throws IOException
* @throws WriterException
*/
public static String colorQR(String content) throws IOException, WriterException {
return QrCodeGenWrapper.of(content)
.setDrawPreColor(Color.RED)
.asString();
}
/**
* 带背景图片的二维码,增加或改变其他的需自己扩展,例如当前透明度为0.5
* @param content 内容
* @param backGroundFile 背景图片
* @return base64 格式的图片
* @throws IOException
* @throws WriterException
*/
public static String backGroundQR(String content,InputStream backGroundFile) throws IOException, WriterException {
return QrCodeGenWrapper.of(content)
.setBgImg(backGroundFile)
.setBgOpacity(0.5f)
.asString();
}
/**
* 特殊样式二维码,增加或改变其他的需自己扩展,例如样式为DrawStyle.CIRCLE
* @param content 内容
* @return base64 格式的图片
* @throws IOException
* @throws WriterException
*/
public static String styleQR(String content) throws IOException, WriterException {
return QrCodeGenWrapper.of(content)
.setDrawEnableScale(true)
.setDrawStyle(QrCodeOptions.DrawStyle.CIRCLE)
.asString();
}
/**
* 特殊样式为图片的二维码,增加或改变其他的需自己扩展
* @param content 内容
* @param fillFile 填充图片
* @return base64 格式的图片
* @throws IOException
* @throws WriterException
*/
public static String styleFillQR(String content,InputStream fillFile) throws IOException, WriterException {
return QrCodeGenWrapper.of(content)
.setW(500)
.setH(500)
.setDrawEnableScale(true)
.setErrorCorrection(ErrorCorrectionLevel.H)
.setDrawStyle(QrCodeOptions.DrawStyle.IMAGE)
.addImg(1,1,fillFile)
.asString();
}
/**
* 背景为动图的二维码,增加或改变其他的需自己扩展
* @param content 内容
* @param gifFile 动图
* @return
* @throws IOException
* @throws WriterException
*/
public static String gifQR(String content,InputStream gifFile) throws IOException, WriterException {
return QrCodeGenWrapper.of(content)
.setW(500)
.setH(500)
.setBgImg(gifFile)
.setBgOpacity(0.5f)
.setPicType("pic")
.asString();
}
}
更多详情请查看官方文档