Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify contents of a byte[] is a JPEG?

Tags:

java

image

jpeg

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.

like image 202
Kylar Avatar asked Dec 28 '10 23:12

Kylar


People also ask

How do I identify a JPEG file?

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.

Do you know the first 2 bytes of JPEG format?

File Organization The first two bytes of every JPEG stream are the Start Of Image (SOI) marker values FFh D8h.

What is the file content for JPG?

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.

What is image byte array?

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.


1 Answers

From wikipedia:

JPEG image files begin with FF D8 and end with FF D9.

http://en.wikipedia.org/wiki/Magic_number_(programming)

like image 128
zsalzbank Avatar answered Sep 28 '22 02:09

zsalzbank