Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java validate image header

I am having one requirement where i need to identify the type of the image not by the files extension but by validating the header. I am trying to validate a JPEG file by magic number validation.

File imgFile =
   new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg");
byte[] ba = Files.toByteArray(imgFile); //Its a google guava library
int i = 0; 
if(ba [i] == 0xFF && ba[i+1] == 0xD8 && ba[i+2] == 0xFF && ba[i+3] == 0xE0){
   System.out.println("isJPEG");
}

But the condition is not getting satisfied at all. ba[i] returns -1.

What am I doing wrong?

like image 228
karthick Avatar asked Oct 22 '22 14:10

karthick


1 Answers

In Java the byte primitive is signed and has a range from -128 to 127, which means your ba[0] comes out as -1, whereas 0xFF is interpreted as an int as 255. If you do a bitwise AND with 0xFF using the & operator this converts the signed byte to the correct value and your code will work, as follows:

    File imgFile =
           new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg");
        byte[] ba = Files.toByteArray(imgFile); //Its a google guava library
        int i = 0; 
        if((ba [i] & 0xFF) == 0xFF && (ba[i+1] & 0xFF) == 0xD8 && (ba[i+2] & 0xFF) == 0xFF 
           && (ba[i+3] & 0xFF) == 0xE0) {
           System.out.println("isJPEG");
        }

I'm not sure about the exact logic you are using however, as wikipedia states that the magic numbers for JPEGs are as follows:

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

FF E0 is JFIF specific, so you may want to double check why you are checking for this

The following would be the code to use to make the checks as per Wikipedia:

        File imgFile =                 
                    new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg");
        byte[] ba = Files.toByteArray(imgFile); //Its a google guava library
        int i = 0; 
        if((ba [i] & 0xFF) == 0xFF && (ba[i+1] & 0xFF) == 0xD8 && (ba[ba.length - 2] & 0xFF) == 0xFF 
           && (ba[ba.length - 1] & 0xFF) == 0xD9) {
           System.out.println("isJPEG");
        }
like image 128
Joe Elleson Avatar answered Oct 27 '22 09:10

Joe Elleson