Browse Source

修改图片插入字体功能

lwx_dev
erdanergou 3 years ago
parent
commit
89d2ad94d9
  1. 17
      src/main/java/com/dreamchaser/depository_manage/utils/CreateQrCodeUtil.java
  2. BIN
      src/main/resources/static/lib/font-awesome-4.7.0/fonts/simsun.ttc

17
src/main/java/com/dreamchaser/depository_manage/utils/CreateQrCodeUtil.java

@ -15,6 +15,7 @@ import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.http.util.TextUtils;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
@ -22,9 +23,7 @@ import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
@ -169,7 +168,17 @@ public class CreateQrCodeUtil {
Color color = new Color(0, 0, 0);
g2d.setColor(color);
// 字体、字型、字号
g2d.setFont(new Font("Droid Sans", Font.PLAIN, 16));
Font font = null;
try {
ClassPathResource classPathResource = new ClassPathResource("static/lib/font-awesome-4.7.0/fonts/simsun.ttc");
InputStream inputStream =classPathResource.getInputStream();
font = Font.createFont(Font.PLAIN, inputStream);
font = font.deriveFont(Font.PLAIN, 16);
} catch (Exception e) {
font = new Font("Droid Sans", Font.PLAIN, 16);
}
g2d.setFont(font);
//文字长度
int strWidth = g2d.getFontMetrics().stringWidth(code);
//总长度减去文字长度的一半 (居中显示)

BIN
src/main/resources/static/lib/font-awesome-4.7.0/fonts/simsun.ttc

Binary file not shown.
Loading…
Cancel
Save