首页>代码>jQuery网页验证码插件>/demo/src/com/demo_image/ImageUtil.java
package com.demo_image;

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

import javax.imageio.ImageIO;

/**
 * 生成随机验证码
 * @author Administrator
 */
public class ImageUtil {
	
	private static String[] strs = { "a", "b", "c", "d", "e", "f", "g", "h",
			"i", "j", "k", "m", "n", "p", "q", "r", "s", "t", "u",
			"v", "w", "x", "y", "z",  "2", "3", "4", "5", "6", "7", "8","9" };

	/**
	  * 方法的描述: 随机生成随字符串
	  * @param n 生成字符个数
	  * @return  String 生成的字符串
	 */
	private static String getRundomStr1(int n) {
		// 用Stringbuffer拼接字符串
		StringBuffer s = new StringBuffer();
		// 循环控制字符个数
		for (int i = 0; i < n; i++) {
			String temp = "";
			// 随机产生下标
			Random r = new Random();
			int a = r.nextInt(strs.length);// 0-34
			if (a < 24) {
				// 0-23是字母
				int b = r.nextInt(100);
				if (b % 2 == 0) {
					temp = strs[a].toUpperCase();
					s.append(temp);
				} else {
					s.append(strs[a]);
				}
			} else {
				s.append(strs[a]);
			}
		}
		return s.toString();
	}

	/**
	  * 方法的描述:默认返回4位的字符串
	  * @return  String
	  *
	 */
	public static String getRundomStr() {
		return getRundomStr1(4);
	}
	
	/**
	  * 方法的描述:生成验证码图片(默认的宽和高)
	  * @param str 验证码字符串
	  * @param out 输出流
	  * @throws IOException
	  * void
	 */
	public static void getImage(String str,
			OutputStream out) throws IOException {
		getImage1(115, 37, str,out);
	}
	
	/**
	  * 方法的描述:创建图片
	  * @param width 图片宽
	  * @param height 图片高
	  * @param str 图片上需要画的字符串
	  * @param out 输出流
	  * @throws IOException
	  * void
	 */
	public static void getImage1(int width, int height, String str,
			OutputStream out) throws IOException {
		BufferedImage imag = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D g = (Graphics2D) imag.getGraphics();
		// 1画背景图片
		g.setColor(getRundomRGB(120, 255));
		g.fillRect(0, 0, width, height);

		// 2随机产生多条线
		g.setColor(getRundomRGB(145, 175));
		Random rd = new Random();
		for (int i = 0; i < 100; i++) {
			if (i % 4 == 0)
				g.setColor(getRundomRGB(120, 220));
			int x = rd.nextInt(width);
			int y = rd.nextInt(height);
			int x1 = rd.nextInt(width / 3);
			int y1 = rd.nextInt(height / 3);
			g.drawLine(x, y, x + x1, y + y1);
		}

		// 3画字符串
		char[] c = str.toCharArray();
		g.setFont(new Font("Serif", Font.BOLD, 24));
		for (int i = 0; i < c.length; i++) {
			g.setColor(getRundomRGB(0, 90));
			if (rd.nextInt(1000) % 2 == 0) {
				//旋转
				g.rotate(.8, width / 10  + (width / 4) * i, height
						+ height / 2 + height/12);
				g.drawString(String.valueOf(c[i]), width / 4 - width / 5-width/7
						+ (width / 4) * i, height - height / 8);
				g.rotate(-.8, width / 10  + (width / 4) * i, height
						+ height / 2+height/12);
			} else {
				g.drawString(String.valueOf(c[i]), width / 4 - width / 6
						+ (width / 4) * i, height - height / 4);
			}
		}
		//====
		for (int i = 0; i < 20; i++) {
			if (i % 2 == 0)
				g.setColor(getRundomRGB(142, 220));
			int x = rd.nextInt(width);
			int y = rd.nextInt(height);
			int x1 = rd.nextInt(width / 3);
			int y1 = rd.nextInt(height / 3);
			g.drawLine(x, y, x + x1, y + y1);
		}
		
		g.dispose();
		ImageIO.write(imag, "png", out);
	}

	// 颜色随机产生器
	private static Color getRundomRGB(int pr, int pg) {
		if (pr > 255)
			pr = 255;
		if (pg > 255)
			pg = 255;
		Random rd = new Random();
		int r = pr + rd.nextInt(pg - pr);
		int g = pr + rd.nextInt(pg - pr);
		int b = pr + rd.nextInt(pg - pr);
		return new Color(r, g, b);
	}
	
	public static void main(String[] args) throws IOException {
		FileOutputStream out = new FileOutputStream("e:/demo/c.png");
		String str = ImageUtil.getRundomStr();
		System.out.println(str);
		ImageUtil.getImage(str, out);
	}

}
最近下载更多
听着雨吹着风  LV9 2022年5月8日
dushuzhong  LV7 2022年5月3日
JohnAlex  LV9 2022年1月29日
g1121345342  LV9 2021年6月22日
helloMD  LV8 2020年11月16日
s779766816  LV10 2020年9月22日
G1ccccc1123  LV1 2020年1月8日
styone  LV3 2019年11月5日
gardenia111  LV16 2019年6月25日
mxboo123  LV8 2019年6月10日
最近浏览更多
YiRenHun  LV11 2023年12月5日
18276177062  LV1 2023年6月12日
快乐的小丸子 2022年12月25日
暂无贡献等级
835512308  LV14 2022年7月22日
Taoaqi  LV3 2022年6月10日
芮坎尚  LV1 2022年5月11日
听着雨吹着风  LV9 2022年5月8日
dushuzhong  LV7 2022年5月3日
Jeasonchan898  LV2 2022年4月26日
liuyanlin_sc 2022年4月11日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友