標籤: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 圖片加浮水印,設定透明度。說明非常具體