Skip to content

The picture is automatically converted to icon #256

Open
@lhDream

Description

@lhDream

Hi @fvarrui .
Are you considering adding the function of automatically converting pictures to icons?

I can provide the following sample:

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>

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions