如何用Java编写8位灰度tiff?

发布于 2024-12-07 22:30:15 字数 1929 浏览 1 评论 0原文

我正在尝试使用 Sanselan 将灰度图像写入 TIFF 文件。显然希望将数据保存为 8 位灰度文件,但不知何故我总是得到 24 位彩色文件。

我搜索了一般文件格式,特别是 ExifTagConstants.EXIF_TAG_PIXEL_FORMAT,但找不到任何有用的内容。

我也考虑过颜色配置文件,但似乎没有灰度图像。但他们都有古怪的名字——我可能只是忽略了正确的名字。

或者我必须使用不同的库?

这是我使用的代码(没有业务逻辑和实验性的东西:

    Test_Only_Sanselan:
    try
    {
        final Map <String, Object> parameter = new HashMap <>();
        parameter.put(org.apache.sanselan.SanselanConstants.PARAM_KEY_COMPRESSION,
            org.apache.sanselan.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_UNCOMPRESSED);

        final java.io.OutputStream output = new java.io.FileOutputStream("Test-1.tiff");
        org.apache.sanselan.Sanselan.writeImage(image, output,
            org.apache.sanselan.ImageFormat.IMAGE_FORMAT_TIFF, parameter);
        output.close();
    }
    catch (final IOException exception)
    {
        LdfImage.log.info("! Could not create tiff image.", exception);
    }
    catch (final org.apache.sanselan.ImageWriteException exception)
    {
        LdfImage.log.info("! Could not create tiff image.", exception);
    }

我想知道是否需要添加一个特殊的参数。但是我还没有找到有用的参数。

以下是对Java 7图像编写器的测试,它创建了正确的灰度缩放图像,但为 PNG 而不是 TIFF:

    Test_Only_Java_7:
    try
    {
        final java.util.Iterator imageWriters = javax.imageio.ImageIO.getImageWritersByMIMEType ("image/png");
        final javax.imageio.ImageWriter imageWriter = (javax.imageio.ImageWriter) imageWriters.next();
        final java.io.File file = new java.io.File("Test-2.png");
        final javax.imageio.stream.ImageOutputStream output = javax.imageio.ImageIO.createImageOutputStream(file);

        imageWriter.setOutput(output);
        imageWriter.write(image);
    }
    catch (final IOException exception)
    {
        LdfImage.log.info("! Could not create tiff image.", exception);
    }

是否有用于 imageio 的 TIFF 插件?

I am trying to write a grey scale image to an TIFF file using Sanselan. Obviously would like the save the data to be 8 bit grey scale file but somehow I always end up with a 24 bit colour file.

I have searched for file formats in general and ExifTagConstants.EXIF_TAG_PIXEL_FORMAT in particular but was unable to find anything helpful.

I also considered colour profiles but there seem to be none for grey scale images. But then they all have wacky names — I might just have overlooked the right one.

Or do I have to use a different library?

Here the code I use (without business logic and experimental stuff:

    Test_Only_Sanselan:
    try
    {
        final Map <String, Object> parameter = new HashMap <>();
        parameter.put(org.apache.sanselan.SanselanConstants.PARAM_KEY_COMPRESSION,
            org.apache.sanselan.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_UNCOMPRESSED);

        final java.io.OutputStream output = new java.io.FileOutputStream("Test-1.tiff");
        org.apache.sanselan.Sanselan.writeImage(image, output,
            org.apache.sanselan.ImageFormat.IMAGE_FORMAT_TIFF, parameter);
        output.close();
    }
    catch (final IOException exception)
    {
        LdfImage.log.info("! Could not create tiff image.", exception);
    }
    catch (final org.apache.sanselan.ImageWriteException exception)
    {
        LdfImage.log.info("! Could not create tiff image.", exception);
    }

I wonder if I need to add a special parameter. But I have not found a useful parameter yet.

The following is a test of the Java 7 image writer which creates a correct grey scale image. But as PNG and not TIFF:

    Test_Only_Java_7:
    try
    {
        final java.util.Iterator imageWriters = javax.imageio.ImageIO.getImageWritersByMIMEType ("image/png");
        final javax.imageio.ImageWriter imageWriter = (javax.imageio.ImageWriter) imageWriters.next();
        final java.io.File file = new java.io.File("Test-2.png");
        final javax.imageio.stream.ImageOutputStream output = javax.imageio.ImageIO.createImageOutputStream(file);

        imageWriter.setOutput(output);
        imageWriter.write(image);
    }
    catch (final IOException exception)
    {
        LdfImage.log.info("! Could not create tiff image.", exception);
    }

Is there a TIFF pug-in for imageio?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃颗糖壮壮胆 2024-12-14 22:30:15

我不熟悉 Sanselan,但我看到有一个 ImageInfo.COLOR_TYPE_GRAYSCALE 常量,我猜它可以在提供给 params 参数映射中使用写图像

我没有找到 Sanselan 的 javadoc,他们的 链接 已损坏,您还有一个最新的吗?

如果您想使用 JAI,您可以在这里获得灵感:

void get8bitImage(byte[] data, int width, int height) {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
    byte[] rasterData = ((DataBufferByte)image.getRaster().getDataBuffer()).getData();
    System.arraycopy(pixels, 0, rasterData, 0, pixels.length); // A LOT faster than 'setData()'
}

void main(String[] argv) {
    TIFFEncodeParam ep = new TIFFEncodeParam();
    FileOutputStream out = new FileOutputStream("c:\\test.tiff");
    BufferedImage img = get8bitImage(myData);
    ImageEncoder encoder = ImageCodec.createImageEncoder("tiff", out, ep);
    encoder.encode(img);
}

确保 BufferedImageBufferedImage.TYPE_BYTE_GRAY 作为 imageType

I'm not familiar with Sanselan but I see there is a ImageInfo.COLOR_TYPE_GRAYSCALE constant, I'm guessing it could be used in the params parameter map given to writeImage.

I didn't find the javadoc for Sanselan, their link is broken, would you have one more up-to-date?

If you want to use JAI you can get your inspiration here:

void get8bitImage(byte[] data, int width, int height) {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
    byte[] rasterData = ((DataBufferByte)image.getRaster().getDataBuffer()).getData();
    System.arraycopy(pixels, 0, rasterData, 0, pixels.length); // A LOT faster than 'setData()'
}

void main(String[] argv) {
    TIFFEncodeParam ep = new TIFFEncodeParam();
    FileOutputStream out = new FileOutputStream("c:\\test.tiff");
    BufferedImage img = get8bitImage(myData);
    ImageEncoder encoder = ImageCodec.createImageEncoder("tiff", out, ep);
    encoder.encode(img);
}

Be sure that the BufferedImage has BufferedImage.TYPE_BYTE_GRAY as imageType.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文