wang123

          用Java來顯示圖片生成器

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

               1、可以設置圖片的寬度、高度、外框顏色、背景色;

               2、可以設置圖片字體的大小、名稱、顏色;

               3、可以設置輸出圖片的格式,如JPEG、GIF等;

               4、可以將圖片存儲到一個文件或者存儲到一個輸出流;

               5、可以為圖片增加若干條干擾線(在生成隨機碼圖片時可用此特性);

               6、打印在圖片上的文字支持自動換行;

           

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

           

          二、下面列出相關的源代碼

               1、抽象類AbstractImageCreator的源代碼

           /**本代碼在 http://www.bt285.cn  http://www.5a520.cn 已使用了 */
          1. public abstract class AbstractImageCreator {   
          2.     private static Random rnd = new Random(new Date().getTime());   
          3.        
          4.     //圖片寬度   
          5.     private int width = 200;   
          6.        
          7.     //圖片高度   
          8.     private int height = 80;   
          9.        
          10.     //外框顏色   
          11.     private Color rectColor;   
          12.        
          13.     //背景色   
          14.     private Color bgColor;   
          15.        
          16.     //干擾線數目   
          17.     private int lineNum = 0;   
          18.        
          19.     //圖片格式   
          20.     private String formatName = "JPEG";   
          21.        
          22.     //字體顏色   
          23.     private Color fontColor = new Color(000);   
          24.        
          25.     //字體名稱   
          26.     private String fontName = "宋體";   
          27.        
          28.     //字體大小   
          29.     private int fontSize = 15;   
          30.        
          31.   
          32.     //##### 這里省略成員變臉的get、set方法 #####   
          33.   
          34.   
          35.     /**  
          36.      * 畫干擾線  
          37.      */  
          38.     private void drawRandomLine(Graphics graph){   
          39.         for(int i=0;i<lineNum;i++){   
          40.             //線條的顏色   
          41.             graph.setColor(getRandomColor(100155));   
          42.                
          43.             //線條兩端坐標值   
          44.             int x1 = rnd.nextInt(width);   
          45.             int y1 = rnd.nextInt(height);   
          46.                
          47.             int x2 = rnd.nextInt(width);   
          48.             int y2 = rnd.nextInt(height);   
          49.                
          50.             //畫線條   
          51.             graph.drawLine(x1, y1, x2, y2);   
          52.         }   
          53.     }   
          54.        
          55.     /**  
          56.      * 隨機獲取顏色對象  
          57.      */  
          58.     private Color getRandomColor(int base, int range){   
          59.         if((base + range) > 255) range = 255 - base;   
          60.            
          61.         int red = base + rnd.nextInt(range);   
          62.         int green = base + rnd.nextInt(range);   
          63.         int blue = base + rnd.nextInt(range);   
          64.            
          65.         return new Color(red, green, blue);   
          66.     }   
          67.            
          68.                 //該方法內應用了模板方法模式   
          69.     public void drawImage(String text)throws IOException{   
          70.         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);   
          71.            
          72.         if(rectColor == null) rectColor = new Color(000);   
          73.         if(bgColor == null) bgColor = new Color(240251200);   
          74.            
          75.         //獲取畫布   
          76.         Graphics graph = image.getGraphics();   
          77.            
          78.         //畫長方形   
          79.         graph.setColor(bgColor);   
          80.         graph.fillRect(00, width, height);   
          81.            
          82.         //外框   
          83.         graph.setColor(rectColor);   
          84.         graph.drawRect(00, width-1, height-1);   
          85.            
          86.         //畫干擾線   
          87.         drawRandomLine(graph);   
          88.            
          89.         //畫字符串   
          90.         drawString(graph, text);   
          91.            
          92.         //執行   
          93.         graph.dispose();   
          94.            
          95.         //輸出圖片結果   
          96.         saveImage(image);   
          97.     }   
          98.        
          99.     protected abstract void drawString(Graphics graph, String text);   
          100.        
          101.     protected abstract void saveImage(BufferedImage image)throws IOException;   
          102.        
          103. }  

           

               2、類DefaultImageCreator的源代碼

                    該類將生成的圖片存儲到一個文件中,需要設置outputFilePath成員變量值,該成員變量值表示圖片的存儲全路徑。

          Java代碼 復制代碼
          1. public class DefaultImageCreator extends AbstractImageCreator {   
          2.     private String outputFilePath;   
          3.        
          4.     public String getOutputFilePath() {   
          5.         return outputFilePath;   
          6.     }   
          7.   
          8.     public void setOutputFilePath(String outputFilePath) {   
          9.         this.outputFilePath = outputFilePath;   
          10.     }   
          11.        
          12.     public DefaultImageCreator(){   
          13.            
          14.     }   
          15.        
          16.     public DefaultImageCreator(String outputFilePath){   
          17.         this.outputFilePath = outputFilePath;   
          18.     }   
          19.   
          20.     @Override  
          21.     protected void drawString(Graphics graph, String text) {   
          22.         graph.setColor(getFontColor());   
          23.         Font font = new Font(getFontName(), Font.PLAIN, getFontSize());   
          24.         graph.setFont(font);   
          25.            
          26.         FontMetrics fm = graph.getFontMetrics(font);   
          27.         int fontHeight = fm.getHeight(); //字符的高度   
          28.            
          29.         int offsetLeft = 0;   
          30.         int rowIndex = 1;   
          31.         for(int i=0;i<text.length();i++){   
          32.             char c = text.charAt(i);   
          33.             int charWidth = fm.charWidth(c); //字符的寬度   
          34.   
          35.             //另起一行   
          36.             if(Character.isISOControl(c) || offsetLeft >= (getWidth()-charWidth)){   
          37.                 rowIndex++;   
          38.                 offsetLeft = 0;   
          39.             }   
          40.                
          41.             graph.drawString(String.valueOf(c), offsetLeft, rowIndex * fontHeight);   
          42.             offsetLeft += charWidth;   
          43.         }   
          44.     }   
          45.        
          46.     @Override  
          47.     protected void saveImage(BufferedImage image)throws IOException{   
          48.         ImageIO.write(image, getFormatName(), new File(outputFilePath));   
          49.     }   
          50.   
          51. }  

           

               3、類OutputStreamImageCreator的源代碼

                   該類將生成的圖片存儲到一個輸出流中,需要設置out成員變量值。

          Java代碼 復制代碼
          1. public class OutputStreamImageCreator extends DefaultImageCreator {   
          2.     private OutputStream out ;   
          3.        
          4.     public OutputStream getOut() {   
          5.         return out;   
          6.     }   
          7.   
          8.     public void setOut(OutputStream out) {   
          9.         this.out = out;   
          10.     }   
          11.        
          12.     public OutputStreamImageCreator(){   
          13.            
          14.     }   
          15.        
          16.     public OutputStreamImageCreator(OutputStream out){   
          17.         this.out = out;   
          18.     }   
          19.   
          20.     @Override  
          21.     public String getOutputFilePath() {   
          22.         return null;   
          23.     }   
          24.   
          25.     @Override  
          26.     public void setOutputFilePath(String outputFilePath) {   
          27.         outputFilePath = null;   
          28.     }   
          29.   
          30.     @Override  
          31.     protected void saveImage(BufferedImage image) throws IOException {   
          32.         if(out!=null) ImageIO.write(image, getFontName(), out);   
          33.     }   
          34.        
          35. }  

           

          三、實例代碼

               1、圖片存儲到文件

          StringBuffer sb = new StringBuffer();   
          1. sb.append("中華人民共和國\n");   
          2. sb.append("中華人民共和國\n");   
          3.   
          4. DefaultImageCreator creator = new DefaultImageCreator("c:\\img.jpeg");   
          5. creator.setWidth(150);   
          6. creator.setHeight(100);   
          7. creator.setLineNum(60);   
          8. creator.setFontSize(20);   
          9. creator.drawImage(sb.toString());  

           

          posted on 2009-03-23 18:49 閱讀(2507) 評論(0)  編輯  收藏

          <2009年3月>
          22232425262728
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          導航

          統計

          常用鏈接

          留言簿(3)

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 平利县| 新泰市| 视频| 莫力| 馆陶县| 宁蒗| 凤翔县| 来凤县| 江华| 开江县| 喀喇沁旗| 神木县| 京山县| 大厂| 神池县| 潜江市| 柏乡县| 景东| 龙江县| 福鼎市| 保康县| 衡山县| 陆河县| 襄樊市| 平顶山市| 湘阴县| 瑞昌市| 临沭县| 花垣县| 平乐县| 修武县| 余江县| 大港区| 佛冈县| 景德镇市| 高阳县| 琼结县| 义乌市| 手游| 旺苍县| 西乌珠穆沁旗|