开发者

How to write an 8 bit grey scale tiff with Java?

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开发者_JS百科");
        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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜