开发者

Java: Detect if image isGIF or isTIFF and convert to JPG

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!


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();
}


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


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.


Using Java Advanced Imaging API for conversion I have been able to convert byte streams to JPEG, JPG, BMP, GIF, etc. And it is really easy. I hope this example helps!

import java.awt.image.RenderedImage;
import java.io.IOException;

import javax.media.jai.JAI;
import com.sun.media.jai.codec.ByteArraySeekableStream;
import com.sun.media.jai.codec.SeekableStream;


public class ImageConverter
{
  public static void convertImage(byte[] image, String targetFormat, String filename)
  {
    try
    {
      SeekableStream stream = new ByteArraySeekableStream(image);

      // read stream into a rendered image
     RenderedImage renderedImage = JAI.create("stream", stream);

      // persist image to file
     JAI.create("filestore", renderedImage, filename, targetFormat);

     stream.close();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜