java实现水印

package com.wawagame;
import java.awt.AlphaComposite; 
import java.awt.Font; 
import java.awt.Graphics2D; 
import java.awt.RenderingHints; 
import java.awt.geom.Rectangle2D; 
import java.awt.image.BufferedImage; 
import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
  
import javax.imageio.ImageIO; 

  
import com.sun.image.codec.jpeg.JPEGCodec; 
import com.sun.image.codec.jpeg.JPEGEncodeParam; 
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class WaterMarkerImageUtil {
/**
      * 保存JPEG图片
      * @param image 二进制图片对象
      * @param imageFile 待保存的文件名称
      * @param quality 待保存的图片质量
      * @throws FileNotFoundException
      * @throws IOException
      */ 
     public static void saveJPEGImage(BufferedImage image,File imageFile, 
             int quality) throws FileNotFoundException,IOException{ 
         BufferedOutputStream output = null; 
         output = new BufferedOutputStream( 
                 new FileOutputStream(imageFile));
         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output); 
         JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image); 
         param.setQuality(quality/100F, false); 
         encoder.setJPEGEncodeParam(param); 
         encoder.encode(image); 
         if(output != null){ 
             output.close(); 
         }
     } 
      
     /**
      * 给图片加上水印文字
      * @param imageFile 待处理的图片
      * @param mark 待添加的水印文字
      * @throws FileNotFoundException
      * @throws IOException
      */ 
     public static void waterMarkImage(File imageFile,String mark) 
         throws FileNotFoundException,IOException{ 
         //读取图片 
         BufferedInputStream input = new BufferedInputStream( 
                 new FileInputStream(imageFile)); 
         BufferedImage image = ImageIO.read(input); 
          
         //给图片加水印效果  z
         Graphics2D g2d = (Graphics2D) image.getGraphics(); 
         int width = image.getWidth(); 
         int height = image.getHeight(); 
         Font font = new Font("black",Font.BOLD,10);
         Rectangle2D rectangle = font.getStringBounds(mark, g2d.getFontRenderContext()); 
//          if(width < rectangle.getWidth() + 6 || height < rectangle.getHeight() + 20) 
//              return; 
         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
         g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,(float)0.8)); 
         int x = width - (int)rectangle.getWidth() - 5; 
         int y = height - 10; 
         g2d.setFont(font); 
         g2d.drawString(mark, x, y); 
          
         //保存图片 
         String fileName = imageFile.getName(); 
         int dotPosition = fileName.lastIndexOf("."); 
         String newFile = fileName.substring(0, dotPosition) + "_waterMark" + fileName.substring(dotPosition);
         System.out.println("x="+x+"y:"+y);
         saveJPEGImage(image, new File(newFile), 100); 
     } 
      
     public static void main(String[] args) { 
         try { 
             File file = new File("c:\\a.jpg"); 
             waterMarkImage(file,"Ujiang.com");
         }
         catch (FileNotFoundException e) { 
             e.printStackTrace(); 
         }  
         catch (IOException e) { 
             e.printStackTrace(); 
         } 
     } 
}

你可能感兴趣的:(java,sun)