一、本图片生成器具有以下功能特性:
1、可以设置图片的宽度、高度、外框颜色、背景色;
2、可以设置图片字体的大小、名称、颜色;
3、可以设置输出图片的格式,如JPEG、GIF等;
4、可以将图片存储到一个文件或者存储到一个输出流;
5、可以为图片增加若干条干扰线(在生成随机码图片时可用此特性);
6、打印在图片上的文字支持自动换行;
另外,本图片生成器还用到了模板方法模式。
二、下面列出相关的源代码
1、抽象类AbstractImageCreator的源代码
- 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;
-
-
-
-
-
-
-
-
- private void drawRandomLine(Graphics graph){
- for(int i=0;i<lineNum;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;
-
- }
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<lineNum;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成员变量值,该成员变量值表示图片的存储全路径。
- 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<text.length();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));
- }
-
- }
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<text.length();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成员变量值。
- 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);
- }
-
- }
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 = 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());
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());