简体   繁体   中英

Java convert GIF image to PNG format

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!

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"));

使用ImageIo以您想要的任何格式保存图像。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM