views:

154

answers:

3

I have a Java website, now Ive build an uploader, the validation for JPG images is done and a way to save it into the server it is too.

But now I need to create an utility class using isGIF and isTIFF validated them by byte[] and convert that byte[] to java.awt.Image to save as JPG

So basically I need boolean isGIF(byte[]), boolean isTIFF(byte[]), java.awt.Image convertGIF(byte[]) and java.awt.Image convertTIFF(byte[]).

But I have no idea how to do it, could someone help me please?

Thank you!

+1  A: 

Most image formats start with a unique identifier i.g.

000: 47 49 46 38 39 61 GIF89a Header

You could read them using Java Advanced Imaging I/O

stacker
Looking for a code example, I have no idea what and how to build.
BoDiE2003
Seems easier to match the one target encoding and recode everything else first, I've added an example of this to my answer. JPEG files start with 0xFFD8 according to my mime magic file from apache.
Geoff
A: 

You could consider using mime-utils. It basically gives you the mime type for a given byte[] or stream source using the same detection that the unix file command uses.

http://www.medsea.eu/mime-util (seems to be unavailable currently, try looking at the google cache for info).

You could also consider simply parsing every image using ImageIO.read and then rewriting them all using ImageIO.write. This will work for jpeg images too, althogh it isn't totally efficient. It would be an appropriate solution if you wanted to scale, crop or watermark your images at the same time. Since this is web submitted data, there is a certain argument for attempting to parse the file as image to make sure that it really is an image, and not a malicious file type.

ImageIO is smart so you don't have to know what type the data is to begin with. And it will through an exception if the data is malformed, so this makes sure that your image data is valid.

JPEG files apparently start with 0xFFD8, so you can just check for that in your code.

For example, to simply recode every image that doesn't look like a jpeg:

byte[] bytes = new byte[1024]; //your input
byte[] result = bytes;
if( bytes[0] != 0xFF || bytes[1] != 0xD8 )
{    
    BufferedImage image = ImageIO.read( new ByteArrayInputStream( bytes ) );
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write( image, "jpeg", baos );
    result = baos.toByteArray();
}
Geoff
Looking for a code example, I have no idea what and how to build.
BoDiE2003
Unfortunately I'm not looking to write one, I've given quite a bit of info, ImageIO is quite simple, I'll maybe update with details of that shortly.
Geoff
A: 

The Java ImageIO package (particular the ImageIO class) has lots of methods for reading and writing images. You could implement your idea by looking up the readers by suffix and seeing if they work on the target file or byte array.

Although Stacker's idea of looking at the first few bytes of the file is probably way better since you could probably achieve the goal by just reading the first few bytes of a file.

maerics