Java validate image header -
I have a requirement where I do not need to identify the type of image by file extension, but by validating the header . I am trying to validate the JPEG file by magic number verification.
file imgFile = new file ("C: \\ user \\ public \\ picture \\ sample image \\ Chrysanthemum.jpg"); Byte [BA] = Files To beatierre (imgfile); // Its a Google Java library int i = 0; If (BA [i] == 0xFF & amp; ba [i + 1] == 0xD8 and b [i + 2] == 0xFF & b [i + 3] == 0xE0) {println ("IsJPEG"); } But the condition is not satisfied at all. BA [i] returns -1 . What am I doing?
byte has been signed in Java and has a limit of 128 to 127, which means your BA [0] -1 comes out as, while 0xFF is interpreted as an int as 255 has been done. If you do the bitword and use the 0xFF with the and operator, it converts the signed byte to the correct value and your code will work as follows: file imgFile = new file ("C: \\ user \\ public \\ picture \\ sample picture \\ Chrysanthemum.jpg"); Byte [BA] = Files To Batterier (IMGFile); // Its a Google Java library int i = 0; If ((BA [I] and 0x FF) == 0x FF & amp (BA [I + 1] and 0x FF) == 0xD8 & amp; (BA [I + 2] and 0x FF) == 0x FF & Amp; amp; (b [i + 3] & amp; 0xFF) == 0xE0) {System.out.println ("isJPEG"); } I'm sure what you are using about exact logic, though Wikipedia says that magic numbers for JPEG are as follows:
JPEG image files start with FF D8 and end with FF D9
FF e , you can make Jeffef specific, so You can check why you are checking for it
file imgFile = new file ("C: \\ user \\" The Public \\ will have the following code to make use of Czech by pictures: \ sample images \\ Chrysanthemum.jpg "); Byte [BA] = Files To Batterier (IMGFile); // Its a Google Java library int i = 0; If ((BA [i] & 0xFF) == 0xFF & amp; (BA [I + 1] and 0xFF) == 0xD8 & amp; (BA [BA.Length = 2] and 0xFF) == 0xFF Amp; (ba [ba.length - 1] and 0xFF) == 0xD 9) {System.out.println ("isJPEG"); }
Comments
Post a Comment