Write a .TIFF with JAI

一笑奈何 提交于 2020-01-15 09:01:05

问题


I'm a beginner in Java and a geomatics student. I'am using IntelliJ. I would like to create a TIFF from a BufferedImage. This is my code :

    byte[] buffer = new byte[width * height];
    ColorSpace cs = ColorSpace.getInstance( ColorSpace.CS_GRAY );
    int[] nBits = { 8 };
    ColorModel cm = new ComponentColorModel( cs, nBits, false, true,Transparency.OPAQUE, DataBuffer.TYPE_BYTE );
    SampleModel sm = cm.createCompatibleSampleModel( width, height );
    DataBufferByte db = new DataBufferByte( buffer, width * height );
    WritableRaster raster = Raster.createWritableRaster( sm, db, null);
    BufferedImage result = new BufferedImage( cm, raster, false , null );
    File outputfile = new File( "saved.png" );
    ImageIO.write( result, "png", outputfile );

A raster .png is create and it works well. But I want to create a .TIFF and ImageIO.write don't create TIFF (only png,bmp and jpeg). So I download the JAI (Java Advanced Imaging) here : http://download.java.net/media/jai/builds/release/1_1_3/ I upload it on my project and on Maven, but I don't know how to make a tiff simply... I try some snippets that I found on the internet but it don't work..

        TIFFEncodeParam params = new TIFFEncodeParam();
        FileOutputStream os = new FileOutputStream("PingsTiff.tiff");
        javax.media.jai.JAI.create("encode", result, os, "TIFF", params);

The "TIFFEncodeParam" and "media" is not recognized...and I'm a real noob at programming..

Thanks


回答1:


First of all, JAI comes with a set of ImageIO plugins, that will allow you to use ImageIO.write to write in TIFF format. But it requires the jai_imageio.jar to be on class path. I guess this is the JAR you are missing.

Also, the code you posted should work, if you have the imports and dependencies set up correctly. It's a little tricky because some parts of JAI requires native libraries that needs to be installed using the installer, and in the correct JRE, etc. Because of this, it's not a perfect fit with Maven (although certainly doable).

However, as you see from the download link in your question, JAI is a pretty much dead project (the latest update is from 2006).

Because of this lack of updates, bug fixes and support, along with the native parts and the license issues, I set up an open source project, aimed to provide at least as good file format support as JAI, with no native requirements and released under BSD license.

You can read about it, and the TIFF plugin in particular at the project home page. A little further down the page is down is download links, Maven dependency information etc.

When you have declared dependency on the TIFF plugin, you should be able to write your TIFF using plain ImageIO like this:

File outputfile = new File("saved.tif");

if (!ImageIO.write(result, "TIFF", outputfile)) {
    // Beware, write is a boolean method, that returns success!
    System.err.println("Could not write " + outputfile.getAbsolutePath() + " in TIFF format.");
}


来源:https://stackoverflow.com/questions/30320434/write-a-tiff-with-jai

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!