多页单个tif文件转换为多个jpg文件

import java.io.*;

import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.TIFFEncodeParam;
import com.sun.media.jai.codec.TIFFDecodeParam;
import com.sun.media.jai.codec.JPEGEncodeParam;
 
import java.awt.image.RenderedImage;
import javax.media.jai.RenderedOp;
import javax.media.jai.JAI;
import java.awt.image.renderable.ParameterBlock;

public class MultiPageRead {

    public static void main(String[] args) throws IOException {
        new MultiPageRead().doitJAI();
    }
 
    public void  doitJAI() throws IOException {
        FileSeekableStream ss = new FileSeekableStream("./zhaoming.tif");
        TIFFDecodeParam param0 = null;
        TIFFEncodeParam param = new TIFFEncodeParam();
        JPEGEncodeParam param1 = new JPEGEncodeParam();
        ImageDecoder dec = ImageCodec.createImageDecoder("tiff", ss, param0);
        int count = dec.getNumPages();
        param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
        param.setLittleEndian(false); // Intel
        System.out.println("This TIF has " + count + " image(s)");
        for (int i = 0; i < count; i++) {
            RenderedImage page = dec.decodeAsRenderedImage(i);
            File f = new File("./fk_" + i + ".jpg");
            System.out.println("Saving " + f.getCanonicalPath());
            ParameterBlock pb = new ParameterBlock();
            pb.addSource(page);
            pb.add(f.toString());
            pb.add("JPEG");
            pb.add(param1);
            //JAI.create("filestore",pb);
            RenderedOp r = JAI.create("filestore",pb);
            r.dispose();
           
            //RenderedOp op = JAI.create("filestore", page, "./zhaoming_" + i + ".jpg", "JPEG", param1);
        }
    }
}

你可能感兴趣的:(jpg)