0

I'm trying to write a tiff image file out using ImageIo, but even though I'm following the recommended guidelines and other tips, it isn't writing. There's no error message given. It just doesn't write.

Here is the code:

 BufferedImage bImageFromConvert = new BufferedImage(dimWidth, dimHeight * 2, BufferedImage.TYPE_BYTE_GRAY);//dimWidth and dimHeight are set elsewhere in the program. For now, assume they are equal to 100
 byte[] bufferHolder = ((DataBufferByte) bImageFromConvert.getRaster().getDataBuffer()).getData();
 System.arraycopy(imageInByte, 0, bufferHolder, 0, imageInByte.length); //imageInByte is the byte array containing all of my tiff image data.
 ImageIO.write(bImageFromConvert, "TIFF", new File(
                    "C:\\out.tif"));

I need the file extension to be "tif" and not "tiff" because I'm working with JP2K (which only recognizes the former extension).

Thank you!

Sarah
  • 145
  • 1
  • 14
  • what does it mean "it just doesn't write"? Does it fail, or does it succeed but there is no file? What does it return? If it returns false, it means that your "TIFF" format is wrong, try "tiff". – MK. Feb 09 '17 at 21:17
  • @MK. I've tried "tiff" before and get the same result. What I mean by "it just doesn't write" is that the operation completes, no errors are thrown, but there is no file. – Sarah Feb 09 '17 at 21:20
  • return value of the method, what is it. – MK. Feb 09 '17 at 21:21
  • and what does ImageReaderWriterSpi.getFormatNames() return. – MK. Feb 09 '17 at 21:22
  • @MK. You mean the method the code is encased in? It returns a file. "return new File("C:\\out.tif")" – Sarah Feb 09 '17 at 21:23
  • @MK. I can't get ImageReaderWriterSpi.getFormatNames() to return anything because I can't use it in a static context like that. – Sarah Feb 09 '17 at 21:27
  • no i mean method ImageIO.write returns a value. You are ignoring it currently. You should do boolean result = ImageIO.write(..) and see what it returns. I pasted the wrong thing -- print out what ImageIO. getFormatNames() returns. – MK. Feb 09 '17 at 21:34
  • @MK. While I don't see a function named getFormatNames() attached to ImageIO, the boolean value is false. – Sarah Feb 09 '17 at 21:40
  • 1
    OK, so false confirms that you are not specifying a supported format name. Check the output of https://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageIO.html#getReaderFormatNames() and see which ones are supported. – MK. Feb 09 '17 at 22:08
  • I'm not sure that TIFF is supoorted. It is/was a proprietary (Compuserve) format and not widely supported in libraries for that reason. But times could have changed. Check @MK.'s link. – user207421 Feb 09 '17 at 22:12
  • @EJP you are confusing TIFF and GIF. TIFF is a very complex flexible container format which is somewhat standardized and widely supported. The documentation (linked) talks about "tiff" so I'm guessing it is most likely supported. – MK. Feb 09 '17 at 23:32
  • 1
    @Sarah I closed this as a duplicate, as you need extra plugins to write TIFF with ImageIO before Java 9. As mentioned in the linked answer, you can use TwelveMonkeys ImageIO, JAI ImageIO (or any other plugin you like). Note that the format name is always TIFF, while the file extension can be ".tif". ImageIO only cares about the format name. – Harald K Feb 10 '17 at 08:41

0 Answers0