一、本图片生成器具有以下功能特性:

1、可以设置图片的宽度、高度、外框颜色、背景色;

2、可以设置图片字体的大小、名称、颜色;

3、可以设置输出图片的格式,如JPEG、GIF等;

4、可以将图片存储到一个文件或者存储到一个输出流;

5、可以为图片增加若干条干扰线(在生成随机码图片时可用此特性);

6、打印在图片上的文字支持自动换行;

另外,本图片生成器还用到了模板方法模式。

二、下面列出相关的源代码

1、抽象类AbstractImageCreator的源代码
publicabstractclassAbstractImageCreator {
privatestaticRandom rnd =newRandom(newDate().getTime());
//图片宽度
privateintwidth =200;
//图片高度
privateintheight =80;
//外框颜色
privateColor rectColor;
//背景色
privateColor bgColor;
//干扰线数目
privateintlineNum =0;
//图片格式
privateString formatName ="JPEG";
//字体颜色
privateColor fontColor =newColor(0,0,0);
//字体名称
privateString fontName ="宋体";
//字体大小
privateintfontSize =15;
//##### 这里省略成员变脸的get、set方法 #####
/**
* 画干扰线
*/
privatevoiddrawRandomLine(Graphics graph){
for(inti=0;i
//线条的颜色
graph.setColor(getRandomColor(100,155));
//线条两端坐标值
intx1 = rnd.nextInt(width);
inty1 = rnd.nextInt(height);
intx2 = rnd.nextInt(width);
inty2 = rnd.nextInt(height);
//画线条
graph.drawLine(x1, y1, x2, y2);
}
}
/**
* 随机获取颜色对象
*/
privateColor getRandomColor(intbase,intrange){
if((base + range) >255) range =255- base;
intred = base + rnd.nextInt(range);
intgreen = base + rnd.nextInt(range);
intblue = base + rnd.nextInt(range);
returnnewColor(red, green, blue);
}
//该方法内应用了模板方法模式
publicvoiddrawImage(String text)throwsIOException{
BufferedImage image =newBufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
if(rectColor ==null) rectColor =newColor(0,0,0);
if(bgColor ==null) bgColor =newColor(240,251,200);
//获取画布
Graphics graph = image.getGraphics();
//画长方形
graph.setColor(bgColor);
graph.fillRect(0,0, width, height);
//外框
graph.setColor(rectColor);
graph.drawRect(0,0, width-1, height-1);
//画干扰线
drawRandomLine(graph);
//画字符串
drawString(graph, text);
//执行
graph.dispose();
//输出图片结果
saveImage(image);
}
protectedabstractvoiddrawString(Graphics graph, String text);
protectedabstractvoidsaveImage(BufferedImage image)throwsIOException;
}
public abstract class AbstractImageCreator {
private static Random rnd = new Random(new Date().getTime());
//图片宽度
private int width = 200;
//图片高度
private int height = 80;
//外框颜色
private Color rectColor;
//背景色
private Color bgColor;
//干扰线数目
private int lineNum = 0;
//图片格式
private String formatName = "JPEG";
//字体颜色
private Color fontColor = new Color(0, 0, 0);
//字体名称
private String fontName = "宋体";
//字体大小
private int fontSize = 15;
//##### 这里省略成员变脸的get、set方法 #####
/**
* 画干扰线
*/
private void drawRandomLine(Graphics graph){
for(int i=0;i
//线条的颜色
graph.setColor(getRandomColor(100, 155));
//线条两端坐标值
int x1 = rnd.nextInt(width);
int y1 = rnd.nextInt(height);
int x2 = rnd.nextInt(width);
int y2 = rnd.nextInt(height);
//画线条
graph.drawLine(x1, y1, x2, y2);
}
}
/**
* 随机获取颜色对象
*/
private Color getRandomColor(int base, int range){
if((base + range) > 255) range = 255 - base;
int red = base + rnd.nextInt(range);
int green = base + rnd.nextInt(range);
int blue = base + rnd.nextInt(range);
return new Color(red, green, blue);
}
//该方法内应用了模板方法模式
public void drawImage(String text)throws IOException{
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
if(rectColor == null) rectColor = new Color(0, 0, 0);
if(bgColor == null) bgColor = new Color(240, 251, 200);
//获取画布
Graphics graph = image.getGraphics();
//画长方形
graph.setColor(bgColor);
graph.fillRect(0, 0, width, height);
//外框
graph.setColor(rectColor);
graph.drawRect(0, 0, width-1, height-1);
//画干扰线
drawRandomLine(graph);
//画字符串
drawString(graph, text);
//执行
graph.dispose();
//输出图片结果
saveImage(image);
}
protected abstract void drawString(Graphics graph, String text);
protected abstract void saveImage(BufferedImage image)throws IOException;
}
2、类DefaultImageCreator的源代码
该类将生成的图片存储到一个文件中,需要设置outputFilePath成员变量值,该成员变量值表示图片的存储全路径。
Java代码 
publicclassDefaultImageCreatorextendsAbstractImageCreator {
privateString outputFilePath;
publicString getOutputFilePath() {
returnoutputFilePath;
}
publicvoidsetOutputFilePath(String outputFilePath) {
this.outputFilePath = outputFilePath;
}
publicDefaultImageCreator(){
}
publicDefaultImageCreator(String outputFilePath){
this.outputFilePath = outputFilePath;
}
@Override
protectedvoiddrawString(Graphics graph, String text) {
graph.setColor(getFontColor());
Font font =newFont(getFontName(), Font.PLAIN, getFontSize());
graph.setFont(font);
FontMetrics fm = graph.getFontMetrics(font);
intfontHeight = fm.getHeight();//字符的高度
intoffsetLeft =0;
introwIndex =1;
for(inti=0;i
charc = text.charAt(i);
intcharWidth = fm.charWidth(c);//字符的宽度
//另起一行
if(Character.isISOControl(c) || offsetLeft >= (getWidth()-charWidth)){
rowIndex++;
offsetLeft =0;
}
graph.drawString(String.valueOf(c), offsetLeft, rowIndex * fontHeight);
offsetLeft += charWidth;
}
}
@Override
protectedvoidsaveImage(BufferedImage image)throwsIOException{
ImageIO.write(image, getFormatName(),newFile(outputFilePath));
}
}
public class DefaultImageCreator extends AbstractImageCreator {
private String outputFilePath;
public String getOutputFilePath() {
return outputFilePath;
}
public void setOutputFilePath(String outputFilePath) {
this.outputFilePath = outputFilePath;
}
public DefaultImageCreator(){
}
public DefaultImageCreator(String outputFilePath){
this.outputFilePath = outputFilePath;
}
@Override
protected void drawString(Graphics graph, String text) {
graph.setColor(getFontColor());
Font font = new Font(getFontName(), Font.PLAIN, getFontSize());
graph.setFont(font);
FontMetrics fm = graph.getFontMetrics(font);
int fontHeight = fm.getHeight(); //字符的高度
int offsetLeft = 0;
int rowIndex = 1;
for(int i=0;i
char c = text.charAt(i);
int charWidth = fm.charWidth(c); //字符的宽度
//另起一行
if(Character.isISOControl(c) || offsetLeft >= (getWidth()-charWidth)){
rowIndex++;
offsetLeft = 0;
}
graph.drawString(String.valueOf(c), offsetLeft, rowIndex * fontHeight);
offsetLeft += charWidth;
}
}
@Override
protected void saveImage(BufferedImage image)throws IOException{
ImageIO.write(image, getFormatName(), new File(outputFilePath));
}
}
3、类OutputStreamImageCreator的源代码
该类将生成的图片存储到一个输出流中,需要设置out成员变量值。
Java代码 
publicclassOutputStreamImageCreatorextendsDefaultImageCreator {
privateOutputStream out ;
publicOutputStream getOut() {
returnout;
}
publicvoidsetOut(OutputStream out) {
this.out = out;
}
publicOutputStreamImageCreator(){
}
publicOutputStreamImageCreator(OutputStream out){
this.out = out;
}
@Override
publicString getOutputFilePath() {
returnnull;
}
@Override
publicvoidsetOutputFilePath(String outputFilePath) {
outputFilePath =null;
}
@Override
protectedvoidsaveImage(BufferedImage image)throwsIOException {
if(out!=null) ImageIO.write(image, getFontName(), out);
}
}
public class OutputStreamImageCreator extends DefaultImageCreator {
private OutputStream out ;
public OutputStream getOut() {
return out;
}
public void setOut(OutputStream out) {
this.out = out;
}
public OutputStreamImageCreator(){
}
public OutputStreamImageCreator(OutputStream out){
this.out = out;
}
@Override
public String getOutputFilePath() {
return null;
}
@Override
public void setOutputFilePath(String outputFilePath) {
outputFilePath = null;
}
@Override
protected void saveImage(BufferedImage image) throws IOException {
if(out!=null) ImageIO.write(image, getFontName(), out);
}
}
三、实例代码
1、图片存储到文件
StringBuffer sb =newStringBuffer();
sb.append("中华人民共和国\n");
sb.append("中华人民共和国\n");
DefaultImageCreator creator =newDefaultImageCreator("c:\\img.jpeg");
creator.setWidth(150);
creator.setHeight(100);
creator.setLineNum(60);
creator.setFontSize(20);
creator.drawImage(sb.toString());
StringBuffer sb = new StringBuffer();
sb.append("中华人民共和国\n");
sb.append("中华人民共和国\n");
DefaultImageCreator creator = new DefaultImageCreator("c:\\img.jpeg");
creator.setWidth(150);
creator.setHeight(100);
creator.setLineNum(60);
creator.setFontSize(20);
creator.drawImage(sb.toString());