The Java Advanced Imaging toolkit contains lots of stuff to work with images. The documentation leaves a lot to be desired though: it is often factually incorrect due to version differences, and examples for common operations are hard to come by.
I needed a way to "resize" monochrome tiffs. The default "scale" operation leaves the result as a black-and-white image, and this results in a reduced image with a lousy quality. To get a better quality the image must be converted to grayscale and then rescaled using a resampler filter.
Most examples tell you to use either the "ColorConvert" or the "BandCombine" JAI operations. But sadly all of the examples I tried threw exceptions at one part or another.
Luckily though an operator exists which does exactly what we need: "subsampleBinaryToGray". To convert you use the following code:
File f = new File("blabla.tiff");
SeekableStream ss = new FileSeekableStream(f);
RenderedImage ri = null;
TIFFDecodeParam tp = null;
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", ss, tp);
ri = dec.decodeAsRenderedImage(1); // Get 1st tiff image
TiledImage tim = new TiledImage(ri, 512, 512);
ri = tim;
ParameterBlock pb = new ParameterBlock();
pb.addSource(ri);
pb.add(scale);
pb.add(scale);
ImageLayout la = new ImageLayout();
la.setTileWidth(512);
la.setTileHeight(512);
RenderingHints rhi = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, la);
RenderedImage sri = JAI.create("subsampleBinaryToGray", pb, rhi);
Comments
thanks for article i
scale value ? s = new
scale value ? s = new FileSeekableStream(file); ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param); int numofpages = dec.getNumPages(); for (int i = 0; i < numofpages; i++) { op = dec.decodeAsRenderedImage(i); TiledImage tim = new TiledImage(op, 512, 512); op = tim; ParameterBlock pb = new ParameterBlock(); pb.addSource(op); pb.add(512); // scale ? pb.add(512); // scale ? ImageLayout la = new ImageLayout(); la.setTileWidth(512); la.setTileHeight(512); RenderingHints rhi = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, la); RenderedImage sri = JAI.create("subsampleBinaryToGray", pb, rhi); JAI.create("filestore", sri, imageDir + simplefilename + "." + i + ".png", "png"); } :sick: :sick: :jawdrop: 8) :O :P :)
how did u guyz use dis code man?.
Thanks!
¡It worked really great! ¡Thanks!