Question

I'm trying to save a byte[] of raw image data to a .tiff file on my harddrive with the help of the Java Advanced Imaging API. There are some minor examples on the web but they didn't help me really because they don't match my exact problem.

I already managed to save the data from the byte[] to a .raw and can view the result with success so the source actually contains some data. The method in question is executable without any exceptions but all I get is a black image whenever I view it with the Windows XP ImageViewer.

This is my method:

public void saveTif(byte[] imgData) {
    BufferedImage bufferedImage = new BufferedImage( 1280, 1024, BufferedImage.TYPE_BYTE_GRAY );
    bufferedImage.createGraphics().drawBytes(imgData, 0, 1280*1024, 0, 0);

    TIFFEncodeParam params = new TIFFEncodeParam();
    params.setCompression( TIFFEncodeParam.COMPRESSION_NONE );
    String filenametiff = path + File.separatorChar + new SimpleDateFormat("dd_MM_yyyy_HH_mm_ss'.tiff'").format(new Date());
    JAI.create("filestore", bufferedImage, filenametiff, "TIFF", params);
}
Was it helpful?

Solution

Graphics.drawBytes() is used for drawing text (and it's not very good for that, as it uses byte[], rather than char[] or String, thus not supporting charsets) onto the image.

Graphics.drawBytes() can't be used to draw raw pixels.

To get a correct representation of your image, you should rather create a new BufferedImage from your pixel data, or create a matching image and set the data onto it. Have a look at the Raster and DataBuffer classes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top