The NoteBook of EricKong

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            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.      *            目標圖片    
          23.      * @param x    
          24.      *            修正值 默認在中 間    
          25.      * @param y    
          26.      *            修正值 默認在中 間    
          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.             // 水印 文件結束      
          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.      *            目標圖片    
          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.      *            比例不對時是 否需要補白    
          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.             // 計算 比例      
          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 閱讀(282) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 马龙县| 阿克陶县| 沂南县| 巴林右旗| 太原市| 博兴县| 合山市| 荆州市| 集安市| 澎湖县| 开江县| 奉化市| 白沙| 淳安县| 富顺县| 安国市| 荣成市| 子洲县| 南康市| 潮州市| 彭山县| 孟连| 江西省| 德化县| 东光县| 嵩明县| 金平| 莱阳市| 漳州市| 阿勒泰市| 大城县| 昔阳县| 外汇| 密云县| 平泉县| 梅河口市| 北海市| 武邑县| 治县。| 乐亭县| 嘉黎县|