Converting to Tiff on Mac using Java Advanced Imaging

Posted on Posted in Custom Programming, Java

While there are a number of resources on the net talking about converting images to tiff in Java using JAI, I wasn’t able to come across one that works on a Mac. JAI is a separate download from the JDK and unfortunately, there are no mac version of JAI that can be downloaded from the Sun site.

Upon reviewing the jar files included in OSX, I found jai_codec.jar and jai_core.jar in my libraries. So, if you have those libraries, you should be able to get JAI to work and convert your images to tiff.

Here’s the code that I’ve successfully used to convert any image to tiff.

public static void convertToTiff(String inputFile, String outputFile) {
	try {
		TIFFEncodeParam param = new TIFFEncodeParam();
		param.setLittleEndian(false); // Intel

		File outFile = new File(outputFile);
		OutputStream ios = new BufferedOutputStream(
	new FileOutputStream(outFile));
    		ImageEncoder enc = ImageCodec.createImageEncoder("tiff", ios, param);
		RenderedOp src = JAI.create("fileload", inputFile);

	      ColorConvertOp filterObj = new ColorConvertOp(
	    //Apply the color filter and return the result.
	    BufferedImage dst = new

	    // save the output file
	} catch (Exception e) {
		_log.error("Failed to create output tiff file.",e);
		throw new SystemException("Failed to create output tiff file.",e);

The example above uses no compression and converts the image to 24bits. You may change the parameters as needed.

One thought on “Converting to Tiff on Mac using Java Advanced Imaging

  1. Hi Alan, first i would like to thank you for the code. I am trying to implement it for converting txt file to tiff image. it worked fine but unfortunately the content of the txt file is not copied over to the tiff image.
    i see the following error when i try to run this..
    if you can advise me what to do , it will be great.
    thanks in adv

    Exception in thread “main” java.lang.ExceptionInInitializerError
    at ConvertToTiffImage.convertToTiff(
    at ConvertToTiffImage.main(
    Caused by: java.lang.SecurityException: sealing violation: package is sealed
    at Source)
    at$000(Unknown Source)
    at$ Source)
    at Method)
    at Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.