Java: detect if image isGIF or isTIFF and convert to JPG
I have a Java website, now I am creating an uploader, validation is done for JPG images and a way to save it to the server too.
But now I need to create a utility class using isGIF and isTIFF checks their byte [] and converts that byte [] to java.awt.Image to save as JPG
So basically I need logic isGIF(byte[])
, boolean isTIFF(byte[])
, java.awt.Image convertGIF(byte[])
and java.awt.Image convertTIFF(byte[])
.
But I have no idea how to do this, can anyone help me?
Thanks!
a source to share
You can use mime-utils. This basically gives you the mime type for a given byte [] or stream source, using the same detection the unix command uses file
.
http://www.medsea.eu/mime-util (doesn't seem to be available currently, try looking at google cache for info).
You can also just parse each image with ImageIO.read and then rewrite them all with ImageIO.write. This will work for jpeg images as well, but it's not entirely efficient. This would be a good solution if you wanted to scale, crop, or watermark your images at the same time. Since this is web data, there is some argument in trying to parse the file as an image to make sure it is indeed an image and not a malicious file type.
ImageIO is smart, so you don't need to know what type of data to start with. And it will happen through an exception if the data gets corrupted, so this ensures that your image data is valid.
JPEG files obviously start with 0xFFD8, so you can just check that in your code.
For example, just recode every image that doesn't look like 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();
}
a source to share
Most image formats start with a unique identifier ig
000: 47 49 46 38 39 61 GIF89a Title
You can read them using Java Advanced Imaging I / O
a source to share
The Java ImageIO package (in particular the ImageIO class) contains many methods for reading and writing images. You could implement your idea by looking at readers by suffix and seeing if they are working on the target file or byte array.
Although Stacker's idea of ββlooking at the first few bytes of a file is probably better, as you could probably achieve the goal simply by reading the first few bytes of the file.
a source to share
Using Java Advanced Imaging API for conversion, I was able to convert byte streams to JPEG, JPG, BMP, GIF, etc. And it's really easy. 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();
}
}
}
a source to share