<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    wang123

    用Java來顯示圖片生成器

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

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

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

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

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

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

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

     

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

     

    二、下面列出相關(guān)的源代碼

         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.     //干擾線數(shù)目   
    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.                 //該方法內(nèi)應(yīng)用了模板方法模式   
    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.         //執(zhí)行   
    93.         graph.dispose();   
    94.            
    95.         //輸出圖片結(jié)果   
    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的源代碼

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

    Java代碼 復(fù)制代碼
    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的源代碼

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

    Java代碼 復(fù)制代碼
    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 閱讀(2505) 評論(0)  編輯  收藏

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

    導(dǎo)航

    統(tǒng)計

    常用鏈接

    留言簿(3)

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲高清免费视频| xxx毛茸茸的亚洲| 国产无人区码卡二卡三卡免费 | 亚洲第一福利网站| 成年免费大片黄在线观看岛国| 狼人大香伊蕉国产WWW亚洲| 亚洲成AV人片在线观看无| 天天摸夜夜摸成人免费视频| 在线免费视频你懂的| 亚洲综合小说另类图片动图| 国产AV无码专区亚洲A∨毛片| 免费做爰猛烈吃奶摸视频在线观看| 一级毛片成人免费看a| 亚洲精品免费在线| 亚洲综合久久夜AV | 国产h视频在线观看免费| 中国好声音第二季免费播放| 久久乐国产综合亚洲精品| 精品久久香蕉国产线看观看亚洲| 手机在线毛片免费播放| 怡红院免费的全部视频| 亚洲精品国产高清在线观看| 4480yy私人影院亚洲| 国产精品V亚洲精品V日韩精品| 欧美大尺寸SUV免费| 久久午夜无码免费| 人与动性xxxxx免费| 亚洲国产成人99精品激情在线| 亚洲深深色噜噜狠狠爱网站| 免费吃奶摸下激烈视频| 久久精品免费一区二区| 成人性做爰aaa片免费看| 黄色三级三级三级免费看| 99亚偷拍自图区亚洲| 亚洲网站在线观看| 亚洲色欲久久久综合网| 亚洲成a人在线看天堂无码| 大陆一级毛片免费视频观看| 亚州免费一级毛片| 久久精品免费一区二区| 99国产精品免费观看视频|