用JAI编写.TIFF [英] Write a .TIFF with JAI

查看:318
本文介绍了用JAI编写.TIFF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的初学者和地理专业的学生. 我正在使用IntelliJ. 我想从BufferedImage创建TIFF. 这是我的代码:

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 );

已创建一个栅格.png,并且效果很好.但是我想创建一个.TIFF和ImageIO.write不要创建TIFF(仅png,bmp和jpeg).因此,我在这里下载了JAI(Java高级映像): http://download. java.net/media/jai/builds/release/1_1_3/ 我将其上传到我的项目和Maven中,但是我不知道如何简单地做一个tiff ...我尝试了一些我在Internet上找到的片段,但是它不起作用.

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);

无法识别"TIFFEncodeParam"和媒体" ...而且我在编程时是一个真正的菜鸟.

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

谢谢

推荐答案

首先,JAI附带了一组ImageIO插件,使您可以使用ImageIO.write以TIFF格式编写.但是它要求jai_imageio.jar在类路径上.我想这就是您所缺少的JAR.

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.

此外,如果正确设置了导入和依赖项,则发布的代码应该可以使用.这有点棘手,因为JAI的某些部分需要使用安装程序以及正确的JRE等安装的本机库.因此,它与Maven并不是完美的结合(尽管肯定是可行的).

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).

但是,正如您从问题的下载链接中看到的那样,JAI是一个非常无效的项目(最新更新来自2006年).

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

由于缺少更新,错误修复和支持以及本机部件和许可证问题,我建立了一个开源项目,旨在提供至少与JAI一样好的文件格式支持,而没有本机要求并根据BSD许可发布.

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.

您可以阅读有关它的内容,以及 TIFF插件,尤其是在项目主页上.再往下一点就是下载链接,Maven依赖项信息等.

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.

声明对TIFF插件的依赖性后,应该可以使用普通的ImageIO编写TIFF,如下所示:

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.");
}

这篇关于用JAI编写.TIFF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆