Browse Source

验证码图片自定义字体

andy 1 year ago
parent
commit
7c47cf5928
1 changed files with 29 additions and 12 deletions
  1. 29 12
      mainFactory/src/main/java/org/bfkj/utils/GenerateImage.java

+ 29 - 12
mainFactory/src/main/java/org/bfkj/utils/GenerateImage.java

@@ -1,9 +1,13 @@
 package org.bfkj.utils;
 
+import org.springframework.core.io.ClassPathResource;
+
 import javax.imageio.ImageIO;
 import java.awt.*;
 import java.awt.image.BufferedImage;
 import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.security.SecureRandom;
 import java.util.Base64;
@@ -21,7 +25,7 @@ public class GenerateImage {
     };
     private static SecureRandom RANDOM = new SecureRandom();
 
-    public static String createCode(int size, String source){
+    public static String createCode(int size, String source) {
         StringBuilder verifyCode = new StringBuilder(size);
         int codesLen = source.length();
         for (int i = 0; i < size; i++) {
@@ -31,35 +35,48 @@ public class GenerateImage {
     }
 
     public static String stringToImage(String code) throws IOException {
-        try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()){
+        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
             BufferedImage image = createImage(code);
             ImageIO.write(image, IMAGE_FORMAT, outputStream);
 
             byte[] data = outputStream.toByteArray();
-            return "data:image/"+IMAGE_FORMAT+";base64," + Base64.getEncoder().encodeToString(data);
+            return "data:image/" + IMAGE_FORMAT + ";base64," + Base64.getEncoder().encodeToString(data);
         }
     }
+
     private static BufferedImage createImage(String verifyCode) {
         BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
         Graphics graphics = image.getGraphics();
 
         graphics.setColor(Color.WHITE);
         graphics.fillRect(0, 0, WIDTH, HEIGHT);
-        Font font = graphics.getFont();
 
-        graphics.setFont(new Font(font.getFamily(),Font.BOLD,HEIGHT-10));
 
-        int desX,desY,distance=16;
+        Font font = null;
+        try {
+            File fontFile = new ClassPathResource("font/SourceHanSansSC-Regular-2.otf").getFile();
+            font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
+//            改变字体大小
+            font = font.deriveFont((float) (HEIGHT - 10));
+        } catch (FontFormatException | IOException e) {
+            font = graphics.getFont();
+            if (font != null) {
+                font.deriveFont((float) HEIGHT - 10);
+            }
+        }
+        graphics.setFont(font);
+
+        int desX, desY, distance = 16;
         for (int i = 0; i < verifyCode.length(); i++) {
             graphics.setColor(COLORS[RANDOM.nextInt(COLORS.length)]);
-            desX = (i+1)*distance;
-            desY = distance+RANDOM.nextInt(HEIGHT-2*distance);
-            graphics.drawString(Character.toString(verifyCode.charAt(i)),desX,desY);
+            desX = (i + 1) * distance;
+            desY = distance + RANDOM.nextInt(HEIGHT - 2 * distance);
+            graphics.drawString(Character.toString(verifyCode.charAt(i)), desX, desY);
         }
 
-        for(int x=0;x<150;x++){
+        for (int x = 0; x < 150; x++) {
             graphics.setColor(getRandomColor());
-            graphics.drawOval(RANDOM.nextInt(WIDTH),RANDOM.nextInt(HEIGHT),1,1);
+            graphics.drawOval(RANDOM.nextInt(WIDTH), RANDOM.nextInt(HEIGHT), 1, 1);
         }
 
         graphics.dispose();
@@ -70,6 +87,6 @@ public class GenerateImage {
         int r = RANDOM.nextInt(256);
         int g = RANDOM.nextInt(256);
         int b = RANDOM.nextInt(256);
-        return new Color(r,g,b);
+        return new Color(r, g, b);
     }
 }