简体   繁体   中英

Android: define image type (jpeg or jpeg2000) from inputstream

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

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.

Try this to check the type of file

private static String getMimeType(String fileUrl) {
    String extension = MimeTypeMap.getFileExtensionFromUrl(fileUrl);
    return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
    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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM