jai给tif格式的图片加水印

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import java.awt.image.renderable.RenderableImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.media.jai.JAI;
import javax.media.jai.ParameterBlockJAI;
import javax.media.jai.PlanarImage;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.JPEGDecodeParam;
import com.sun.media.jai.codec.JPEGEncodeParam;
import com.sun.media.jai.codec.TIFFDecodeParam;
import com.sun.media.jai.codec.TIFFEncodeParam;

public class ImageWaterMark {
 /**
 * 图片类型-JPEG
 */
 public final static int IMG_TYPE_JPEG = 0;

 /**
 * 图片类型-TIFF
 */
 public final static int IMG_TYPE_TIFF = 1;

 /**
 * 转换图片文件格式为TIFF
 * @param srcFile File 要转换格式的原文件
 * @param resFile File 转换格式后的目标文件
 * @param preType int 原文件的格式,目前只支持JPEG格式-0
 * @throws IOException 抛出IO异常
 */
 public static void convertImageToTIFF(File srcFile,File resFile,int preType) throws IOException {
 if(preType == IMG_TYPE_TIFF) {
 System.out.println("Needn't to convert!");
 return;
 }

 InputStream fis = new FileInputStream(srcFile);
 OutputStream fos = new FileOutputStream(resFile);

 TIFFEncodeParam encodeParam = new TIFFEncodeParam();
 encodeParam.setCompression(TIFFEncodeParam.COMPRESSION_DEFLATE);

 switch(preType) {
 case IMG_TYPE_JPEG:
 JPEGDecodeParam decodeParam = new JPEGDecodeParam();
 ImageDecoder decoder = ImageCodec.createImageDecoder("JPEG",fis,decodeParam);
 RenderedImage image = decoder.decodeAsRenderedImage();
 ImageEncoder encoder = ImageCodec.createImageEncoder("TIFF",fos,encodeParam);
 encoder.encode(image);
 break;
 default:
 break;
 }

 fos.close();
 fis.close();
 }

 /**
 * 把图片格式转换为JPEG格式
 * @param srcFile File 要转换的原文件
 * @param resFile File 转换后的目标文件
 * @param preType int 原文件的类型,目前只支持TIFF格式-1
 * @throws IOException 抛出IO异常
 */
 public static void convertImageToJPEG(File srcFile,File resFile,int preType) throws IOException {
 if(preType == IMG_TYPE_JPEG) {
 System.out.println("Needn't to convert!");
 return;
 }

 InputStream fis = new FileInputStream(srcFile);
 OutputStream fos = new FileOutputStream(resFile);

 JPEGEncodeParam encodeParam = new JPEGEncodeParam();

 switch(preType) {
 case IMG_TYPE_TIFF:
 TIFFDecodeParam decodeParam = new TIFFDecodeParam();
 decodeParam.setJPEGDecompressYCbCrToRGB(false);

 ImageDecoder decoder = ImageCodec.createImageDecoder("TIFF",fis,decodeParam);
 RenderedImage image = decoder.decodeAsRenderedImage();

 ImageEncoder encoder = ImageCodec.createImageEncoder("JPEG",fos,encodeParam);
 encoder.encode(image);
 break;
 default:
 break;
 }

 fos.close();
 fis.close();
 }

 /**
  * 添加图片水印
  *
  * @param file
  */
 public static void waterMark(File file) {
  try {
   Image srcImage = ImageIO.read(file);
   int width = srcImage.getWidth(null);
   int height = srcImage.getHeight(null);
   BufferedImage bImage = new BufferedImage(width, height,
     BufferedImage.TYPE_INT_RGB);
   Graphics2D gh = bImage.createGraphics();
   gh.drawImage(srcImage, 0, 0, width, height, null);
   File waterFile = new File("d://a.jpeg");
   Image waterImage = ImageIO.read(waterFile);
   int waterWidth = waterImage.getWidth(null);
   int waterHeight = waterImage.getHeight(null);
   gh.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
     0.5f));
   gh.drawImage(waterImage, (width - waterWidth) / 2,
     (height - waterHeight) / 2, waterWidth, waterHeight, null);
   gh.dispose();
   FileOutputStream fos = new FileOutputStream(file);
   JPEGImageEncoder jie = JPEGCodec.createJPEGEncoder(fos);
   jie.encode(bImage);
   fos.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 /**
  * 加字符串水印
  *
  * @param file
  * @param str
  */
 public static void waterMarkStr(File file, String str) {
  try {
   if (!file.exists()) {
    file.createNewFile();
   }
   // 创建图片流
   FileInputStream fis = new FileInputStream(file);
   // 创建一个Image对象并以原图像流进行填充
   Image image = ImageIO.read(fis);
   int width = image.getWidth(null);
   int height = image.getHeight(null);
   // 创建一个图像操作容器
   BufferedImage bImage = new BufferedImage(width, height,
     BufferedImage.TYPE_INT_RGB);
   // 创建绘画环境
   Graphics2D gh = bImage.createGraphics();
   gh.drawImage(image, 0, 0, width, height, null);
   // 设置字体
   gh.setFont(new Font("宋体", Font.BOLD, 28));
   // 设置颜色
   gh.setColor(Color.RED);
   // 设置透明度
   gh.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
     0.5f));
   // 向BufferedImage写入字串,设置水印的位置
   gh.drawString(str, width / 3, height / 3);
   // 使更改生效
   gh.dispose();
   // 创建文件输出对象
   FileOutputStream fos = new FileOutputStream(file);
   // 创建按JPEG编码对象
   JPEGImageEncoder jie = JPEGCodec.createJPEGEncoder(fos);
   // 对BufferedImage进行JPEG编码
   jie.encode(bImage);
   // 关闭输出流
   fos.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public  static boolean  smallpicture(String   src,String   dest,int   w,int   h)
    {
        try
        {
            ParameterBlockJAI   loadPB   =   new   ParameterBlockJAI("fileload");
            loadPB.setParameter("d.jpeg",src);
            PlanarImage   source   =   JAI.create( "fileload",loadPB);
            int   orinWidth   =   source.getWidth();
            int   orinHeigth   =   source.getHeight();

            int   newHeigth   =   orinHeigth   *   w/orinWidth;
            ParameterBlock   pb   =   new   ParameterBlock();
            pb.addSource(source);
            RenderableImage   ren   =   JAI.createRenderable( "renderable ",pb);
            PlanarImage   dst   =   (PlanarImage)ren.createScaledRendering(w,newHeigth,null);
            File   fout   =   new   File(dest);
            FileOutputStream   out   =   new   FileOutputStream(fout);
            JPEGEncodeParam   param   =   new   JPEGEncodeParam();
            param.setQuality(0.70f);//设置图片质量
            //这里你可以选择不同的图片编码,不过gif因为版权问题要压缩成png
            ImageEncoder   encoder   =   ImageCodec.createImageEncoder( "JPEG ",out,param);
            encoder.encode(dst);
            out.close();
            return   true;
        }
        catch   (Exception   e)
        {
            e.printStackTrace();
            return   false;
        }
    }


 public static void main(String[] args) {
 try {
  //1.读取tif格式的文件,将文件转换为jpeg格式
  File srcFile = new File("d://a.tif");
  File recfile = new File("d://a.jpeg");
  File file = new File("d://c.tif");
  if (!srcFile.exists()) {
   srcFile.createNewFile();
    }
  if(!recfile.exists()){
   recfile.createNewFile();
  }
  convertImageToJPEG(srcFile,recfile,IMG_TYPE_TIFF);


  //2.给转换的jpeg文件加水印
  waterMarkStr(recfile,"添加成功123");


  //3.将加过水印后的jpeg文件转换为tif格式的
  convertImageToTIFF(recfile,file,IMG_TYPE_JPEG);

  //4.将生成的临时的jpeg文件删掉
//  recfile.delete();


  } catch (IOException e) {
   e.printStackTrace();
 }


 /* try {
   File file = new File("d://a.jpg");
   if (!file.exists()) {
    file.createNewFile();
   }
   ImageWaterMark.waterMarkStr(file, "你好  2011-000051-15");
 //  ImageWaterMark.waterMark(file);
  } catch (IOException e) {
   e.printStackTrace();
  }*/
 }
}

转自:http://blog.csdn.net/hhxinghuo/article/details/6336282