图片加水印(学习笔记)
import java.io.OutputStream;/** * * @接口 */public interface IImageWatermark { void watermark(); void setOutputStream(OutputStream out);}?
import java.awt.AlphaComposite;import java.awt.Graphics2D;import java.awt.Image;import java.awt.RenderingHints;import java.awt.image.BufferedImage;import java.io.IOException;import java.io.OutputStream;import java.util.logging.Level;import java.util.logging.Logger;import javax.imageio.ImageIO;import javax.swing.ImageIcon;/** * * @author Administrator * LOGO水印 */public class IconImageWatermark implements IImageWatermark { private Integer degree; private Image image; private ImageIcon icon; private float alpha=1f; private OutputStream out; public IconImageWatermark(Image image, ImageIcon icon) { this(image, icon, null,1f); } public IconImageWatermark(Image image, ImageIcon icon, Integer degree,float alpha) { this.image = image; this.icon = icon; this.degree = degree; this.alpha=alpha; } @Override public void watermark() { if (this.image == null) { throw new NullPointerException("image is null"); } if (this.icon == null) { throw new NullPointerException("icon is null"); } int width=this.image.getWidth(null); int height=this.image.getHeight(null); //创建访问图像数据缓冲区 BufferedImage buffer=new BufferedImage(width, height,BufferedImage.TYPE_INT_BGR); //创建一个 Graphics2D 图表 Graphics2D grap=buffer.createGraphics(); // 设置对线段的锯齿状边缘处理 grap.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); //绘制指定图像 grap.drawImage(image.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null); //设置水印旋转 if(degree!=null){ grap.rotate(Math.toRadians(degree), Double.valueOf(width/2),Double.valueOf(height/2)); } //组合绘图,并设定透明 grap.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,this.alpha)); //绘制一个水印 int x=(width-icon.getImage().getWidth(null))/2; int y=(height-10)-icon.getImage().getHeight(null); grap.drawImage(icon.getImage(),x,y, null); //组合结束 grap.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); //释放此图形的上下文以及它使用的所有系统资源 grap.dispose(); try { ImageIO.write(buffer, "JPG", out); } catch (IOException ex) { Logger.getLogger(IconImageWatermark.class.getName()).log(Level.SEVERE, null, ex); } } @Override public void setOutputStream(OutputStream out) { this.out = out; } public float getAlpha() { return alpha; } public void setAlpha(float alpha) { this.alpha = alpha; } public Integer getDegree() { return degree; } public void setDegree(Integer degree) { this.degree = degree; } }??
import java.awt.AlphaComposite;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.Image;import java.awt.RenderingHints;import java.awt.image.BufferedImage;import java.io.IOException;import java.io.OutputStream;import java.util.logging.Level;import java.util.logging.Logger;import javax.imageio.ImageIO;/** * * @author Administrator */public class StringImageWatermark implements IImageWatermark { private Image image; private String water; private Integer degree; private float alpha; private OutputStream os; public StringImageWatermark(Image image, String water) { this(image, water, null, 1f); } public StringImageWatermark(Image image, String water, Integer degree, float alpha) { this.image = image; this.water = water; this.degree = degree; this.alpha = alpha; } @Override public void watermark() { if(image==null){ throw new NullPointerException("image is null"); } int width=image.getWidth(null); int height=image.getHeight(null); BufferedImage buffered=new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB); Graphics2D graph=buffered.createGraphics(); graph.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graph.drawImage(image.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0,0, null); if(degree!=null){ graph.rotate(degree, Double.valueOf(width/2),Double.valueOf(height/2)); } graph.setColor(Color.RED); graph.setFont(new Font("宋体",Font.BOLD,30)); graph.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); int x= (width-water.length()*30)/2; int y=height-30; graph.drawString(water,x,y); // graph.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); graph.dispose(); try { ImageIO.write(buffered, "jpg", os); } catch (IOException ex) { Logger.getLogger(StringImageWatermark.class.getName()).log(Level.SEVERE, null, ex); } } @Override public void setOutputStream(OutputStream out) { this.os=out; } public float getAlpha() { return alpha; } public void setAlpha(float alpha) { this.alpha = alpha; } public Integer getDegree() { return degree; } public void setDegree(Integer degree) { this.degree = degree; } }?
import java.io.IOException;import java.io.OutputStream;/** * * @author Administrator */public class WatermarkContext { private IImageWatermark iImageWatermark; public WatermarkContext(IImageWatermark iImageWatermark) { this.iImageWatermark = iImageWatermark; } public void watermark(OutputStream out) { this.iImageWatermark.setOutputStream(out); this.iImageWatermark.watermark(); if(out!=null){ try { out.close(); } catch (IOException ex) { ex.printStackTrace(); } } }}?
?
import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;import java.util.logging.Level;import java.util.logging.Logger;import javax.imageio.ImageIO;import javax.swing.ImageIcon;/** * * @author Administrator */public class Test { public static void main(String... args) { try { OutputStream os = new FileOutputStream("d:\\img_mark_icon.jpg"); OutputStream sos = new FileOutputStream("d:\\img_mark_string.jpg"); IconImageWatermark iiw=new IconImageWatermark(ImageIO.read(new File("d:\\img.jpg")), new ImageIcon("d:\\logo.jpg")); iiw.setAlpha(0.5f);// iiw.setDegree(-40); WatermarkContext context=new WatermarkContext(iiw); context.watermark(os); StringImageWatermark siw=new StringImageWatermark(ImageIO.read(new File("d:\\img.jpg")), "襄樊诚作房地产评估有限公司"); siw.setAlpha(0.5f); WatermarkContext scontext=new WatermarkContext(siw); scontext.watermark(sos); } catch (Exception ex) { Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); } }}?