I have to build a Java servlet that receives an image and returns that image converted to PNG format. How can I achieve this? By converting I don't mean changing the file extension, like some examples suggest.
Thanks in advance!
Select a GIF file to convert by uploading it from your computer or a cloud storage service like Google Drive or Dropbox. You can also just drag-and-drop your file into the box to upload. Once you have done this, our free* online tool will begin to convert your GIF file into a PNG image.
In Java, write() method is used to convert an image from gif type of format to jpg, use the static method write() provided by the class ImageIO under javax. imageio package. Parameters: write() method accepts 3 parameters namely image, formatName and output.
Try this out:
package demo;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
public class Main {
public static void main( String [] args ) throws IOException {
File input = new File("input.gif");
File output = new File("output.png");
ImageIO.write( ImageIO.read( input ), "png", ouput);
}
}
Read ImageIO.
Of course, you may want to read and write from an stream instead.
ImageIO.write(ImageIO.read(new File("img.gif")), "png", new File("img.png"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With