1、获取缓冲区图像对象指定宽高、图像字节灰度
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
2、获取画布
Graphics g = image.getGraphics();
//将填充的区域为指定颜色
g.setColor(Color.white);
//填充矩形
g.fillRect(0, 0, width, height);
3、添加干扰线
/**
* 设置干扰线
* @param width 画布宽度
* @param height 画布高度
* @param interLine 线条数
* @param g
*/
private static void interferenceLine(int width, int height, int interLine, Graphics g) {
Random r = new Random();
if (interLine > 0) {
int x = r.nextInt(4), y = 0;
int x1 = width - r.nextInt(4), y1 = 0;
for (int i = 0; i < interLine; i++) {
g.setColor(Color.gray);
y = r.nextInt(height - r.nextInt(4));
y1 = r.nextInt(height - r.nextInt(4));
g.drawLine(x, y, x1, y1);
}
}
}
4、生成四位随机数
public static String randomChar(int count){
String[] sourceArray = new String[]{
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"0","1","2","3","4","5","6","7","8","9"
};
Random random = new Random();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < count; i++) {
int nextInt = random.nextInt(sourceArray.length);
buffer.append(sourceArray[nextInt]);
}
return buffer.toString();
}
5、设置内容(设置字体左右上下浮动)
/**
* 设置内容
* @param width 画布宽
* @param height 画布高
* @param g 画布对象
* @param textCode 四位随机码
* @param fsize 字体大小
* @return
*/
private static String setTypeface(int width, int height, Graphics g, String textCode,int fsize) {
//字体大小为图片高度的80%
fsize = (int) (height * 0.8);
int fx = 5;
int fy = fsize;
g.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, fsize));
//写字符
for (int i = 0; i < textCode.length(); i++) {
//每个字符高低是否随机
fy = (int) ((Math.random() * 0.3 + 0.6) * height);
//字符的颜色
g.setColor(getRandomColor());
//用预先设置好的颜色和字体来绘制文本
g.drawString(textCode.charAt(i) + "", fx, fy);
//依据宽度浮动
fx += (width / textCode.length()) * (Math.random() * 0.3 + 0.8);
}
return textCode;
}
6、根据x轴偏移
/**
*
* @param g 画布对象
* @param w1 宽
* @param h1 高
* @param color 颜色
*/
private static void shearX(Graphics g, int w1, int h1, Color color) {
Random random = new Random();
int period = 2;
boolean borderGap = true;
int frames = 1;
int phase = random.nextInt(2);
for (int i = 0; i < h1; i++) {
double d = (double) (period >> 1) * Math.sin((double) i / (double) period
+ (2.2831853071795862D * (double) phase) / (double) frames);
g.copyArea(0, i, w1, 1, (int) d, 0);
if (borderGap) {
g.setColor(color);
g.drawLine((int) d, i, 0, i);
g.drawLine((int) d + w1, i, w1, i);
}
}
}
7、根据y轴偏移
/**
*
* @param g 画布对象
* @param w1 宽
* @param h1 高
* @param color 颜色
*/
private static void shearY(Graphics g, int w1, int h1, Color color) {
Random random = new Random();
// 50
int period = random.nextInt(40) + 10;
boolean borderGap = true;
int frames = 20;
int phase = random.nextInt(2);
for (int i = 0; i < w1; i++) {
double d = (double) (period >> 1)
* Math.sin((double) i / (double) period
+ (2.2831853071795862D * (double) phase) / (double) frames);
g.copyArea(i, 0, 1, h1, 0, (int) d);
if (borderGap) {
g.setColor(color);
g.drawLine(i, (int) d, i, 0);
g.drawLine(i, (int) d + h1, i, h1);
}
}
}
8、设置噪点
/**
* 设置噪点
* @param width 宽
* @param height 高
* @param image 缓冲区图像对象
*/
private static void hotPixel(int width, int height, BufferedImage image) {
Random r = new Random();
// 噪声率
float yawpRate = 0.05f;
//噪点数量
int area = (int) (yawpRate * width * height);
for (int i = 0; i < area; i++) {
int xxx = r.nextInt(width);
int yyy = r.nextInt(height);
//int rgb = Color.GRAY.getRGB();
int rgb = getRandomColor().getRGB();
image.setRGB(xxx, yyy, rgb);
}
}
9、结束
g.dispose();
return image即可;