您当前的位置:首页 > 计算机 > 编程开发 > Java

java实现 图片转ico

时间:09-05来源:作者:点击数:

java实现 图片转ico

code

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>
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐