java 圖片加浮水印,設定透明度。說明非常具體

來源:互聯網
上載者:User

標籤:puts   type   try   ack   ble   tar   exception   except   str   

package com.yidao.common;     import java.awt.AlphaComposite;import java.awt.Graphics2D;import java.awt.Image;import java.awt.RenderingHints;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;import javax.imageio.ImageIO;import javax.swing.ImageIcon;  /**   * 圖片加浮水印。設定透明度 * http://blog.csdn.net/hfmbook * @author Gary  * 建立日期:2014年12月16日 22:45:17 */  public class ImageMarkLogoByIcon {         /**       * @param args       */      public static void main(String[] args) {           String srcImgPath = "c:/1111.png";           String iconPath = "c:/0439.jpg";           String targerPath = "c:/c.png" ;          // 給圖片加入浮水印           ImageMarkLogoByIcon.markImageByIcon(iconPath, srcImgPath, targerPath , -45);      }       /**       * 給圖片加入浮水印       * @param iconPath 浮水印圖片路徑       * @param srcImgPath 源圖片路徑       * @param targerPath 靶心圖表片路徑       */      public static void markImageByIcon(String iconPath, String srcImgPath,               String targerPath) {           markImageByIcon(iconPath, srcImgPath, targerPath, null) ;     }       /**       * 給圖片加入浮水印、可設定浮水印圖片旋轉角度       * @param iconPath 浮水印圖片路徑       * @param srcImgPath 源圖片路徑       * @param targerPath 靶心圖表片路徑       * @param degree 浮水印圖片旋轉角度     */      public static void markImageByIcon(String iconPath, String srcImgPath,               String targerPath, Integer degree) {           OutputStream os = null;           try {               Image srcImg = ImageIO.read(new File(srcImgPath));             BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),                       srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);             // 得到畫筆對象               // Graphics g= buffImg.getGraphics();               Graphics2D g = buffImg.createGraphics();                 // 設定對線段的鋸齒狀邊緣處理               g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,                       RenderingHints.VALUE_INTERPOLATION_BILINEAR);                 g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg                       .getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);                 if (null != degree) {                   // 設定浮水印旋轉                   g.rotate(Math.toRadians(degree),                           (double) buffImg.getWidth() / 2, (double) buffImg                                   .getHeight() / 2);               }               // 浮水印圖象的路徑 浮水印一般為gif或者png的,這樣可設定透明度              ImageIcon imgIcon = new ImageIcon(iconPath);               // 得到Image對象。               Image img = imgIcon.getImage();               float alpha = 0.2f; // 透明度               g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,                       alpha));               // 表示浮水印圖片的位置               g.drawImage(img, 150, 300, null);               g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));               g.dispose();               os = new FileOutputStream(targerPath);               // 產生圖片               ImageIO.write(buffImg, "JPG", os);           } catch (Exception e) {               e.printStackTrace();           } finally {               try {                   if (null != os)                       os.close();               } catch (Exception e) {                   e.printStackTrace();               }           }       }   } 


java 圖片加浮水印,設定透明度。說明非常具體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.