|
|
@@ -44,15 +44,25 @@ public class QrCodeUtil {
|
|
|
|
|
|
private static final String CHARSET = "utf-8";
|
|
|
private static final String FORMAT_NAME = "JPG";
|
|
|
- /** 二维码尺寸 */
|
|
|
+ /**
|
|
|
+ * 二维码尺寸
|
|
|
+ */
|
|
|
private static final int QRCODE_SIZE = 300;
|
|
|
- /** LOGO宽度 */
|
|
|
+ /**
|
|
|
+ * LOGO宽度
|
|
|
+ */
|
|
|
private static final int WIDTH = 60;
|
|
|
- /** LOGO高度 */
|
|
|
+ /**
|
|
|
+ * LOGO高度
|
|
|
+ */
|
|
|
private static final int HEIGHT = 60;
|
|
|
- /** 字体大小 */
|
|
|
+ /**
|
|
|
+ * 字体大小
|
|
|
+ */
|
|
|
private static final int FONT_SIZE = 18;
|
|
|
- /** 字体高度 */
|
|
|
+ /**
|
|
|
+ * 字体高度
|
|
|
+ */
|
|
|
private static final int FONT_HEIGHT = 30;
|
|
|
|
|
|
private static BufferedImage createImage(String content, List<String> bottomDes, String imgPath,
|
|
|
@@ -105,12 +115,13 @@ public class QrCodeUtil {
|
|
|
graph.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
|
|
|
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
|
|
|
|
|
- for(int i = 0; i < declareText.size(); i ++){
|
|
|
+ for (int i = 0; i < declareText.size(); i++) {
|
|
|
String declare = declareText.get(i);
|
|
|
BufferedImage textImage = strToImage(declare);
|
|
|
int width = textImage.getWidth(null);
|
|
|
int height = textImage.getHeight(null);
|
|
|
- int y = QRCODE_SIZE + (declareText.size() * 20) - ((declareText.size() - i - 1) * FONT_HEIGHT);
|
|
|
+ int y =
|
|
|
+ QRCODE_SIZE + (declareText.size() * 20) - ((declareText.size() - i - 1) * FONT_HEIGHT);
|
|
|
graph.drawImage(textImage, 0, y, width, height, null);
|
|
|
}
|
|
|
|
|
|
@@ -130,7 +141,8 @@ public class QrCodeUtil {
|
|
|
}
|
|
|
|
|
|
private static BufferedImage strToImage(String str) {
|
|
|
- BufferedImage textImage = new BufferedImage(QRCODE_SIZE, FONT_HEIGHT, BufferedImage.TYPE_INT_RGB);
|
|
|
+ BufferedImage textImage = new BufferedImage(QRCODE_SIZE, FONT_HEIGHT,
|
|
|
+ BufferedImage.TYPE_INT_RGB);
|
|
|
Graphics2D g2 = (Graphics2D) textImage.getGraphics();
|
|
|
//开启文字抗锯齿
|
|
|
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
|
|
|
@@ -139,6 +151,17 @@ public class QrCodeUtil {
|
|
|
g2.clearRect(0, 0, QRCODE_SIZE, FONT_HEIGHT);
|
|
|
g2.setPaint(Color.BLACK);
|
|
|
FontRenderContext context = g2.getFontRenderContext();
|
|
|
+ /*
|
|
|
+ 注意:显示到图片上的文字,定义的字体,必须为系统已有的字体,否则汉字会因为没有对应字体而乱码
|
|
|
+ 解决方法:
|
|
|
+ 一、把相关字体上传到服务器
|
|
|
+ 1、查看jdk安装目录,echo ${JAVA_HOME}
|
|
|
+ 2、上传windows字体(宋体)到Linux服务器
|
|
|
+ windows 字体目录:“C:\WINDOWS\Fonts\simsun.ttc”
|
|
|
+ Linux jdk字体目录:“/usr/local/jdk8/jre/lib/fonts/simsun.ttc””
|
|
|
+ 二、在module中加入字体库,使用自字义的Font
|
|
|
+ 为方便每次变更运行环境时重新部署程序时,不再上传字体库,此处采用自定义Font
|
|
|
+ */
|
|
|
// Font font = new Font("宋体", Font.BOLD, FONT_SIZE)
|
|
|
Font font = FontsUtil.getSimsun(Font.BOLD, FONT_SIZE);
|
|
|
g2.setFont(font);
|
|
|
@@ -146,7 +169,8 @@ public class QrCodeUtil {
|
|
|
FontMetrics fontMetrics = FontDesignMetrics.getMetrics(font);
|
|
|
float offset = ((float) QRCODE_SIZE - fontMetrics.stringWidth(str)) / 2;
|
|
|
float y =
|
|
|
- (FONT_HEIGHT + lineMetrics.getAscent() - lineMetrics.getDescent() - lineMetrics.getLeading())
|
|
|
+ (FONT_HEIGHT + lineMetrics.getAscent() - lineMetrics.getDescent()
|
|
|
+ - lineMetrics.getLeading())
|
|
|
/ 2;
|
|
|
g2.drawString(str, (int) offset, (int) y);
|
|
|
return textImage;
|
|
|
@@ -224,7 +248,7 @@ public class QrCodeUtil {
|
|
|
File file = new File(destPath);
|
|
|
// 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
|
|
|
if (!file.exists() && !file.isDirectory()) {
|
|
|
- if(!file.mkdirs()){
|
|
|
+ if (!file.mkdirs()) {
|
|
|
throw new Exception(destPath + ", 创建失败");
|
|
|
}
|
|
|
}
|