java图片文字水印、图片水印、缩放、补白处理

 
  1. package  com.hmw.picMark;      
  2.      
  3. import  java.awt.AlphaComposite;      
  4. import  java.awt.Color;      
  5. import  java.awt.Font;      
  6. import  java.awt.Graphics2D;      
  7. import  java.awt.Image;      
  8. import  java.awt.geom.AffineTransform;      
  9. import  java.awt.image.AffineTransformOp;      
  10. import  java.awt.image.BufferedImage;      
  11. import  java.io.File;      
  12. import  java.io.IOException;      
  13.      
  14. import  javax.imageio.ImageIO;      
  15.      
  16.      
  17. public   final   class  ImageUtils {      
  18.      /**    
  19.      * 图片水印    
  20.      *     
  21.      * @param pressImg    
  22.      *            水印图片    
  23.      * @param targetImg    
  24.      *            目标图片    
  25.      * @param x    
  26.      *            修正值 默认在中间    
  27.      * @param y    
  28.      *            修正值 默认在中间    
  29.      * @param alpha    
  30.      *            透明度    
  31.      */      
  32.      public   final   static   void  pressImage(String pressImg, String targetImg,  int  x,  int  y,  float  alpha) {      
  33.          try  {      
  34.             File img =  new  File(targetImg);      
  35.             Image src = ImageIO.read(img);      
  36.              int  wideth = src.getWidth( null );      
  37.              int  height = src.getHeight( null );      
  38.             BufferedImage image =  new  BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);      
  39.             Graphics2D g = image.createGraphics();      
  40.             g.drawImage(src,  00 , wideth, height,  null );      
  41.              // 水印文件      
  42.             Image src_biao = ImageIO.read( new  File(pressImg));      
  43.              int  wideth_biao = src_biao.getWidth( null );      
  44.              int  height_biao = src_biao.getHeight( null );      
  45.             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));      
  46.             g.drawImage(src_biao, (wideth - wideth_biao) /  2 , (height - height_biao) /  2 , wideth_biao, height_biao,  null );      
  47.              // 水印文件结束      
  48.             g.dispose();      
  49.             ImageIO.write((BufferedImage) image,  "jpg" , img);      
  50.         }  catch  (Exception e) {      
  51.             e.printStackTrace();      
  52.         }      
  53.     }      
  54.      
  55.      /**    
  56.      * 文字水印    
  57.      *     
  58.      * @param pressText    
  59.      *            水印文字    
  60.      * @param targetImg    
  61.      *            目标图片    
  62.      * @param fontName    
  63.      *            字体名称    
  64.      * @param fontStyle    
  65.      *            字体样式    
  66.      * @param color    
  67.      *            字体颜色    
  68.      * @param fontSize    
  69.      *            字体大小    
  70.      * @param x    
  71.      *            修正值    
  72.      * @param y    
  73.      *            修正值    
  74.      * @param alpha    
  75.      *            透明度    
  76.      */      
  77.      public   static   void  pressText(String pressText, String targetImg, String fontName,  int  fontStyle, Color color,  int  fontSize,  int  x,  int  y,  float  alpha) {      
  78.          try  {      
  79.             File img =  new  File(targetImg);      
  80.             Image src = ImageIO.read(img);      
  81.              int  width = src.getWidth( null );      
  82.              int  height = src.getHeight( null );      
  83.             BufferedImage image =  new  BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);      
  84.             Graphics2D g = image.createGraphics();      
  85.             g.drawImage(src,  00 , width, height,  null );      
  86.             g.setColor(color);      
  87.             g.setFont( new  Font(fontName, fontStyle, fontSize));      
  88.             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));      
  89.             g.drawString(pressText, (width - (getLength(pressText) * fontSize)) /  2  + x, (height - fontSize) /  2  + y);      
  90.             g.dispose();      
  91.             ImageIO.write((BufferedImage) image,  "jpg" , img);      
  92.         }  catch  (Exception e) {      
  93.             e.printStackTrace();      
  94.         }      
  95.     }      
  96.      
  97.      /**    
  98.      * 缩放    
  99.      *     
  100.      * @param filePath    
  101.      *            图片路径    
  102.      * @param height    
  103.      *            高度    
  104.      * @param width    
  105.      *            宽度    
  106.      * @param bb    
  107.      *            比例不对时是否需要补白    
  108.      */      
  109.      public   static   void  resize(String filePath,  int  height,  int  width,  boolean  bb) {      
  110.          try  {      
  111.              double  ratio =  0// 缩放比例      
  112.             File f =  new  File(filePath);      
  113.             BufferedImage bi = ImageIO.read(f);      
  114.             Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);      
  115.              // 计算比例      
  116.              if  ((bi.getHeight() > height) || (bi.getWidth() > width)) {      
  117.                  if  (bi.getHeight() > bi.getWidth()) {      
  118.                     ratio = ( new  Integer(height)).doubleValue() / bi.getHeight();      
  119.                 }  else  {      
  120.                     ratio = ( new  Integer(width)).doubleValue() / bi.getWidth();      
  121.                 }      
  122.                 AffineTransformOp op =  new  AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio),  null );      
  123.                 itemp = op.filter(bi,  null );      
  124.             }      
  125.              if  (bb) {      
  126.                 BufferedImage image =  new  BufferedImage(width, height,      
  127.                         BufferedImage.TYPE_INT_RGB);      
  128.                 Graphics2D g = image.createGraphics();      
  129.                 g.setColor(Color.white);      
  130.                 g.fillRect( 00 , width, height);      
  131.                  if  (width == itemp.getWidth( null ))      
  132.                     g.drawImage(itemp,  0 , (height - itemp.getHeight( null )) /  2 , itemp.getWidth( null ), itemp.getHeight( null ), Color.white,  null );      
  133.                  else      
  134.                     g.drawImage(itemp, (width - itemp.getWidth( null )) /  20 , itemp.getWidth( null ), itemp.getHeight( null ), Color.white,  null );      
  135.                 g.dispose();      
  136.                 itemp = image;      
  137.             }      
  138.             ImageIO.write((BufferedImage) itemp,  "jpg" , f);      
  139.         }  catch  (IOException e) {      
  140.             e.printStackTrace();      
  141.         }      
  142.     }      
  143.      
  144.      public   static   void  main(String[] args)  throws  IOException {      
  145.         pressImage( "G://imgtest//sy.jpg""G://imgtest//testjpg"00 , 5f);      
  146.         pressText( "我是文字水印""G://imgtest//testjpg""黑体"36 , Color.white,  80 ,      
  147.                  00 , 3f);      
  148.         resize( "G://imgtest//testjpg"500500true );      
  149.     }      
  150.      
  151.      public   static   int  getLength(String text) {      
  152.          int  length =  0 ;      
  153.          for  ( int  i =  0 ; i < text.length(); i++) {      
  154.              if  ( new  String(text.charAt(i) +  "" ).getBytes().length >  1 ) {      
  155.                 length +=  2 ;      
  156.             }  else  {      
  157.                 length +=  1 ;      
  158.             }      
  159.         }      
  160.          return  length /  2 ;      
  161.     }      
  162. }     
  163.  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值