标签:
package com.xxx;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.junit.Test;
public class IdentifyingCode {
private final int WIDTH = 70;
private final int HEIGHT = 30;
private final byte[] BACKGROUND_COLOR = new byte[]{127, 127, 127};
private final String FONT_STYLE = "微软雅黑";
private final char[] IDETIFYING_CODE = new char[]{‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘};
private final int CODE_LENGTH= 4;
private final int FONT_SIZE = 17;
//生成验证码图
public BufferedImage createIdentifyingCode(){
StringBuilder sb = new StringBuilder();
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_3BYTE_BGR);
//得到画笔
Graphics2D g = image.createGraphics();
//得到之前画笔的颜色
Color color = g.getColor();
//画图片边框
this.drawImageCheek(g);
this.drawCamouflageLine(g, 8);
//设置背景色
g.setBackground(new Color(BACKGROUND_COLOR[0]+128, BACKGROUND_COLOR[1]+128, BACKGROUND_COLOR[2]+128));
//设置字体大小及其样式
g.setFont(new Font(FONT_STYLE, Font.PLAIN, (int)(this.FONT_SIZE)){
private static final long serialVersionUID = 1L;});
//将验证字符加入图中
for(int i=0; i<CODE_LENGTH; i++){
//设置画笔颜色
g.setColor(this.randomColor());
//得到随机字符
char charCode = IDETIFYING_CODE[this.randomNumber(0, IDETIFYING_CODE.length-1)];
sb.append(charCode);
//将字符写到图片中
System.out.println(sb);
//g.rotate(this.randomNumber(45, 135));
g.drawString(""+charCode, WIDTH/5*(i+1)-5, (this.FONT_SIZE)+this.randomNumber(0, this.FONT_SIZE/2));
}
try {
ImageIO.write(image, "jpeg", new FileOutputStream("D:/11.jpeg"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return image;
}
/**
* 按照随机三原色生成一个颜色
*
* @return 颜色
*/
public Color randomColor(){
int min = 0;
int max = 255;
int red = randomNumber(min, max);
int green = randomNumber(min, max);
int yellow = randomNumber(min, max);
return new Color(red, green, yellow);
}
public void drawCamouflageLine(Graphics2D g, int quantity){
Color oldColor = g.getColor();
for(int i=0; i<quantity; i++){
g.setColor(this.randomColor());
g.drawLine(this.randomNumber(0, this.WIDTH), this.randomNumber(0, this.HEIGHT), this.randomNumber(0, this.WIDTH), this.randomNumber(0, this.HEIGHT));
}
g.setColor(oldColor);
}
/**
* 绘制图片内侧边框
*
* @param g 画笔
*/
public void drawImageCheek(Graphics2D g){
Color oldColor = g.getColor();
g.setColor(new Color(255, 255, 255));
g.drawLine(0, 0, this.WIDTH-2, 0);
g.drawLine(0, 0, 0, this.HEIGHT-2);
g.drawLine(0, this.HEIGHT-2, this.WIDTH-2, this.HEIGHT-2);
g.drawLine(this.WIDTH-2, 0, this.WIDTH-2, this.HEIGHT-2);
g.setColor(oldColor);
}
public int randomNumber(int min, int max){
return (int)((Math.random()*(max-min+1))+min);
}
public static void main(String[] args) {
new IdentifyingCode().createIdentifyingCode();
}
}
其中待解决的问题:背景没有设置正确
待优化:验证图中的验证码需要旋转增加程序识别难度
这是目前的效果
标签:
原文地址:http://www.cnblogs.com/chenchen-fei/p/5464389.html