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

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

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

    The NoteBook of EricKong

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks
    1. import java.awt.AlphaComposite;      
    2. import java.awt.Color;      
    3. import java.awt.Font;      
    4. import java.awt.Graphics2D;      
    5. import java.awt.Image;      
    6. import java.awt.geom.AffineTransform;      
    7. import java.awt.image.AffineTransformOp;      
    8. import java.awt.image.BufferedImage;      
    9. import java.io.File;      
    10. import java.io.IOException;      
    11.      
    12. import javax.imageio.ImageIO;      
    13.      
    14.      
    15. public final class ImageUtils {      
    16.     /**    
    17.      * 圖片水印    
    18.      *     
    19.      * @param pressImg    
    20.      *            水印圖片    
    21.      * @param targetImg    
    22.      *            目標(biāo)圖片    
    23.      * @param x    
    24.      *            修正值 默認(rèn)在中 間    
    25.      * @param y    
    26.      *            修正值 默認(rèn)在中 間    
    27.      * @param alpha    
    28.      *            透明度    
    29.      */     
    30.     public final static void pressImage(String pressImg, String targetImg, int x, int y, float alpha) {      
    31.         try {      
    32.             File img = new File(targetImg);      
    33.             Image src = ImageIO.read(img);      
    34.             int wideth = src.getWidth(null);      
    35.             int height = src.getHeight(null);      
    36.             BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);      
    37.             Graphics2D g = image.createGraphics();      
    38.             g.drawImage(src, 0, 0, wideth, height, null);      
    39.             // 水印 文件      
    40.             Image src_biao = ImageIO.read(new File(pressImg));      
    41.             int wideth_biao = src_biao.getWidth(null);      
    42.             int height_biao = src_biao.getHeight(null);      
    43.             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));      
    44.             g.drawImage(src_biao, (wideth - wideth_biao) / 2, (height - height_biao) / 2, wideth_biao, height_biao, null);      
    45.             // 水印 文件結(jié)束      
    46.             g.dispose();      
    47.             ImageIO.write((BufferedImage) image, "jpg", img);      
    48.         } catch (Exception e) {      
    49.             e.printStackTrace();      
    50.         }      
    51.     }      
    52.      
    53.     /**    
    54.      * 文字水印    
    55.      *     
    56.      * @param pressText    
    57.      *            水印文字    
    58.      * @param targetImg    
    59.      *            目標(biāo)圖片    
    60.      * @param fontName    
    61.      *            字體名稱    
    62.      * @param fontStyle    
    63.      *            字體樣式    
    64.      * @param color    
    65.      *            字體顏色    
    66.      * @param fontSize    
    67.      *            字體大小    
    68.      * @param x    
    69.      *            修正值    
    70.      * @param y    
    71.      *            修正值    
    72.      * @param alpha    
    73.      *            透明度    
    74.      */     
    75.     public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color color, int fontSize, int x, int y, float alpha) {      
    76.         try {      
    77.             File img = new File(targetImg);      
    78.             Image src = ImageIO.read(img);      
    79.             int width = src.getWidth(null);      
    80.             int height = src.getHeight(null);      
    81.             BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);      
    82.             Graphics2D g = image.createGraphics();      
    83.             g.drawImage(src, 0, 0, width, height, null);      
    84.             g.setColor(color);      
    85.             g.setFont(new Font(fontName, fontStyle, fontSize));      
    86.             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));      
    87.             g.drawString(pressText, (width - (getLength(pressText) * fontSize)) / 2 + x, (height - fontSize) / 2 + y);      
    88.             g.dispose();      
    89.             ImageIO.write((BufferedImage) image, "jpg", img);      
    90.         } catch (Exception e) {      
    91.             e.printStackTrace();      
    92.         }      
    93.     }      
    94.      
    95.     /**    
    96.      * 縮放    
    97.      *     
    98.      * @param filePath    
    99.      *            圖片路徑    
    100.      * @param height    
    101.      *            高度    
    102.      * @param width    
    103.      *            寬度    
    104.      * @param bb    
    105.      *            比例不對時(shí)是 否需要補(bǔ)白    
    106.      */     
    107.     public static void resize(String filePath, int height, int width, boolean bb) {      
    108.         try {      
    109.             double ratio = 0; // 縮放比例      
    110.             File f = new File(filePath);      
    111.             BufferedImage bi = ImageIO.read(f);      
    112.             Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);      
    113.             // 計(jì)算 比例      
    114.             if ((bi.getHeight() > height) || (bi.getWidth() > width)) {      
    115.                 if (bi.getHeight() > bi.getWidth()) {      
    116.                     ratio = (new Integer(height)).doubleValue() / bi.getHeight();      
    117.                 } else {      
    118.                     ratio = (new Integer(width)).doubleValue() / bi.getWidth();      
    119.                 }      
    120.                 AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);      
    121.                 itemp = op.filter(bi, null);      
    122.             }      
    123.             if (bb) {      
    124.                 BufferedImage image = new BufferedImage(width, height,      
    125.                         BufferedImage.TYPE_INT_RGB);      
    126.                 Graphics2D g = image.createGraphics();      
    127.                 g.setColor(Color.white);      
    128.                 g.fillRect(0, 0, width, height);      
    129.                 if (width == itemp.getWidth(null))      
    130.                     g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);      
    131.                 else     
    132.                     g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);      
    133.                 g.dispose();      
    134.                 itemp = image;      
    135.             }      
    136.             ImageIO.write((BufferedImage) itemp, "jpg", f);      
    137.         } catch (IOException e) {      
    138.             e.printStackTrace();      
    139.         }      
    140.     }      
    141.      
    142.     public static void main(String[] args) throws IOException {      
    143.         pressImage("G:""imgtest""sy.jpg", "G:""imgtest""testjpg", 0, 0, 5f);      
    144.         pressText("我是 文字水印", "G:""imgtest""testjpg", "黑體", 36, Color.white, 80,      
    145.                 0, 0, 3f);      
    146.         resize("G:""imgtest""testjpg", 500, 500, true);      
    147.     }      
    148.      
    149.     public static int getLength(String text) {      
    150.         int length = 0;      
    151.         for (int i = 0; i < text.length(); i++) {      
    152.             if (new String(text.charAt(i) + "").getBytes().length > 1) {      
    153.                 length += 2;      
    154.             } else {      
    155.                 length += 1;      
    156.             }      
    157.         }      
    158.         return length / 2;      
    159.     }      
    160. }     
    161.  
    posted on 2010-07-14 20:04 Eric_jiang 閱讀(280) 評(píng)論(0)  編輯  收藏 所屬分類: Java
    主站蜘蛛池模板: 亚洲人成网站观看在线播放| 美女在线视频观看影院免费天天看| 亚洲色大成网站www永久男同| 亚洲国产成a人v在线| 亚洲精品一卡2卡3卡三卡四卡| 亚洲视频在线一区二区三区| 亚洲色图国产精品| 亚洲精品国产第1页| 亚洲人成日本在线观看| 亚洲国产综合精品中文第一| 亚洲国产区男人本色在线观看| 亚洲人成网站免费播放| 亚洲精品伦理熟女国产一区二区 | 国产卡一卡二卡三免费入口| 无码国产精品一区二区免费虚拟VR| 很黄很黄的网站免费的| 18禁无遮挡无码网站免费| 在线免费观看污网站| 免费国产不卡午夜福在线| 亚洲精品成人片在线观看| 亚洲精品自在在线观看| 亚洲天天做日日做天天欢毛片| 亚洲欧洲日韩综合| 亚洲午夜无码久久| 特级毛片全部免费播放| 丁香花在线观看免费观看图片| 日本免费电影一区二区| 97国产免费全部免费观看| 免费无码又爽又高潮视频| 亚洲成av人片在线观看天堂无码 | 成人免费a级毛片| 四虎永久免费影院| 亚洲中文字幕无码一区二区三区| 亚洲精品卡2卡3卡4卡5卡区| 亚洲综合色丁香麻豆| 亚洲乱码中文字幕在线| 一级做a毛片免费视频| 香蕉成人免费看片视频app下载| 青青在线久青草免费观看| 亚洲精品无码av天堂| 亚洲av无码一区二区三区网站 |