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 阅读(270) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: