/*
* RandomCodeImage.java
* Created on Jun 3, 2008 5:57:28 PM
* Copyright (c) IBEX All Rights Reserved.
*
* This software is the confidential and proprietary information of IBEX.
* You shall not disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with IBEX.
*/
package net.mmgg.commons;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Paint;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
public class RandomCodeImage {
public static ArrayList fonts = null;
// 单例模式
private static RandomCodeImage instance = null;
// 定义验证码内容
private String codes = null;
//乘法口诀常量
private final int MULTIPLY=1;//乘
private final int DIVIDED=2;//除
private final int PLUS=3;//加
private final int SUBTRACT=4;//减
/**
* 单例模式
*
* @date Jun 13, 2008 9:27:42 AM
* @author Fly(Fly@mmgg.net)
* @return
*/
public static RandomCodeImage getInstance() {
if (null == instance) {
instance = new RandomCodeImage();
}
return instance;
}
/**
* 公共构造函数,加载用户自定义字体
* @date Jun 13, 2008 4:31:22 PM
* @author Fly(Fly@mmgg.net)
* @param fontName 字体名集合
*/
public RandomCodeImage(String[] fontName){
if(null!=fonts){
return;
}
Font font=null;
fonts=new ArrayList();
for (int i = 0; i < fontName.length; i++) {
font=new Font(fontName[i],Font.PLAIN,14);
fonts.add(font);
}
}
/**
* 公共构造函数,根据字体文件所在的文件夹,加载用户自定义字体,只支持TTF字体
* @date Jun 13, 2008 5:17:08 PM
* @author Fly(Fly@mmgg.net)
* @param fontsPath
*/
public RandomCodeImage(String fontsPath){
if(null!=fonts){
return;
}
List fontlist=this.readfile(fontsPath, "TTF");
fonts = new ArrayList();
for (Iterator iterator = fontlist.iterator(); iterator.hasNext();) {
String object = (String) iterator.next();
File file_ = new File(object);
try {
Font font_ = Font.createFont(Font.TRUETYPE_FONT, file_);
fonts.add(font_);
} catch (FontFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 私有构造函数,加载系统默认字体
* @date Jun 13, 2008 9:27:08 AM
* @author Fly(Fly@mmgg.net)
*/
private RandomCodeImage() {
if(null!=fonts){
return;
}
fonts = new ArrayList();
GraphicsEnvironment.getLocalGraphicsEnvironment().preferLocaleFonts();
String[] names = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getAvailableFontFamilyNames(Locale.getDefault());
for (int i = 0; i < names.length; i++) {
char c = names[i].charAt(0);
if (Character.isLowerCase(c) || Character.isUpperCase(c)) {
} else {
fonts.add(new Font(names[i],Font.PLAIN,14));
}
}
}
/**
* 生成数字和字母验证码
*
* @date Jun 13, 2008 9:25:49 AM
* @author Fly(Fly@mmgg.net)
* @return
* @throws Exception
*/
private BufferedImage getImageOfABC123(){
BufferedImage bi = new BufferedImage(150, 50,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
String code = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
char[] cs = code.toCharArray();
char[] use = new char[5];
g.setColor(this.getBgColor());
g.fillRect(0, 0, 150, 50);
codes="";
java.awt.geom.AffineTransform af=new AffineTransform();
af.setToRotation(Math.PI/2.0);
for (int i = 0; i < 5; i++) {
//抗锯齿,图片不失真
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
Point p = getPoint(i);
int size = getSize();
use[i] = cs[(int) (Math.random() * cs.length)];
g.setPaint(getPaint(p, size));
g.setFont(((Font) fonts.get((int) (Math.random() * fonts.size()))).deriveFont(this.getFace(), this.getSize()));
g.drawString("" + use[i], p.x, p.y);
codes += use[i];
}
g.setStroke(new BasicStroke(1.5f));
g.setPaint(null);
//画干扰线
this.drawLine(g, 2,200,50);
//画杂点
this.drawPoint(g, 50,150,50);
// 画个边框
//g.setColor(new Color((int) (Math.random() * 0x00FFFFFFF)));
//g.drawRect(1, 1, 150 - 2, 50 - 2);
g.dispose();
bi.flush();
return bi;
}
/**
* 设置背景颜色
*
* @date Jun 13, 2008 9:26:15 AM
* @author Fly(Fly@mmgg.net)
* @return
*/
private Color getBgColor() {
Random r = new Random();
int x = r.nextInt(256);
x = x > 105 ? x : (x + 150);
return new Color(x, x, x);
}
private Stroke getStroke() {
BasicStroke bs = new BasicStroke((float) (Math.random() * 3));
return bs;
}
/**
* 文字距离
*
* @date Jun 13, 2008 9:26:36 AM
* @author Fly(Fly@mmgg.net)
* @param index
* @return
*/
private Point getPoint(int index) {
return new Point(5 + (index * ((int) (Math.random() * 6) + 24)), 40);
}
private Paint getPaint(Point p, int size) {
GradientPaint gp = new GradientPaint(p.x, p.y, new Color((int) (Math
.random() * 200), 0, (int) (Math.random() * 200)), p.x, p.y
- size, new Color((int) (Math.random() * 200), (int) (Math
.random() * 200), (int) (Math.random() * 200)));
return gp;
}
/**
* 字体样式
*
* @date Jun 5, 2008 1:44:15 PM
* @author Fly(Fly@mmgg.net)
* @return
*/
private int getFace() {
if (Math.random() * 10 > 5) {
return Font.BOLD;
} else {
return Font.ITALIC;
}
}
/**
* 字体大小
*
* @date Jun 5, 2008 1:44:00 PM
* @author Fly(Fly@mmgg.net)
* @return
*/
private int getSize() {
int[] sizes = new int[20];
for (int i = 0; i < 20; i++) {
sizes[i] = 30 + i;
}
return sizes[(int) (Math.random() * sizes.length)];
}
/**
* 获取验证码图像,并把图像用流输出
* 生成字母和数字的验证码
* @date Jun 13, 2008 3:19:12 PM
* @author Fly(Fly@mmgg.net)
* @param imageType 验证码输出的类型,可以是PNG或者JPGE
* @param output 输出流
* @return 验证码字符串
* @throws IOException
*/
public String drawImageOfABC123( OutputStream output) throws IOException {
try {
this.drawImage(this.getImageOfABC123(), output);
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally{
output.flush();
output.close();
}
return codes;
}
public String drawImageOfABC123( ByteArrayOutputStream output) throws IOException {
try {
//ImageIO.write(this.getImageOfABC123(), imageType.toUpperCase(), output);
this.drawImage(this.getImageOfABC123(), output);
} catch (IOException e) {
e.printStackTrace();
throw e;
}finally{
output.flush();
output.close();
}
return codes;
}
/**
* 获取验证码图像,并把图像用流输出
* 生成加减乘除的验证码
* @date Jun 13, 2008 3:20:40 PM
* @author Fly(Fly@mmgg.net)
* @param imageType 验证码输出的类型,可以是PNG或者JPGE
* @param output 输出流
* @return 验证码字符串
* @throws IOException
*/
public String drawImageOfMultiplicationFormula(OutputStream output) throws IOException{
try {
this.drawImage(this.getImageOfMultiplicationFormula(), output);
//ImageIO.write(this.getImageOfMultiplicationFormula(), imageType.toUpperCase(), output);
} catch (IOException e) {
e.printStackTrace();
throw e;
}finally{
output.flush();
output.close();
}
return codes;
}
public String drawImageOfMultiplicationFormula(ByteArrayOutputStream output) throws IOException{
try {
this.drawImage(this.getImageOfMultiplicationFormula(), output);
//ImageIO.write(this.getImageOfMultiplicationFormula(), imageType.toUpperCase(), output);
} catch (IOException e) {
e.printStackTrace();
throw e;
}
finally{
output.flush();
output.close();
}
return codes;
}
/**
* 获取验证码图像,并把图像用流输出
* 生成中文汉字的验证码
* @date Jun 13, 2008 3:13:15 PM
* @author Fly(Fly@mmgg.net)
* @param imageType 验证码输出的类型,可以是PNG或者JPGE
* @param output 输出流
* @return 验证码字符串
* @throws IOException
*/
public String drawImageOfChineseChar(OutputStream output) throws IOException{
try {
//ImageIO.write(this.getImageOfChineseChar(), imageType.toUpperCase(), output);
this.drawImage(this.getImageOfChineseChar(), output);
} catch (IOException e) {
e.printStackTrace();
throw e;
}finally{
output.flush();
output.close();
}
return codes;
}
public String drawImageOfChineseChar(ByteArrayOutputStream output) throws IOException{
try {
this.drawImage(this.getImageOfChineseChar(), output);
//ImageIO.write(this.getImageOfChineseChar(), imageType.toUpperCase(), output);
} catch (IOException e) {
e.printStackTrace();
throw e;
}finally{
output.flush();
output.close();
}
return codes;
}
/**
* 产生清晰的图片
* @Author:吴云辉
* @date:Oct 22, 2009 9:47:42 AM
* @param image
* @param output
* @throws IOException
*/
private void drawImage(BufferedImage image,Object output) throws IOException{
//BufferedImage image =this.getImageOfABC123();
//ImageIO.write(this.getImageOfABC123(), imageType.toUpperCase(), output);
ImageWriter writer = null;
ImageTypeSpecifier type = ImageTypeSpecifier.createFromRenderedImage(image);
Iterator<ImageWriter> iter = ImageIO.getImageWriters(type, "JPG");
if (iter.hasNext()) {
writer = (ImageWriter) iter.next();
}
IIOImage iioImage = new IIOImage(image, null, null);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality((float) (1));
ImageOutputStream outputStream = ImageIO.createImageOutputStream(output);
writer.setOutput(outputStream);
writer.write(null, iioImage, param);
}
/**
* 产生从X到Y的随机数
*
* @date Jun 13, 2008 9:59:47 AM
* @author Fly(Fly@mmgg.net)
* @param x
* @param y
* @return
*/
private int getRandom(int x, int y) {
return new Random().nextInt(y - x + 1) + x;
}
/**
* 生成乘法口诀类似的加、减、乘、除运算公式
*
* @date Jun 13, 2008 10:03:37 AM
* @author Fly(Fly@mmgg.net)
* @return
*/
private BufferedImage getImageOfMultiplicationFormula(){
String cds=this.getMultiplication(this.getRandom(1, 4));
int width_;
final int height_=50;
//根据文字的长度设置图片的宽度
switch (cds.length()) {
case 7:width_=160;break;
case 8:width_=180;break;
case 6:width_=140;break;
case 5:width_=120;break;
default:width_=150;break;
}
BufferedImage bi = new BufferedImage(width_, height_,BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
//抗锯齿,图片不失真
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(this.getBgColor());
g.fillRect(0, 0, width_, height_);
for (int i=0;i<cds.length();i++){
g.setColor(new Color((int) (Math.random() * 100), (int) (Math.random() * 100), (int) (Math.random() * 100)));
g.setFont(((Font) fonts.get((int) (Math.random() * fonts.size()))).deriveFont(this.getFace(), 25));
g.drawString(String.valueOf(cds.charAt(i)),22*i+5,35);
}
//画干扰线
this.drawLine(g, 3,200,50);
//画杂点
this.drawPoint(g, 30,width_,height_);
// 画个边框
//g.setColor(new Color((int) (Math.random() * 0x00FFFFFFF)));
//g.drawRect(1, 1, width_ - 2, height_ - 2);
g.dispose();
bi.flush();
return bi;
}
private String getMultiplication(int type) {
int a,b,v;
String ret=null;
if(type>this.DIVIDED){
a=this.getRandom(0, 99);
b=this.getRandom(0, 99);
}else{
a=this.getRandom(0, 9);
b=this.getRandom(0, 9);
}
//乘以
if(this.MULTIPLY==type){
v=a*b;
this.codes=String .valueOf(v);
ret=this.getArithmetic(a, b, type);
return ret;
}
//除以
if(this.DIVIDED==type){
//保证除数不为0
b=this.getRandom(1, 9);
v=a*b;
this.codes=String.valueOf(a);
ret=this.getArithmetic(v, b, type);
return ret;
}
//加上
if(this.PLUS==type){
v=a+b;
this.codes=String.valueOf(v);
ret=this.getArithmetic(a, b, type);
return ret;
}
//减去
if(this.SUBTRACT==type){
v=a+b;
this.codes=String.valueOf(a);
ret=this.getArithmetic(v, b, type);
return ret;
}
return ret;
}
private String getArithmetic(int a,int b,int type){
String str="";
switch (type) {
case MULTIPLY:
str=a+"×"+b+"=?";
break;
case DIVIDED:
str=a+"÷"+b+"=?";
break;
case PLUS:
str=a+"+"+b+"=?";
break;
case SUBTRACT:
str=a+"-"+b+"=?";
break;
default:
str=a+"×"+b+"=?";
break;
}
return str;
}
/**
* 得到随机常用汉字
* 从2500个常用汉字中选
* @date Jun 13, 2008 2:18:53 PM
* @author Fly(Fly@mmgg.net)
* @return
*/
private char getRandomChineseChar()
{
String str="\u4e00\u4e59\u4e8c\u5341\u4e01\u5382\u4e03\u535c\u4eba\u5165\u516b\u4e5d\u51e0\u513f\u4e86\u529b\u4e43\u5200\u53c8\u4e09\u4e8e\u5e72\u4e8f\u58eb\u5de5\u571f\u624d\u5bf8\u4e0b\u5927\u4e08\u4e0e\u4e07\u4e0a\u5c0f\u53e3\u5dfe\u5c71\u5343\u4e5e\u5ddd\u4ebf\u4e2a\u52fa\u4e45\u51e1\u53ca\u5915\u4e38\u4e48\u5e7f\u4ea1\u95e8\u4e49\u4e4b\u5c38\u5f13\u5df1\u5df2\u5b50\u536b\u4e5f\u5973\u98de\u5203\u4e60\u53c9\u9a6c\u4e61\u4e30\u738b\u4e95\u5f00\u592b\u5929\u65e0\u5143\u4e13\u4e91\u624e\u827a\u6728\u4e94\u652f\u5385\u4e0d\u592a\u72ac\u533a\u5386\u5c24\u53cb\u5339\u8f66\u5de8\u7259\u5c6f\u6bd4\u4e92\u5207\u74e6\u6b62\u5c11\u65e5\u4e2d\u5188\u8d1d\u5185\u6c34\u89c1\u5348\u725b\u624b\u6bdb\u6c14\u5347\u957f\u4ec1\u4ec0\u7247\u4ec6\u5316\u4ec7\u5e01\u4ecd\u4ec5\u65a4\u722a\u53cd\u4ecb\u7236\u4ece\u4eca\u51f6\u5206\u4e4f\u516c\u4ed3\u6708\u6c0f\u52ff\u6b20\u98ce\u4e39\u5300\u4e4c\u51e4\u52fe\u6587\u516d\u65b9\u706b\u4e3a\u6597\u5fc6\u8ba2\u8ba1\u6237\u8ba4\u5fc3\u5c3a\u5f15\u4e11\u5df4\u5b54\u961f\u529e\u4ee5\u5141\u4e88\u529d\u53cc\u4e66\u5e7b\u7389\u520a\u793a\u672b\u672a\u51fb\u6253\u5de7\u6b63\u6251\u6252\u529f\u6254\u53bb\u7518\u4e16\u53e4\u8282\u672c\u672f\u53ef\u4e19\u5de6\u5389\u53f3\u77f3\u5e03\u9f99\u5e73\u706d\u8f67\u4e1c\u5361\u5317\u5360\u4e1a\u65e7\u5e05\u5f52\u4e14\u65e6\u76ee\u53f6\u7532\u7533\u53ee\u7535\u53f7\u7530\u7531\u53f2\u53ea\u592e\u5144\u53fc\u53eb\u53e6\u53e8\u53f9\u56db\u751f\u5931\u79be\u4e18\u4ed8\u4ed7\u4ee3\u4ed9\u4eec\u4eea\u767d\u4ed4\u4ed6\u65a5\u74dc\u4e4e\u4e1b\u4ee4\u7528\u7529\u5370\u4e50\u53e5\u5306\u518c\u72af\u5916\u5904\u51ac\u9e1f\u52a1\u5305\u9965\u4e3b\u5e02\u7acb\u95ea\u5170\u534a\u6c41\u6c47\u5934\u6c49\u5b81\u7a74\u5b83\u8ba8\u5199\u8ba9\u793c\u8bad\u5fc5\u8bae\u8baf\u8bb0\u6c38\u53f8\u5c3c\u6c11\u51fa\u8fbd\u5976\u5974\u52a0\u53ec\u76ae\u8fb9\u53d1\u5b55\u5723\u5bf9\u53f0\u77db\u7ea0\u6bcd\u5e7c\u4e1d\u5f0f\u5211\u52a8\u625b\u5bfa\u5409\u6263\u8003\u6258\u8001\u6267\u5de9\u573e\u6269\u626b\u5730\u626c\u573a\u8033\u5171\u8292\u4e9a\u829d\u673d\u6734\u673a\u6743\u8fc7\u81e3\u518d\u534f\u897f\u538b\u538c\u5728\u6709\u767e\u5b58\u800c\u9875\u5320\u5938\u593a\u7070\u8fbe\u5217\u6b7b\u6210\u5939\u8f68\u90aa\u5212\u8fc8\u6bd5\u81f3\u6b64\u8d1e\u5e08\u5c18\u5c16\u52a3\u5149\u5f53\u65e9\u5410\u5413\u866b\u66f2\u56e2\u540c\u540a\u5403\u56e0\u5438\u5417\u5c7f\u5e06\u5c81\u56de\u5c82\u521a\u5219\u8089\u7f51\u5e74\u6731\u5148\u4e22\u820c\u7af9\u8fc1\u4e54\u4f1f\u4f20\u4e52\u4e53\u4f11\u4f0d\u4f0f\u4f18\u4f10\u5ef6\u4ef6\u4efb\u4f24\u4ef7\u4efd\u534e\u4ef0\u4eff\u4f19\u4f2a\u81ea\u8840\u5411\u4f3c\u540e\u884c\u821f\u5168\u4f1a\u6740\u5408\u5146\u4f01\u4f17\u7237\u4f1e\u521b\u808c\u6735\u6742\u5371\u65ec\u65e8\u8d1f\u5404\u540d\u591a\u4e89\u8272\u58ee\u51b2\u51b0\u5e84\u5e86\u4ea6\u5218\u9f50\u4ea4\u6b21\u8863\u4ea7\u51b3\u5145\u5984\u95ed\u95ee\u95ef\u7f8a\u5e76\u5173\u7c73\u706f\u5dde\u6c57\u6c61\u6c5f\u6c60\u6c64\u5fd9\u5174\u5b87\u5b88\u5b85\u5b57\u5b89\u8bb2\u519b\u8bb8\u8bba\u519c\u8bbd\u8bbe\u8bbf\u5bfb\u90a3\u8fc5\u5c3d\u5bfc\u5f02\u5b59\u9635\u9633\u6536\u9636\u9634\u9632\u5978\u5982\u5987\u597d\u5979\u5988\u620f\u7fbd\u89c2\u6b22\u4e70\u7ea2\u7ea4\u7ea7\u7ea6\u7eaa\u9a70\u5de1\u5bff\u5f04\u9ea6\u5f62\u8fdb\u6212\u541e\u8fdc\u8fdd\u8fd0\u6276\u629a\u575b\u6280\u574f\u6270\u62d2\u627e\u6279\u626f\u5740\u8d70\u6284\u575d\u8d21\u653b\u8d64\u6298\u6293\u626e\u62a2\u5b5d\u5747\u629b\u6295\u575f\u6297\u5751\u574a\u6296\u62a4\u58f3\u5fd7\u626d\u5757\u58f0\u628a\u62a5\u5374\u52ab\u82bd\u82b1\u82b9\u82ac\u82cd\u82b3\u4e25\u82a6\u52b3\u514b\u82cf\u6746\u6760\u675c\u6750\u6751\u674f\u6781\u674e\u6768\u6c42\u66f4\u675f\u8c46\u4e24\u4e3d\u533b\u8fb0\u52b1\u5426\u8fd8\u6b7c\u6765\u8fde\u6b65\u575a\u65f1\u76ef\u5448\u65f6\u5434\u52a9\u53bf\u91cc\u5446\u56ed\u65f7\u56f4\u5440\u5428\u8db3\u90ae\u7537\u56f0\u5435\u4e32\u5458\u542c\u5429\u5439\u545c\u5427\u543c\u522b\u5c97\u5e10\u8d22\u9488\u9489\u544a\u6211\u4e71\u5229\u79c3\u79c0\u79c1\u6bcf\u5175\u4f30\u4f53\u4f55\u4f46\u4f38\u4f5c\u4f2f\u4f36\u4f63\u4f4e\u4f60\u4f4f\u4f4d\u4f34\u8eab\u7682\u4f5b\u8fd1\u5f7b\u5f79\u8fd4\u4f59\u5e0c\u5750\u8c37\u59a5\u542b\u90bb\u5c94\u809d\u809a\u80a0\u9f9f\u514d\u72c2\u72b9\u89d2\u5220\u6761\u5375\u5c9b\u8fce\u996d\u996e\u7cfb\u8a00\u51bb\u72b6\u4ea9\u51b5\u5e8a\u5e93\u7597\u5e94\u51b7\u8fd9\u5e8f\u8f9b\u5f03\u51b6\u5fd8\u95f2\u95f4\u95f7\u5224\u7076\u707f\u5f1f\u6c6a\u6c99\u6c7d\u6c83\u6cdb\u6c9f\u6ca1\u6c88\u6c89\u6000\u5fe7\u5feb\u5b8c\u5b8b\u5b8f\u7262\u7a76\u7a77\u707e\u826f\u8bc1\u542f\u8bc4\u8865\u521d\u793e\u8bc6\u8bc9\u8bca\u8bcd\u8bd1\u541b\u7075\u5373\u5c42\u5c3f\u5c3e\u8fdf\u5c40\u6539\u5f20\u5fcc\u9645\u9646\u963f\u9648\u963b\u9644\u5999\u5996\u59a8\u52aa\u5fcd\u52b2\u9e21\u9a71\u7eaf\u7eb1\u7eb3\u7eb2\u9a73\u7eb5\u7eb7\u7eb8\u7eb9\u7eba\u9a74\u7ebd\u5949\u73a9\u73af\u6b66\u9752\u8d23\u73b0\u8868\u89c4\u62b9\u62e2\u62d4\u62e3\u62c5\u5766\u62bc\u62bd\u62d0\u62d6\u62cd\u8005\u9876\u62c6\u62e5\u62b5\u62d8\u52bf\u62b1\u5783\u62c9\u62e6\u62cc\u5e78\u62db\u5761\u62ab\u62e8\u62e9\u62ac\u5176\u53d6\u82e6\u82e5\u8302\u82f9\u82d7\u82f1\u8303\u76f4\u8304\u830e\u8305\u6797\u679d\u676f\u67dc\u6790\u677f\u677e\u67aa\u6784\u6770\u8ff0\u6795\u4e27\u6216\u753b\u5367\u4e8b\u523a\u67a3\u96e8\u5356\u77ff\u7801\u5395\u5954\u5947\u594b\u6001\u6b27\u5784\u59bb\u8f70\u9877\u8f6c\u65a9\u8f6e\u8f6f\u5230\u975e\u53d4\u80af\u9f7f\u4e9b\u864e\u864f\u80be\u8d24\u5c1a\u65fa\u5177\u679c\u5473\u6606\u56fd\u660c\u7545\u660e\u6613\u6602\u5178\u56fa\u5fe0\u5490\u547c\u9e23\u548f\u5462\u5cb8\u5ca9\u5e16\u7f57\u5e1c\u5cad\u51ef\u8d25\u8d29\u8d2d\u56fe\u9493\u5236\u77e5\u5782\u7267\u7269\u4e56\u522e\u79c6\u548c\u5b63\u59d4\u4f73\u4f8d\u4f9b\u4f7f\u4f8b\u7248\u4f84\u4fa6\u4fa7\u51ed\u4fa8\u4f69\u8d27\u4f9d\u7684\u8feb\u8d28\u6b23\u5f81\u5f80\u722c\u5f7c\u5f84\u6240\u820d\u91d1\u547d\u65a7\u7238\u91c7\u53d7\u4e73\u8d2a\u5ff5\u8d2b\u80a4\u80ba\u80a2\u80bf\u80c0\u670b\u80a1\u80a5\u670d\u80c1\u5468\u660f\u9c7c\u5154\u72d0\u5ffd\u72d7\u5907\u9970\u9971\u9972\u53d8\u4eac\u4eab\u5e97\u591c\u5e99\u5e9c\u5e95\u5242\u90ca\u5e9f\u51c0\u76f2\u653e\u523b\u80b2\u95f8\u95f9\u90d1\u5238\u5377\u5355\u7092\u708a\u7095\u708e\u7089\u6cab\u6d45\u6cd5\u6cc4\u6cb3\u6cbe\u6cea\u6cb9\u6cca\u6cbf\u6ce1\u6ce8\u6cfb\u6cf3\u6ce5\u6cb8\u6ce2\u6cfc\u6cfd\u6cbb\u6016\u6027\u6015\u601c\u602a\u5b66\u5b9d\u5b97\u5b9a\u5b9c\u5ba1\u5b99\u5b98\u7a7a\u5e18\u5b9e\u8bd5\u90ce\u8bd7\u80a9\u623f\u8bda\u886c\u886b\u89c6\u8bdd\u8bde\u8be2\u8be5\u8be6\u5efa\u8083\u5f55\u96b6\u5c45\u5c4a\u5237\u5c48\u5f26\u627f\u5b5f\u5b64\u9655\u964d\u9650\u59b9\u59d1\u59d0\u59d3\u59cb\u9a7e\u53c2\u8270\u7ebf\u7ec3\u7ec4\u7ec6\u9a76\u7ec7\u7ec8\u9a7b\u9a7c\u7ecd\u7ecf\u8d2f\u594f\u6625\u5e2e\u73cd\u73bb\u6bd2\u578b\u6302\u5c01\u6301\u9879\u57ae\u630e\u57ce\u6320\u653f\u8d74\u8d75\u6321\u633a\u62ec\u62f4\u62fe\u6311\u6307\u57ab\u6323\u6324\u62fc\u6316\u6309\u6325\u632a\u67d0\u751a\u9769\u8350\u5df7\u5e26\u8349\u8327\u8336\u8352\u832b\u8361\u8363\u6545\u80e1\u5357\u836f\u6807\u67af\u67c4\u680b\u76f8\u67e5\u67cf\u67f3\u67f1\u67ff\u680f\u6811\u8981\u54b8\u5a01\u6b6a\u7814\u7816\u5398\u539a\u780c\u780d\u9762\u8010\u800d\u7275\u6b8b\u6b83\u8f7b\u9e26\u7686\u80cc\u6218\u70b9\u4e34\u89c8\u7ad6\u7701\u524a\u5c1d\u662f\u76fc\u7728\u54c4\u663e\u54d1\u5192\u6620\u661f\u6628\u754f\u8db4\u80c3\u8d35\u754c\u8679\u867e\u8681\u601d\u8682\u867d\u54c1\u54bd\u9a82\u54d7\u54b1\u54cd\u54c8\u54ac\u54b3\u54ea\u70ad\u5ce1\u7f5a\u8d31\u8d34\u9aa8\u949e\u949f\u94a2\u94a5\u94a9\u5378\u7f38\u62dc\u770b\u77e9\u600e\u7272\u9009\u9002\u79d2\u9999\u79cd\u79cb\u79d1\u91cd\u590d\u7aff\u6bb5\u4fbf\u4fe9\u8d37\u987a\u4fee\u4fdd\u4fc3\u4fae\u4fed\u4fd7\u4fd8\u4fe1\u7687\u6cc9\u9b3c\u4fb5\u8ffd\u4fca\u76fe\u5f85\u5f8b\u5f88\u987b\u53d9\u5251\u9003\u98df\u76c6\u80c6\u80dc\u80de\u80d6\u8109\u52c9\u72ed\u72ee\u72ec\u72e1\u72f1\u72e0\u8d38\u6028\u6025\u9976\u8680\u997a\u997c\u5f2f\u5c06\u5956\u54c0\u4ead\u4eae\u5ea6\u8ff9\u5ead\u75ae\u75af\u75ab\u75a4\u59ff\u4eb2\u97f3\u5e1d\u65bd\u95fb\u9600\u9601\u5dee\u517b\u7f8e\u59dc\u53db\u9001\u7c7b\u8ff7\u524d\u9996\u9006\u603b\u70bc\u70b8\u70ae\u70c2\u5243\u6d01\u6d2a\u6d12\u6d47\u6d4a\u6d1e\u6d4b\u6d17\u6d3b\u6d3e\u6d3d\u67d3\u6d4e\u6d0b\u6d32\u6d51\u6d53\u6d25\u6052\u6062\u6070\u607c\u6068\u4e3e\u89c9\u5ba3\u5ba4\u5bab\u5baa\u7a81\u7a7f\u7a83\u5ba2\u51a0\u8bed\u6241\u8884\u7956\u795e\u795d\u8bef\u8bf1\u8bf4\u8bf5\u57a6\u9000\u65e2\u5c4b\u663c\u8d39\u9661\u7709\u5b69\u9664\u9669\u9662\u5a03\u59e5\u59e8\u59fb\u5a07\u6012\u67b6\u8d3a\u76c8\u52c7\u6020\u67d4\u5792\u7ed1\u7ed2\u7ed3\u7ed5\u9a84\u7ed8\u7ed9\u7edc\u9a86\u7edd\u7ede\u7edf\u8015\u8017\u8273\u6cf0\u73e0\u73ed\u7d20\u8695\u987d\u76cf\u532a\u635e\u683d\u6355\u632f\u8f7d\u8d76\u8d77\u76d0\u634e\u634f\u57cb\u6349\u6346\u6350\u635f\u90fd\u54f2\u901d\u6361\u6362\u633d\u70ed\u6050\u58f6\u6328\u803b\u803d\u606d\u83b2\u83ab\u8377\u83b7\u664b\u6076\u771f\u6846\u6842\u6863\u6850\u682a\u6865\u6843\u683c\u6821\u6838\u6837\u6839\u7d22\u54e5\u901f\u9017\u6817\u914d\u7fc5\u8fb1\u5507\u590f\u7840\u7834\u539f\u5957\u9010\u70c8\u6b8a\u987e\u8f7f\u8f83\u987f\u6bd9\u81f4\u67f4\u684c\u8651\u76d1\u7d27\u515a\u6652\u7720\u6653\u9e2d\u6643\u664c\u6655\u868a\u54e8\u54ed\u6069\u5524\u554a\u5509\u7f62\u5cf0\u5706\u8d3c\u8d3f\u94b1\u94b3\u94bb\u94c1\u94c3\u94c5\u7f3a\u6c27\u7279\u727a\u9020\u4e58\u654c\u79e4\u79df\u79ef\u79e7\u79e9\u79f0\u79d8\u900f\u7b14\u7b11\u7b0b\u503a\u501f\u503c\u501a\u503e\u5012\u5018\u4ff1\u5021\u5019\u4fef\u500d\u5026\u5065\u81ed\u5c04\u8eac\u606f\u5f92\u5f90\u8230\u8231\u822c\u822a\u9014\u62ff\u7239\u7231\u9882\u7fc1\u8106\u8102\u80f8\u80f3\u810f\u80f6\u8111\u72f8\u72fc\u9022\u7559\u76b1\u997f\u604b\u6868\u6d46\u8870\u9ad8\u5e2d\u51c6\u5ea7\u810a\u75c7\u75c5\u75be\u75bc\u75b2\u6548\u79bb\u5510\u8d44\u51c9\u7ad9\u5256\u7ade\u90e8\u65c1\u65c5\u755c\u9605\u7f9e\u74f6\u62f3\u7c89\u6599\u76ca\u517c\u70e4\u70d8\u70e6\u70e7\u70db\u70df\u9012\u6d9b\u6d59\u6d9d\u9152\u6d89\u6d88\u6d69\u6d77\u6d82\u6d74\u6d6e\u6d41\u6da6\u6d6a\u6d78\u6da8\u70eb\u6d8c\u609f\u6084\u6094\u60a6\u5bb3\u5bbd\u5bb6\u5bb5\u5bb4\u5bbe\u7a84\u5bb9\u5bb0\u6848\u8bf7\u6717\u8bf8\u8bfb\u6247\u889c\u8896\u888d\u88ab\u7965\u8bfe\u8c01\u8c03\u51a4\u8c05\u8c08\u8c0a\u5265\u6073\u5c55\u5267\u5c51\u5f31\u9675\u9676\u9677\u966a\u5a31\u5a18\u901a\u80fd\u96be\u9884\u6851\u7ee2\u7ee3\u9a8c\u7ee7\u7403\u7406\u6367\u5835\u63cf\u57df\u63a9\u6377\u6392\u6389\u5806\u63a8\u6380\u6388\u6559\u638f\u63a0\u57f9\u63a5\u63a7\u63a2\u636e\u6398\u804c\u57fa\u8457\u52d2\u9ec4\u840c\u841d\u83cc\u83dc\u8404\u83ca\u840d\u83e0\u8425\u68b0\u68a6\u68a2\u6885\u68c0\u68b3\u68af\u6876\u6551\u526f\u7968\u621a\u723d\u804b\u88ad\u76db\u96ea\u8f85\u8f86\u865a\u96c0\u5802\u5e38\u5319\u6668\u7741\u772f\u773c\u60ac\u91ce\u5566\u665a\u5544\u8ddd\u8dc3\u7565\u86c7\u7d2f\u5531\u60a3\u552f\u5d16\u5d2d\u5d07\u5708\u94dc\u94f2\u94f6\u751c\u68a8\u7281\u79fb\u7b28\u7b3c\u7b1b\u7b26\u7b2c\u654f\u505a\u888b\u60a0\u507f\u5076\u5077\u60a8\u552e\u505c\u504f\u5047\u5f97\u8854\u76d8\u8239\u659c\u76d2\u9e3d\u6089\u6b32\u5f69\u9886\u811a\u8116\u8138\u8131\u8c61\u591f\u731c\u732a\u730e\u732b\u731b\u9985\u9986\u51d1\u51cf\u6beb\u9ebb\u75d2\u75d5\u5eca\u5eb7\u5eb8\u9e7f\u76d7\u7ae0\u7adf\u5546\u65cf\u65cb\u671b\u7387\u7740\u76d6\u7c98\u7c97\u7c92\u65ad\u526a\u517d\u6e05\u6dfb\u6dcb\u6df9\u6e20\u6e10\u6df7\u6e14\u6dd8\u6db2\u6de1\u6df1\u5a46\u6881\u6e17\u60c5\u60dc\u60ed\u60bc\u60e7\u60d5\u60ca\u60e8\u60ef\u5bc7\u5bc4\u5bbf\u7a91\u5bc6\u8c0b\u8c0e\u7978\u8c1c\u902e\u6562\u5c60\u5f39\u968f\u86cb\u9686\u9690\u5a5a\u5a76\u9888\u7ee9\u7eea\u7eed\u9a91\u7ef3\u7ef4\u7ef5\u7ef8\u7eff\u7434\u6591\u66ff\u6b3e\u582a\u642d\u5854\u8d8a\u8d81\u8d8b\u8d85\u63d0\u5824\u535a\u63ed\u559c\u63d2\u63ea\u641c\u716e\u63f4\u88c1\u6401\u6402\u6405\u63e1\u63c9\u65af\u671f\u6b3a\u8054\u6563\u60f9\u846c\u845b\u8463\u8461\u656c\u8471\u843d\u671d\u8f9c\u8475\u68d2\u68cb\u690d\u68ee\u6905\u6912\u68f5\u68cd\u68c9\u68da\u68d5\u60e0\u60d1\u903c\u53a8\u53a6\u786c\u786e\u96c1\u6b96\u88c2\u96c4\u6682\u96c5\u8f88\u60b2\u7d2b\u8f89\u655e\u8d4f\u638c\u6674\u6691\u6700\u91cf\u55b7\u6676\u5587\u9047\u558a\u666f\u8df5\u8dcc\u8dd1\u9057\u86d9\u86db\u8713\u559d\u5582\u5598\u5589\u5e45\u5e3d\u8d4c\u8d54\u9ed1\u94f8\u94fa\u94fe\u9500\u9501\u9504\u9505\u9508\u950b\u9510\u77ed\u667a\u6bef\u9e45\u5269\u7a0d\u7a0b\u7a00\u7a0e\u7b50\u7b49\u7b51\u7b56\u7b5b\u7b52\u7b54\u7b4b\u7b5d\u50b2\u5085\u724c\u5821\u96c6\u7126\u508d\u50a8\u5965\u8857\u60e9\u5fa1\u5faa\u8247\u8212\u756a\u91ca\u79bd\u814a\u813e\u8154\u9c81\u733e\u7334\u7136\u998b\u88c5\u86ee\u5c31\u75db\u7ae5\u9614\u5584\u7fa1\u666e\u7caa\u5c0a\u9053\u66fe\u7130\u6e2f\u6e56\u6e23\u6e7f\u6e29\u6e34\u6ed1\u6e7e\u6e21\u6e38\u6ecb\u6e89\u6124\u614c\u60f0\u6127\u6109\u6168\u5272\u5bd2\u5bcc\u7a9c\u7a9d\u7a97\u904d\u88d5\u88e4\u88d9\u8c22\u8c23\u8c26\u5c5e\u5c61\u5f3a\u7ca5\u758f\u9694\u9699\u7d6e\u5ac2\u767b\u7f0e\u7f13\u7f16\u9a97\u7f18\u745e\u9b42\u8086\u6444\u6478\u586b\u640f\u584c\u9f13\u6446\u643a\u642c\u6447\u641e\u5858\u644a\u849c\u52e4\u9e4a\u84dd\u5893\u5e55\u84ec\u84c4\u8499\u84b8\u732e\u7981\u695a\u60f3\u69d0\u6986\u697c\u6982\u8d56\u916c\u611f\u788d\u7891\u788e\u78b0\u7897\u788c\u96f7\u96f6\u96fe\u96f9\u8f93\u7763\u9f84\u9274\u775b\u7761\u776c\u9119\u611a\u6696\u76df\u6b47\u6697\u7167\u8de8\u8df3\u8dea\u8def\u8ddf\u9063\u86fe\u8702\u55d3\u7f6e\u7f6a\u7f69\u9519\u9521\u9523\u9524\u9526\u952e\u952f\u77ee\u8f9e\u7a20\u6101\u7b79\u7b7e\u7b80\u6bc1\u8205\u9f20\u50ac\u50bb\u50cf\u8eb2\u5fae\u6108\u9065\u8170\u8165\u8179\u817e\u817f\u89e6\u89e3\u9171\u75f0\u5ec9\u65b0\u97f5\u610f\u7cae\u6570\u714e\u5851\u6148\u7164\u714c\u6ee1\u6f20\u6e90\u6ee4\u6ee5\u6ed4\u6eaa\u6e9c\u6eda\u6ee8\u7cb1\u6ee9\u614e\u8a89\u585e\u8c28\u798f\u7fa4\u6bbf\u8f9f\u969c\u5acc\u5ac1\u53e0\u7f1d\u7f20\u9759\u78a7\u7483\u5899\u6487\u5609\u6467\u622a\u8a93\u5883\u6458\u6454\u805a\u853d\u6155\u66ae\u8511\u6a21\u69b4\u699c\u69a8\u6b4c\u906d\u9177\u917f\u9178\u78c1\u613f\u9700\u5f0a\u88f3\u9897\u55fd\u873b\u8721\u8747\u8718\u8d5a\u9539\u953b\u821e\u7a33\u7b97\u7ba9\u7ba1\u50da\u9f3b\u9b44\u8c8c\u819c\u818a\u8180\u9c9c\u7591\u9992\u88f9\u6572\u8c6a\u818f\u906e\u8150\u7626\u8fa3\u7aed\u7aef\u65d7\u7cbe\u6b49\u7184\u7194\u6f06\u6f02\u6f2b\u6ef4\u6f14\u6f0f\u6162\u5be8\u8d5b\u5bdf\u871c\u8c31\u5ae9\u7fe0\u718a\u51f3\u9aa1\u7f29\u6167\u6495\u6492\u8da3\u8d9f\u6491\u64ad\u649e\u64a4\u589e\u806a\u978b\u8549\u852c\u6a2a\u69fd\u6a31\u6a61\u98d8\u918b\u9189\u9707\u9709\u7792\u9898\u66b4\u778e\u5f71\u8e22\u8e0f\u8e29\u8e2a\u8776\u8774\u5631\u58a8\u9547\u9760\u7a3b\u9ece\u7a3f\u7a3c\u7bb1\u7bad\u7bc7\u50f5\u8eba\u50fb\u5fb7\u8258\u819d\u819b\u719f\u6469\u989c\u6bc5\u7cca\u9075\u6f5c\u6f6e\u61c2\u989d\u6170\u5288\u64cd\u71d5\u85af\u85aa\u8584\u98a0\u6a58\u6574\u878d\u9192\u9910\u5634\u8e44\u5668\u8d60\u9ed8\u955c\u8d5e\u7bee\u9080\u8861\u81a8\u96d5\u78e8\u51dd\u8fa8\u8fa9\u7cd6\u7cd5\u71c3\u6fa1\u6fc0\u61d2\u58c1\u907f\u7f34\u6234\u64e6\u97a0\u85cf\u971c\u971e\u77a7\u8e48\u87ba\u7a57\u7e41\u8fab\u8d62\u7cdf\u7ce0\u71e5\u81c2\u7ffc\u9aa4\u97ad\u8986\u8e66\u9570\u7ffb\u9e70\u8b66\u6500\u8e72\u98a4\u74e3\u7206\u7586\u58e4\u8000\u8e81\u56bc\u56b7\u7c4d\u9b54\u704c\u8822\u9738\u9732\u56ca\u7f50";
return str.charAt(this.getRandom(0, str.length()-1));
}
/**
* 生成汉字验证码
*
* @date Jun 13, 2008 3:14:14 PM
* @author Fly(Fly@mmgg.net)
* @return
*/
private BufferedImage getImageOfChineseChar(){
BufferedImage bi = new BufferedImage(150, 50,BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
g.setColor(this.getBgColor());
g.fillRect(0, 0, 150, 50);
codes="";
for (int i = 0; i < 4; i++) {
//抗锯齿,图片不失真
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
String rands=String.valueOf(this.getRandomChineseChar());
g.setColor(new Color((int) (Math.random() * 200), 0, (int) (Math.random() * 200)));
g.setFont(((Font) fonts.get((int) (Math.random() * fonts.size()))).deriveFont(this.getFace(), 28));
g.drawString(rands,32*i+10,35);
codes+=rands;
}
//画干扰线
this.drawLine(g, 2,150,50);
//画杂点
this.drawPoint(g, 20,150,50);
// 画个边框
//g.setColor(new Color((int) (Math.random() * 0x00FFFFFFF)));
//g.drawRect(1, 1, 150 - 2, 50 - 2);
g.dispose();
bi.flush();
return bi;
}
/**
* 读取文件
*
* @date Jun 13, 2008 4:35:09 PM
* @author Fly(Fly@mmgg.net)
* @param filepath
* @param ext
* @return
* @throws FileNotFoundException
* @throws IOException
*/
private List readfile(String filepath, String ext){
List list = new ArrayList();
try {
File file = new File(filepath);
//如果不是字体路径那么就是字体文件
if (!file.isDirectory()) {
list.add(file.getPath());
} else if (file.isDirectory()) {
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "/" + filelist[i]);
if (!readfile.isDirectory()) {
int x = filelist[i].lastIndexOf('.');
if (filelist[i].substring(x + 1).toLowerCase().equals(ext.toLowerCase())) {
list.add(readfile.getPath());
}
} else if (readfile.isDirectory()) {
//不再判断子文件夹
continue;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
/**
* 在图片画杂点
* @date Sep 12, 2008 4:41:46 PM
* @author Fly(Fly@mmgg.net)
* @param g
* @param num 杂点数目
* @param width 基于原点的最宽位置
* @param height 基于原点的最高位置
*/
private void drawPoint(Graphics2D g,int num,int width,int height){
Random random = new Random();
for (int i = 0; i < num; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
g.setColor(new Color((int) (Math.random() * 0x00FFFFFFF)));
g.setStroke(getStroke());
g.drawLine(x, y, x, y);
}
}
/**
* 在图片上画直线
* @date Sep 12, 2008 4:43:10 PM
* @author Fly(Fly@mmgg.net)
* @param g
* @param num 线条数
* @param width 基于原点的最宽位置
* @param height 基于原点的最高位置
*/
private void drawLine(Graphics2D g,int num,int width,int height){
for (int i = 0; i < num; i++) {
g.setColor(new Color((int) (Math.random() * 0x00FFFFFFF)));
g.drawLine((int) (Math.random() * width), (int) (Math.random() * height),
(int) (Math.random() * width), (int) (Math.random() * height));
}
}
public static void main(String[] args) throws IOException {
//List ls=null;
RandomCodeImage c=new RandomCodeImage();
try {
FileOutputStream o=new FileOutputStream("c:\\a.jpg");
FileOutputStream a=new FileOutputStream("c:\\b.jpg");
FileOutputStream d=new FileOutputStream("c:\\c.jpg");
//ImageIO.write(rotateJ2D(c.getImageOfABC123(),30),"JPG", o);
System.out.println(c.drawImageOfABC123(o));
System.out.println(c.drawImageOfChineseChar(a));
System.out.println(c.drawImageOfMultiplicationFormula(d));
} catch (FileNotFoundException e) {
e.printStackTrace();
throw e;
}
//System.out.println(c.getImageOfABC123());
//System.out.println((int)(Math.random()*6+1));
}
}