jai 读取tif页数

用的sun的jai东西,保留为方便查询与使用。

 

import java.io.File;
import java.io.IOException;

import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.SeekableStream;
import com.sun.media.jai.codec.TIFFDecodeParam;
import com.sun.media.jai.codec.TIFFDirectory;

public class TifTest {
	public static void readTifFIle(String fileName) {
		File file = new File(fileName);
		SeekableStream s = null;
		TIFFDecodeParam param = null;
		try {
			s = new FileSeekableStream(file);
			ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
			int numofpages;
			numofpages = dec.getNumPages();
			System.out.print(numofpages);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
	//读取文件的 页数
	public static void readTifFIle2(String fileName){
		File file = new File(fileName);
		SeekableStream s = null;
		try {
			s = new FileSeekableStream(file);
			System.out.println(TIFFDirectory.getNumDirectories(s));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		String fileName = "C:\\婚姻证明.tif";
		readTifFIle2(fileName);
	}
	
/*	public static int DISPLAY_WIDTH = 300;

	public static boolean doTiff2JPEG (String filename, String imageDir) {
        File file = new File(imageDir , filename);
        SeekableStream s = null;
        TIFFDecodeParam param = null;
        RenderedImage op = null;
        String simplefilename = filename.substring(0,filename.lastIndexOf("."));
try {
          s = new FileSeekableStream(file);
          ImageDecoder dec = (ImageDecoder) ImageCodec.createImageDecoder("tiff", s, param);
int numofpages = dec.getNumPages();
for (int i=0; i < numofpages; i++) {
            op = new NullOpImage
               (dec.decodeAsRenderedImage(i),null,null,OpImage.OP_COMPUTE_BOUND);
int width = op.getWidth();
int height = op.getHeight();
double conversionFactor = (double)DISPLAY_WIDTH / (double)width;
int thumbHeight = (int)((double)height * conversionFactor);
int thumbWidth = (int)((double)width * conversionFactor);
            Dimension dim = new Dimension(thumbHeight, thumbWidth);       
            JAI.setDefaultRenderingSize(dim);
            JAI.setDefaultTileSize(dim);
            JAI.create("filestore",
                    op, imageDir <i>File.separator</i> simplefilename + "." + i + ".jpg", "png");
}
} catch (IOException e) {
          System.out.println(e.getLocalizedMessage());
return false;
}
return true;
}

	public static void main(String[] args) {
		TifTest.doTiff2JPEG("aa.tiff", "d:temp");
	}*/
}
 

 

你可能感兴趣的:(读取)