Joche
Joche

Reputation: 346

I need to convert a colored image to a bilevel version

In java I need to convert an incoming file in formats of jpeg, gif or png to a bilevel version for further conversion to a CCIT compressed TIF file. I am able to get the tif file out, but not with compression. I'm truly not an expert in either JAI, compression or image formats. Any help is much appreciated. (I have googled this topic for some hours now, but can't find an example I understand.)

    String inFile = "myFile.jpg"
    String newFileName = "myFile.tif"; 
    FileOutputStream out = new FileOutputStream(newFileName);

    RenderedOp src = JAI.create("fileload", inFile);

    // TIFFEncodeParam params = new TIFFEncodeParam();
    // params.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);

    TIFFImageEncoder encoder = new TIFFImageEncoder (out, null);
    encoder.encode (src);
    out.flush();
    out.close();

Upvotes: 2

Views: 3934

Answers (1)

amickj
amickj

Reputation: 138

You will have to convert your image to compression format that is bilevel in order to be able to write Tiff images using COMPRESSION_GROUP4. The easiest way to do that is to probably use a BufferedImage to render your image with a compatible compression scheme.

        FileOutputStream fos = new FileOutputStream("C:/test/output/myFile.tif"); 

        RenderedOp src = JAI.create("fileload", "C:/test/input/myFile.jpg");

        BufferedImage buf = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
        buf.getGraphics().drawImage(src.getAsBufferedImage(), 0, 0, null);
        RenderedImage ri = (RenderedImage) buf;

        encodeParam = new TIFFEncodeParam ();
        encodeParam.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);

        ImageEncoder enc = ImageCodec.createImageEncoder("TIFF", fos, encodeParam);
        enc.encode(ri);

Upvotes: 5

Related Questions