0

I have the code from this example which is how it's supposed to work for all formats. It works great for png but not for jpg or bmp (gif does not have this setting).

I tried this code but it throws on

Element jfif = (Element)tree.getElementsByTagName("app0JFIF").item(0);

Any idea how to do this?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
David Thielen
  • 28,723
  • 34
  • 119
  • 193
  • have you checked this one: http://stackoverflow.com/questions/233504/write-dpi-metadata-to-a-jpeg-image-in-java ? – Erich Kitzmueller May 13 '17 at 13:51
  • It is quite possible that the JPEG stream you are looking at is not in JFIF format and has no JFIF APP0 marker. – user3344003 May 13 '17 at 15:48
  • @user3344003 - yes tried that. "tree.getElementsByTagName("app0JFIF")" returns null for me. – David Thielen May 15 '17 at 14:10
  • That's your problem then. It's not in JFIF format. – user3344003 May 15 '17 at 17:33
  • @DavidThielen Do you even use the native metadata (not the "plugin-neutral" one, that most of the code you refer to use)? – Harald K May 16 '17 at 11:50
  • @haraldK - I am using the code shown in the selected answer in http://stackoverflow.com/questions/321736/how-to-set-dpi-information-in-an-image – David Thielen May 16 '17 at 21:46
  • @DavidThielen But that makes no sense... That code *writes* metada, using the `javax_imageio_1.0` (["standard" or plugin neutral](https://docs.oracle.com/javase/7/docs/api/javax/imageio/metadata/doc-files/standard_metadata.html)) format. However, the code you have shown tries to *read* an element (`app0JFIF`) that is part of the [native JPEG](https://docs.oracle.com/javase/7/docs/api/javax/imageio/metadata/doc-files/jpeg_metadata.html) (`javax_imageio_jpeg_image_1.0`) format. – Harald K May 17 '17 at 12:26
  • I.e. It would help if you created an MCVE. – Harald K May 17 '17 at 12:38

1 Answers1

0

I had this problem and it took a lot of fixing - not helped by the error reporting differently in different versions of the JVM, as well as coming three steps after the actual problem (the BufferedImage) was last used in a call.

The problem is that the results depend on the type of the BufferedImage. Specifically if you switch from the png in the example to jpg, you can no longer use BufferedImage.TYPE_INT_RGB. Try BufferedImage.TYPE_3BYTE_BGR, which works for me with jpg.

2hi4u
  • 1