用JAI实现TIF(TIFF)格式图片的裁剪
精选
转载
/*
Java的图像处理能力不断地在增长,但是将一个如PNG或者JPEG格式的文件写入磁盘或从磁盘读取这样的操作仍然比较烦琐。而现时相对简单的一种方法是是使用Java Advanced Imaging(JAI)API。以下是使用JAI进行TIF(TIFF)图片裁剪的一种方法,希望对大家有所帮助:
*/
import java.awt.p_w_picpath.renderable.ParameterBlock;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
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 CropTifPic {
public static void main(String[] args) throws IOException {
float x = 0f;
float y = 0f;
FileSeekableStream stream = new FileSeekableStream("D:\\srcPic.tif");
ParameterBlock pb = (new ParameterBlock());
PlanarImage in = JAI.create("stream", stream);
pb.addSource(in);
pb.add(x);
pb.add((float) (in.getHeight() / 3));
pb.add((float) in.getWidth());
pb.add((float) ((in.getHeight()) * 2 / 3));
PlanarImage out = JAI.create("crop", pb, null);
OutputStream os = new FileOutputStream("D:\\destPic.tif");
TIFFEncodeParam param = new TIFFEncodeParam();
ImageEncoder enc = ImageCodec.createImageEncoder("tiff", os, param);
enc.encode(out);
stream.close();
os.close();
}
}