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.
The post Converting to Tiff on Mac using Java Advanced Imaging appeared first on Ideyatech - Java Development Outsourcing Philippines.