6

I am trying to convert TIF / TIFF images to JPG which works fine but for few TIF images I am getting an IllegalArgumentException: Bad endianness tag (not 0x4949 or 0x4d4d).

Exception :

java.io.IOException: Bad endianness tag (not 0x4949 or 0x4d4d).
    at com.sun.media.jai.codecimpl.CodecUtils.toIOException(CodecUtils.java:76)
    at com.sun.media.jai.codecimpl.TIFFImageDecoder.getNumPages(TIFFImageDecoder.java:98)
    at com.sun.media.jai.codecimpl.TIFFImageDecoder.decodeAsRenderedImage(TIFFImageDecoder.java:103)
    at com.sun.media.jai.codec.ImageDecoderImpl.decodeAsRenderedImage(ImageDecoderImpl.java:140)
    at com.pkg.jae.utils.GenericImageUtils.convertTiffToJpg(GenericImageUtils.java:35)
    at com.pkg.jae.utils.GenericImageUtils.main(GenericImageUtils.java:92)
Caused by: java.lang.IllegalArgumentException: Bad endianness tag (not 0x4949 or 0x4d4d).
    at com.sun.media.jai.codec.TIFFDirectory.getNumDirectories(TIFFDirectory.java:595)
    at com.sun.media.jai.codecimpl.TIFFImageDecoder.getNumPages(TIFFImageDecoder.java:96)
    ... 4 more

Code Function :

public static void convertTiffToJpg(String strTiffUrl,String strJpgFileDestinationUrl) throws Exception {
        try {
            FileSeekableStream obj_FileSeekableStream = new FileSeekableStream(new File(strTiffUrl));
            ImageDecoder obj_ImageDecoder = ImageCodec.createImageDecoder(EXT_TIFFX, obj_FileSeekableStream, null);
            RenderedImage obj_RenderedImage = obj_ImageDecoder.decodeAsRenderedImage();
            JAI.create("filestore", obj_RenderedImage,strJpgFileDestinationUrl, EXT_JEPGX);
            obj_RenderedImage = null;
            obj_ImageDecoder = null;
            obj_FileSeekableStream.close();
        } catch (Exception ex) {
            throw ex;
        }
    }

If anyone knows the issue and can help in this.

Matthieu
  • 2,736
  • 4
  • 57
  • 87
Shreyas Dave
  • 3,815
  • 3
  • 28
  • 57

3 Answers3

0

As stated in a comment by bitbank, this means you're passing a JPEG file to it when it expects to get a TIFF file.

Community
  • 1
  • 1
Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
0

Startlingly, this JAI

RenderedOp renderer = JAI.create("fileload", filename);
BufferedImage bi = renderer.getAsBufferedImage();

does not have the same failure and just works regardless of image "kind." Don't use this particular method (passing in the filename) though, see Is JAI closing file handles too early?

Community
  • 1
  • 1
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
0

I had this issue and it turned out to be a front-end problem. Yes, I was trying to upload the wrong file type, but I was expecting correct handling and a gracious popup message alert. Instead I was getting the error you described.

In my case, I was using extjs and I had a failure function like this:

failure: function (a) {
...some message alert...
}

instead of:

failure: function (f, a) {
...some message alert...
}

and this was throwing that exception, instead of displaying my message alert.

João Matos
  • 6,102
  • 5
  • 41
  • 76