Article Directory
Table of Contents
- Article Table of Contents
- Text
- 1. Directly upload the code
- 2. Things to note
- 3. Test
- 4. Get Base64 code
Text
1. Directly upload the code
package com.review.demo.util; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Base64; import java.util.Random; public class ImageCodeCreate { /** * long */ private static final int WIDTH = 120; /** * Width */ private static final int HEIGHT = 60; /** * Number of interference lines */ private static final int LINE_COUNT = 5; /** * Font collection */ private static final String[] FONT_NAMES = {"OCR A", "Cooper", "Unispace"}; /** * Get base64 code * * @param code code * @return base64 code */ public static String getBase64(String code) { try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) { BufferedImage bufferImage = getBufferImage(code); ImageIO.write(bufferImage, "PNG", byteArrayOutputStream); byte[] bytes = byteArrayOutputStream.toByteArray(); return "data:image/png;base64," + Base64.getEncoder().encodeToString(bytes); } catch (IOException e) { return null; } } /** * Create graphic verification password * * @param length verification code length * @return BufferedImage object */ public static BufferedImage getBufferImage(String code) { BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); //Set background color g.setColor(new Color(255, 255, 255)); g.fillRect(0, 0, WIDTH, HEIGHT); // //Set the rectangular border // g.setColor(new Color(0, 0, 0)); // g.drawRect(0, 0, WIDTH - 1, HEIGHT - 1); //Add verification code drawCode(g, code); //Add interference line drawLine(g); // Release resources g.dispose(); return image; } //Add verification code private static void drawCode(Graphics2D g, String code) { Random random = new Random(); for (int i = 0; i < code.length(); i + + ) { //Set random font int size = (WIDTH / code.length()) + random.nextInt(-4, 4); int style = random.nextInt(4); // 0: No style, 1: Bold, 2: Italic 4: BOLD_ITALIC Font font = new Font(FONT_NAMES[random.nextInt(FONT_NAMES.length)], style, size); g.setFont(font); //Set random character color Color color = new Color(random.nextInt(200), random.nextInt(200), random.nextInt(200)); g.setColor(color); //Set random rotation angle double radians = Math.toRadians(random.nextDouble(-10, 10)); g.rotate(radians); // draw characters int x = ((WIDTH / code.length()) * 4 / 5) * i + ((WIDTH / code.length() / 3)); int y = (HEIGHT / 2) + ((WIDTH / code.length()) / 2) + random.nextInt(-4, 4); g.drawString(String.valueOf(code.charAt(i)), x, y); //Reset rotation angle g.rotate(-radians); } } //Add interference line private static void drawLine(Graphics2D g) { Random random = new Random(); for (int i = 0; i < LINE_COUNT; i + + ) { // The color of the interference line is randomly generated Color color = new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)); g.setColor(color); int x1 = random.nextInt(WIDTH); int y1 = random.nextInt(HEIGHT); int x2 = random.nextInt(WIDTH); int y2 = random.nextInt(HEIGHT); g.drawLine(x1, y1, x2, y2); } } }
2. Things to note
1. In the drawCode() method of adding the verification code, the character color range is not set to 255, but to 200, in order to prevent the character color on the picture from being too light and unclear.
2. In the method of adding the verification code drawCode(), the rotation angle g.rotate(radians) is set. After drawing to the picture, it needs to be reset, otherwise the next character will be at the rotation angle of the previous character. based on rotation again
3. Test
class MyTests { public static void main(String[] args) { BufferedImage bufferedImage = ImageCodeCreate.getBufferImage("sret7"); String filePath = System.getProperty("user.dir") + File.separator + "myTest"; String fileName = "test.png"; File path = new File(filePath); if (!path.exists()) { path.mkdirs(); } File file = new File(filePath + File.separator + fileName); try { ImageIO.write(bufferedImage, "PNG", file); } catch (IOException e) { throw new RuntimeException(e); } } }
In the myTest folder under the project directory, you can see that the verification code was successfully generated:
4. Obtain Base64 code
String base64Encode; ByteArrayOutputStream outputStream = null; outputStream = new ByteArrayOutputStream(); ImageIO.write(bufferImage, "PNG", outputStream); byte[] bytes = outputStream.toByteArray(); base64Encode = "data:image/png;base64," + Base64.getEncoder().encodeToString(bytes);
Remember to close the stream and handle exceptions, so I won’t write them here.