I have a small byte array (under 25K) that I receive and decode as part of a larger message envelope. Sometimes this is an image, furthermore it is a JPG. I have no context information other than the byte array, and need to identify both if it IS an image, and if the image is of type JPG.
Is there some magic number, or magic bytes that exist at the beginning, end or at some offset that I can look at to identify it?
An example of my code looks like this (from memory, not c/p):
byte[] messageBytesAfterDecode = retrieveBytesFromEnvelope(); if(null != messageBytesAfterDecode && messageBytesAfterDecode > 0){ if(areTheseBytesAJpeg(messageBytesAfterDecode)){ doSomethingWithAJpeg(messageBytesAfterDecode) }else{ flagEnvelopeAsHavingBadContentInTheField(); } }
I really need what would go into the
areTheseBytesAJpeg(byte[] mBytes){}
method, or even a pointer to a spec that details it. I'm hoping there is a very quick way to make this determination, since I don't really want to read them into an Image, etc.
If you are having trouble and want to check if you photo is a JPEG, look at the writing under the photo in its file name. If it ends . jpg or . jpeg- then the file is a JPEG and will upload.
File Organization The first two bytes of every JPEG stream are the Start Of Image (SOI) marker values FFh D8h.
JPG is a digital image format which contains compressed image data. With a 10:1 compression ratio JPG images are very compact. JPG format contains important image details. This format is the most popular image format for sharing photos and other images on the internet and between Mobile and PC users.
Images are binary data - this is easily represented as byte arrays. The image in the sample is stored in the database as a BLOB - not a string or location, that is, it is binary data.
From wikipedia:
JPEG image files begin with FF D8 and end with FF D9.
http://en.wikipedia.org/wiki/Magic_number_(programming)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With