⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 randomimagegenerator.java

📁 这是一个用java实现的简单验证码程序
💻 JAVA
字号:
package yy.common.random;

import org.apache.log4j.Logger;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;

import org.apache.commons.lang.RandomStringUtils;

public class RandomImageGenerator
{
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(RandomImageGenerator.class);
    
    public static String random(int num, boolean fromChar, boolean fromNumber)
    {
	logger.info("生成"+num+"位验证码,");
	return RandomStringUtils.random(num, fromChar, fromNumber);
    }

    public static void render(String num, OutputStream out, int len, boolean move, boolean draw, int fontSize)
	    throws IOException
    {
	int width = len * 10 + 10;
	int height = 20;
	if (fontSize >= height - 5)
	{
	    height += 5;
	}	    
	if (move)
	{
	    height = len * 10;
	}	    
	BufferedImage bi = new BufferedImage(width, height, 1);
	Graphics2D g = (Graphics2D) bi.getGraphics();
	Random random = new Random();
	g.setColor(Color.white);
	g.fillRect(0, 0, width, height);
	Font mFont = new Font("Tahoma", 1, fontSize);
	g.setFont(mFont);
	g.setColor(Color.BLACK);
	String[] str1 = new String[len];
	for (int i = 0; i < str1.length; i++)
	{
	    str1[i] = num.substring(i, i + 1);
	    int w = 14;
	    if (move)
	    {
		int x = (i + 1) % 3;
		if (x == random.nextInt(3))
		{
		    w = 19 - random.nextInt(7);
		}		    
		else
		{
		    w = 19 + random.nextInt(7);
		}		    
	    }
	    Color color1 = new Color(random.nextInt(180), random.nextInt(180), random.nextInt(180));
	    g.setColor(color1);
	    g.drawString(str1[i], 11 * i, w);
	}
	if (draw)
	{
	    for (int i = 0; i < 10; i++)
	    {
		int x = random.nextInt(width);
		int y = random.nextInt(height);
		Color color1 = new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));
		g.setColor(color1);
		g.drawOval(x, y, 0, 0);
	    }
	    for (int i = 0; i < 2; i++)
	    {
		int x = random.nextInt(width);
		int y = random.nextInt(height);
		int x1 = random.nextInt(width);
		int y1 = random.nextInt(height);
		Color color1 = new Color(random.nextInt(2), random.nextInt(5), random.nextInt(5));
		g.setColor(color1);
		g.drawLine(x, y, x1, y1);
	    }
	}
	g.dispose();
	ImageIO.write(bi, "jpg", out);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -