All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.nichetoolkit.rest.image.ImageUtils Maven / Gradle / Ivy

The newest version!
package io.github.nichetoolkit.rest.image;

import io.github.nichetoolkit.rest.constant.UtilConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

/**
 * ImageUtils
 * 

The image utils class.

* @author Cyan ([email protected]) * @see lombok.extern.slf4j.Slf4j * @see org.springframework.stereotype.Component * @since Jdk1.8 */ @Slf4j @Component public class ImageUtils { /** * randoms *

The randoms method.

* @return {@link io.github.nichetoolkit.rest.image.ImageVerify}

The randoms return object is ImageVerify type.

* @see io.github.nichetoolkit.rest.image.ImageVerify */ public static synchronized ImageVerify randoms() { /* 渲染图片 */ BufferedImage image = new BufferedImage(UtilConstants.DEFAULT_IMAGE_WIDTH, UtilConstants.DEFAULT_IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = (Graphics2D) image.getGraphics(); graphics.setColor(UtilConstants.DEFAULT_IMAGE_COLOR); graphics.fillRect(0,0,UtilConstants.DEFAULT_IMAGE_WIDTH, UtilConstants.DEFAULT_IMAGE_HEIGHT); /* 干扰线条 */ for (int i = 0; i < 150; ++i) { int startX = UtilConstants.SECURE_RANDOM.nextInt(UtilConstants.DEFAULT_IMAGE_WIDTH); int startY = UtilConstants.SECURE_RANDOM.nextInt(UtilConstants.DEFAULT_IMAGE_HEIGHT); int endX = UtilConstants.SECURE_RANDOM.nextInt(UtilConstants.DEFAULT_IMAGE_WIDTH); int endY = UtilConstants.SECURE_RANDOM.nextInt(UtilConstants.DEFAULT_IMAGE_HEIGHT); graphics.setColor(randomColor(170, 200)); graphics.drawLine(startX, startY, endX, endY); } /* 验证码 */ StringBuilder contentBuilder = new StringBuilder(); for (int i = 0; i < 4; ++i) { String character = String.valueOf(randomChar()); contentBuilder.append(character); AffineTransform affineTransform = new AffineTransform(); affineTransform.rotate(randomTransform() * 3.14 / 180, 15 * i + 3.5, 5); graphics.setTransform(affineTransform); graphics.setFont(randomFont()); graphics.setColor(randomColor()); graphics.drawString(character, 15 * i + 7, 16); } return new ImageVerify(contentBuilder.toString(),image); } /** * randomChar *

The random char method.

* @return {@link java.lang.Character}

The random char return object is Character type.

* @see java.lang.Character */ protected static Character randomChar() { int index = UtilConstants.SECURE_RANDOM.nextInt(UtilConstants.BASE_STRING.length()); return UtilConstants.BASE_STRING.charAt(index); } /** * randomColor *

The random color method.

* @return {@link java.awt.Color}

The random color return object is Color type.

* @see java.awt.Color */ protected static Color randomColor() { return randomColor(0,150); } /** * randomColor *

The random color method.

* @param min {@link java.lang.Integer}

The min parameter is Integer type.

* @param max {@link java.lang.Integer}

The max parameter is Integer type.

* @return {@link java.awt.Color}

The random color return object is Color type.

* @see java.lang.Integer * @see java.awt.Color */ protected static Color randomColor(Integer min, Integer max) { if (min > 255) { min = 255; } if (max > 255) { max = 255; } int red = UtilConstants.SECURE_RANDOM.nextInt(max - min) + min; int green = UtilConstants.SECURE_RANDOM.nextInt(max - min) + min; int blue = UtilConstants.SECURE_RANDOM.nextInt(max - min) + min; return new Color(red, green, blue); } /** * randomFont *

The random font method.

* @return {@link java.awt.Font}

The random font return object is Font type.

* @see java.awt.Font */ protected static Font randomFont() { int index = UtilConstants.SECURE_RANDOM.nextInt(UtilConstants.FONT_NAME_ARRAY.length); String fontName = UtilConstants.FONT_NAME_ARRAY[index]; int style = UtilConstants.SECURE_RANDOM.nextInt(4); int size = UtilConstants.SECURE_RANDOM.nextInt(5) + 20; //noinspection MagicConstant return new Font(fontName, style, size); } /** * randomTransform *

The random transform method.

* @return {@link java.lang.Integer}

The random transform return object is Integer type.

* @see java.lang.Integer */ protected static Integer randomTransform() { int index = UtilConstants.SECURE_RANDOM.nextInt(UtilConstants.AFFINE_TRANSFORM_ARRAY.length); return UtilConstants.AFFINE_TRANSFORM_ARRAY[index]; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy