import net.ifok.image.image4j.codec.ico.ICOEncoder;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Demo {
public static void main(String[] args) throws IOException {
BufferedImage bufferedImage = ImageIO.read(new File("d://logo.png"));
if(bufferedImage == null){
System.out.println("File not find");
}
List<BufferedImage> icons = new ArrayList<>();
icons.add(getScaledInstance(bufferedImage, 16, 16));
icons.add(getScaledInstance(bufferedImage, 32, 32));
icons.add(getScaledInstance(bufferedImage, 64, 64));
icons.add(getScaledInstance(bufferedImage, 128, 128));
ICOEncoder.write(icons,new File("d://logo.ico"));
}
public static BufferedImage getScaledInstance(BufferedImage bufferedImage,int toWidth,int toHeight){
Image scaledInstance = bufferedImage.getScaledInstance(toWidth, toHeight, Image.SCALE_SMOOTH);
BufferedImage newBufferedImage = new BufferedImage(toWidth,
toHeight, BufferedImage.TYPE_INT_RGB);
newBufferedImage.createGraphics().drawImage(scaledInstance, 0, 0, Color.WHITE, null);
return newBufferedImage;
}
}
pom.xml
<dependency>
<groupId>net.ifok.image</groupId>
<artifactId>image4j</artifactId>
<version>0.7.2</version>
</dependency>