一本图片生成器具有以下功能特性
可以设置图片的宽度高度外框颜色背景色
可以设置图片字体的大小名称颜色
可以设置输出图片的格式如JPEGGIF等
可以将图片存储到一个文件或者存储到一个输出流
可以为图片增加若干条干扰线(在生成随机码图片时可用此特性)
打印在图片上的文字支持自动换行
另外本图片生成器还用到了模板方法模式
二下面列出相关的源代码
抽象类AbstractImageCreator的源代码
/**本代码在 已使用了 */
publicabstractclassAbstractImageCreator{
privatestaticRandomrnd=newRandom(newDate()getTime());
//图片宽度
privateintwidth=;
//图片高度
privateintheight=;
//外框颜色
privateColorrectColor;
//背景色
privateColorbgColor;
//干扰线数目
privateintlineNum=;
//图片格式
privateStringformatName=JPEG;
//字体颜色
privateColorfontColor=newColor();
//字体名称
privateStringfontName=宋体;
//字体大小
privateintfontSize=;
//#####这里省略成员变脸的getset方法#####
/**
*画干扰线
*/
privatevoiddrawRandomLine(Graphicsgraph){
for(inti=;i<lineNum;i++){
//线条的颜色
graphsetColor(getRandomColor());
//线条两端坐标值
intx=rndnextInt(width);
inty=rndnextInt(height);
intx=rndnextInt(width);
inty=rndnextInt(height);
//画线条
graphdrawLine(xyxy);
}
}
/**
*随机获取颜色对象
*/
privateColorgetRandomColor(intbaseintrange){
if((base+range)>)range=base;
intred=base+rndnextInt(range);
intgreen=base+rndnextInt(range);
intblue=base+rndnextInt(range);
returnnewColor(redgreenblue);
}
//该方法内应用了模板方法模式
publicvoiddrawImage(Stringtext)throwsIOException{
BufferedImageimage=newBufferedImage(widthheightBufferedImageTYPE_INT_RGB);
if(rectColor==null)rectColor=newColor();
if(bgColor==null)bgColor=newColor();
//获取画布
Graphicsgraph=imagegetGraphics();
//画长方形
graphsetColor(bgColor);
graphfillRect(widthheight);
//外框
graphsetColor(rectColor);
graphdrawRect(widthheight);
//画干扰线
drawRandomLine(graph);
//画字符串
drawString(graphtext);
//执行
graphdispose();
//输出图片结果
saveImage(image);
}
protectedabstractvoiddrawString(GraphicsgraphStringtext);
protectedabstractvoidsaveImage(BufferedImageimage)throwsIOException;
}
类DefaultImageCreator的源代码
该类将生成的图片存储到一个文件中需要设置outputFilePath成员变量值该成员变量值表示图片的存储全路径
Java代码
publicclassDefaultImageCreatorextendsAbstractImageCreator{
privateStringoutputFilePath;
publicStringgetOutputFilePath(){
returnoutputFilePath;
}
publicvoidsetOutputFilePath(StringoutputFilePath){
thisoutputFilePath=outputFilePath;
}
publicDefaultImageCreator(){
}
publicDefaultImageCreator(StringoutputFilePath){
thisoutputFilePath=outputFilePath;
}
@Override
protectedvoiddrawString(GraphicsgraphStringtext){
graphsetColor(getFontColor());
Fontfont=newFont(getFontName()FontPLAINgetFontSize());
graphsetFont(font);
FontMetricsfm=graphgetFontMetrics(font);
intfontHeight=fmgetHeight();//字符的高度
intoffsetLeft=;
introwIndex=;
for(inti=;i<textlength();i++){
charc=textcharAt(i);
intcharWidth=fmcharWidth(c);//字符的宽度
//另起一行
if(CharacterisISOControl(c)||offsetLeft>=(getWidth()charWidth)){
rowIndex++;
offsetLeft=;
}
graphdrawString(StringvalueOf(c)offsetLeftrowIndex*fontHeight);
offsetLeft+=charWidth;
}
}
@Override
protectedvoidsaveImage(BufferedImageimage)throwsIOException{
ImageIOwrite(imagegetFormatName()newFile(outputFilePath));
}
}
类OutputStreamImageCreator的源代码
该类将生成的图片存储到一个输出流中需要设置out成员变量值
Java代码
publicclassOutputStreamImageCreatorextendsDefaultImageCreator{
privateOutputStreamout;
publicOutputStreamgetOut(){
returnout;
}
publicvoidsetOut(OutputStreamout){
thisout=out;
}
publicOutputStreamImageCreator(){
}
publicOutputStreamImageCreator(OutputStreamout){
thisout=out;
}
@Override
publicStringgetOutputFilePath(){
returnnull;
}
@Override
publicvoidsetOutputFilePath(StringoutputFilePath){
outputFilePath=null;
}
@Override
protectedvoidsaveImage(BufferedImageimage)throwsIOException{
if(out!=null)ImageIOwrite(imagegetFontName()out);
}
}
三实例代码
图片存储到文件
StringBuffersb=newStringBuffer();
sbappend(中华人民共和国\n);
sbappend(中华人民共和国\n);
DefaultImageCreatorcreator=newDefaultImageCreator(c:\\imgjpeg);
creatorsetWidth();
creatorsetHeight();
creatorsetLineNum();
creatorsetFontSize();
creatordrawImage(sbtoString());