用JAI实现对TIF(TIFF)格式图片的合并

用JAI实现对TIF(TIFF)格式图片的合并

 

方法一:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;

import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.TIFFEncodeParam;

public class MergeTif
{
   public static void main(String[] args) throws IOException
   {
      try
      {
         File[] files = new File[2];
     //需要进行合并的tif图片.(均为单页)
         files[0] = new File("d:\\aa.tif");  
         files[1] = new File("d:\\bb.tif");  
         ArrayList pages = new ArrayList(files.length - 1);
         RenderedOp firstPage =
            JAI.create("fileload", files[0].getCanonicalPath());
         for (int i = 1; i < files.length; i++)
         {
            RenderedOp page =
               JAI.create("fileload", files[i].getCanonicalPath());
            pages.add(page);
         }
         TIFFEncodeParam param = new TIFFEncodeParam();
     //合并后生成的图片
         OutputStream out = new FileOutputStream("d:\\cc.tif");
         ImageEncoder encoder =
            ImageCodec.createImageEncoder("TIFF", out, param);
         param.setExtraImages(pages.iterator());
         encoder.encode(firstPage);
         firstPage.dispose();
         for (int i = 0; i < pages.size(); i++)
         {
            ((RenderedOp)pages.get(i)).dispose();
         }
         out.close();
      }
      catch (Exception e)
      {
         System.out.println(e.toString());
      }
   }
}
方法二:

import java.awt.image.renderable.ParameterBlock;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;

import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.TIFFEncodeParam;

public class MergeTif
{
   public static void main(String[] args) throws IOException
   {  
 File[] files = new File[2];
 //需要进行合并的tif图片.(均为单页)
 files[0] = new File("d:\\aa.tif");
 files[1] = new File("d:\\bb.tif");

 ArrayList pages = new ArrayList(files.length - 1);
 FileSeekableStream []stream ={ new FileSeekableStream(files[0].getCanonicalPath()),
      new FileSeekableStream(files[1].getCanonicalPath())};
 ParameterBlock pb = (new ParameterBlock());
 PlanarImage firstPage=JAI.create("stream", stream[0]);

 for (int i = 1; i < files.length; i++)
 {
  PlanarImage page=JAI.create("stream", stream[i]);
  pages.add(page);
 }
 TIFFEncodeParam param = new TIFFEncodeParam();
 //合并后生成的图片
 OutputStream os = new FileOutputStream("d:\\cc.tif");

 ImageEncoder enc = ImageCodec.createImageEncoder("tiff", os, param);
 param.setExtraImages(pages.iterator());
  
 enc.encode(firstPage);
 for(int i=0; i<files.length;i++)
 { 
  stream[i].close();
 }
 os.close();
   }
}

你可能感兴趣的:(java,职场,格式,休闲,tif,把多个jpg合并成tif,JAI)