📄 randomcheckpics.java
字号:
package com.scitel.checkDemo.bo;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Random;
public class RandomCheckPics {
private Properties p=new Properties();
private BufferedImage imgs;
private Graphics g;
private int height,width,length;
private String a;
private Random random=new Random();
private String s=new String();
//private String[] fontTypes = {"\u5b8b\u4f53","\u65b0\u5b8b\u4f53","\u9ed1\u4f53","\u6977\u4f53","\u96b6\u4e66"};
public RandomCheckPics(String realPath){
try {
InputStream in = new BufferedInputStream(new FileInputStream(realPath+"/WEB-INF/char.properties"));
p.load(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
height=Integer.parseInt(p.getProperty("pic.height"));
width=Integer.parseInt(p.getProperty("pic.width"));
length=Integer.parseInt(p.getProperty("char.lehgth"));
imgs=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
g=imgs.getGraphics();
a=p.getProperty("char.char");
g.setColor(getRandColor(random,200,250));
g.fillRect(0,0,width,height);
g.setColor(getRandColor(random,160,200));
g.setFont(new Font("Times New Roman",Font.PLAIN,Integer.parseInt(p.getProperty("noise.size"))));
String noise=p.getProperty("noise.char");
int sssss=Integer.parseInt(p.getProperty("noise.number"));
for (int i=0;i<sssss;i++)
{
g.drawString(noise,random.nextInt(width),random.nextInt(height));
}
for (int i=0;i<length;i++)
{
int start = random.nextInt(a.length());
String rand=a.substring(start,start+1);
s+=rand;
g.setColor(getRandColor(random,10,150));
g.setFont(new Font("\u5b8b\u4f53",Font.PLAIN,Integer.parseInt(p.getProperty("char.size"))));
g.drawString(rand,(width/length)*i,height-random.nextInt(height/2));
}
g.dispose();
}
private Color getRandColor(Random random,int fc,int bc)
{
if(fc>255) fc=255;
if(bc>255) bc=255;
int r=fc+random.nextInt(bc-fc);
int g=fc+random.nextInt(bc-fc);
int b=fc+random.nextInt(bc-fc);
return new Color(r,g,b);
}
public BufferedImage getImage(){
return imgs;
}
public String getString(){
return s;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -