1

I have an inputstream that contains image. Image can be as in jpeg, so in jpeg2000 format. I've logged that stream and see next for jpeg2000:

������jP

for jpeg that it something like

������JFIF

To my understanding, there should be some magic bytes in stream, that will return image type. Any ideas how to get them? That should be compatible for Android

Vi Matviichuk
  • 1,122
  • 15
  • 32
  • check this http://stackoverflow.com/questions/51438/getting-a-files-mime-type-in-java – Fahim Mar 04 '15 at 09:32
  • any chances to do that on Android? `Files` class is not available there. Will rename title of question as well to point to android – Vi Matviichuk Mar 04 '15 at 09:52

3 Answers3

1

It seems to me that what you want is to read some meta-data in order to find out more about the picture that you are loading.

I do not know of a API's existance in android in order to perform this operation, but this is probably what you need.

For example, in your case you could refer to this on the official documentation.

George Daramouskas
  • 3,720
  • 3
  • 22
  • 51
  • this one looks good, but it does not support `JPEG2000`. If type was not defined I can assume it to be JPEG2000, but looking for more handsome solution. Found few open sourced variants, trying to combine them right now – Vi Matviichuk Mar 04 '15 at 10:25
1
    public static ImageType getImageType(byte[] data) {
    if (data == null) {
        return UNKNOWN;
    }
    byte[] header = new byte[11];
    System.arraycopy(data, 0, header, 0, Math.min(data.length, header.length));
    int c1 = header[0] & 0xff;
    int c2 = header[1] & 0xff;
    int c3 = header[2] & 0xff;
    int c4 = header[3] & 0xff;
    int c5 = header[4] & 0xff;
    int c6 = header[5] & 0xff;
    int c7 = header[6] & 0xff;
    int c8 = header[7] & 0xff;
    int c9 = header[8] & 0xff;
    int c10 = header[9] & 0xff;
    int c11 = header[10] & 0xff;

    Log.d(TAG, "Headers: " + "\nc1: " + c1 + "\nc2: " + c2 + "\nc3: " + c3 + "\nc4: " + c4 +  "\nc5: " + c5
            + "\nc6: " + c6 + "\nc7: " + c7 + "\nc8: " + c8 + "\nc9: " + c9 + "\nc10: " + c10 + "\nc11: " + c11);

    // verify JPEG format
    if (c1 == 0xFF && c2 == 0xD8 && c3 == 0xFF) {
        if (c4 == 0xE0) {
            return JPEG;
        }

        /**
         * File format used by digital cameras to store images. Exif Format can be read by any application supporting JPEG. Exif Spec can be found at:
         * http://www.pima.net/standards/it10/PIMA15740/Exif_2-1.PDF
         */
        if ((c4 == 0xE1) && (c7 == 'E' && c8 == 'x' && c9 == 'i' && c10 == 'f' && c11 == 0)) {
            return JPEG;
        }
    }
    // verify JPEG2000 format. Codestream - http://sourceforge.net/p/optipng/mercurial/ci/a9d8ace0ee2f1aab641b787d252c166373bdeddc/tree/src/pngxtern/pngxrjpg.c#l26
    // 00 00 00 0C 6A 50 20 20 0D 0A 87 0A
    else if (c1 == 0x00 && c2 == 0x00 && c3 == 0x00 && c4 == 0x0C) {
        return JPEG2000;
    }

    // return JPEG by default
    return JPEG;
}

Some code snippet was taken from here and enhanced for JPEG2000 definition. By default returning JPEG format in my case, can be anything else in yours. UNKNOWN, JPEG and JPEG2000 are just my custom enums.

Vi Matviichuk
  • 1,122
  • 15
  • 32
0

Try this to check the type of file

private static String getMimeType(String fileUrl) {
    String extension = MimeTypeMap.getFileExtensionFromUrl(fileUrl);
    return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
Fahim
  • 12,198
  • 5
  • 39
  • 57