fvarrui / JavaPackager

:package: Gradle/Maven plugin to package Java applications as native Windows, MacOS, or Linux executables and create installers for them.
GNU General Public License v3.0
1.07k stars 134 forks source link

The picture is automatically converted to icon #256

Open lhDream opened 2 years ago

lhDream commented 2 years ago

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>
lhDream commented 2 years ago

https://github.com/imcdonagh/image4j

fvarrui commented 2 years ago

Hi @lhDream! Thanks for your proposal. Maybe can we do that JavaPackager looks for a PNG file and convert it in an ICO file if it can't find this last one?

lhDream commented 2 years ago

Hi @fvarrui ! Yes. Or automatically convert when configured like this.

<icoFile>yourPath/logo.png</icoFile>