用Java来显示图片生成器

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

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

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

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

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

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

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

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

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

1、抽象类AbstractImageCreaTor的源代码

/**本代码在 http://www.bt285.cn  http://www.5a520.cn 已使用了 */

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成员变量值,该成员变量值表示图片的存储全路径。

Java代码

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 ffsetLeft = 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++;                    ffsetLeft = 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代码

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) {           utputFilePath = 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());

切忌贪婪,恨不得一次玩遍所有传说中的好景点,

用Java来显示图片生成器

相关文章:

你感兴趣的文章:

标签云: