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.setCompression(TIFFEncodeParam.COMPRESSION_NONE);
		param.setLittleEndian(false); // Intel

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

	      ColorConvertOp filterObj = new ColorConvertOp(
	            ColorSpace.getInstance(ColorSpace.CS_sRGB),null);
	    //Apply the color filter and return the result.
	    BufferedImage dst = new
BufferedImage(src.getWidth(),src.getHeight(),BufferedImage.TYPE_3BYTE_BGR);
	    filterObj.filter(src.getAsBufferedImage(),dst);

	    // save the output file
		enc.encode(dst);
		ios.close();
	} 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 com.sun.media.jai.imageioimpl.ImageReadWriteSpi.updateRegistry(ImageReadWriteSpi.java:83)
    at javax.media.jai.OperationRegistry.registerServices(OperationRegistry.java:2056)
    at javax.media.jai.ThreadSafeOperationRegistry.registerServices(ThreadSafeOperationRegistry.java:620)
    at javax.media.jai.OperationRegistry.initializeRegistry(OperationRegistry.java:373)
    at javax.media.jai.JAI.(JAI.java:566)
    at ConvertToTiffImage.convertToTiff(ConvertToTiffImage.java:36)
    at ConvertToTiffImage.main(ConvertToTiffImage.java:25)
    Caused by: java.lang.SecurityException: sealing violation: package com.sun.media.jai.operator is sealed
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$000(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown 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)
    at com.sun.media.jai.operator.ImageReadDescriptor.(ImageReadDescriptor.java:645)

Leave a Reply

Your email address will not be published.

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