51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

图片转ico - java

img,png图片转ico {#imgpng图片转ico}

code {#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 {


    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> IOException {
        <span class="hljs-type">BufferedImage</span> <span class="hljs-variable">bufferedImage</span> <span class="hljs-operator">=</span> ImageIO.read(<span class="hljs-keyword">new</span> <span class="hljs-title class_">File</span>(<span class="hljs-string">"d://logo.png"</span>));
        <span class="hljs-keyword">if</span>(bufferedImage == <span class="hljs-literal">null</span>){
            System.out.println(<span class="hljs-string">"File not find"</span>);
        }
        List&lt;BufferedImage&gt; icons = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();
        icons.add(getScaledInstance(bufferedImage, <span class="hljs-number">16</span>, <span class="hljs-number">16</span>));
        icons.add(getScaledInstance(bufferedImage, <span class="hljs-number">32</span>, <span class="hljs-number">32</span>));
        icons.add(getScaledInstance(bufferedImage, <span class="hljs-number">64</span>, <span class="hljs-number">64</span>));
        icons.add(getScaledInstance(bufferedImage, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>));
        ICOEncoder.write(icons,<span class="hljs-keyword">new</span> <span class="hljs-title class_">File</span>(<span class="hljs-string">"d://logo.ico"</span>));
    }

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> BufferedImage <span class="hljs-title function_">getScaledInstance</span><span class="hljs-params">(BufferedImage bufferedImage,<span class="hljs-type">int</span> toWidth,<span class="hljs-type">int</span> toHeight)</span>{
        <span class="hljs-type">Image</span> <span class="hljs-variable">scaledInstance</span> <span class="hljs-operator">=</span> bufferedImage.getScaledInstance(toWidth, toHeight, Image.SCALE_SMOOTH);
        <span class="hljs-type">BufferedImage</span> <span class="hljs-variable">newBufferedImage</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BufferedImage</span>(toWidth,
                toHeight, BufferedImage.TYPE_INT_RGB);
        newBufferedImage.createGraphics().drawImage(scaledInstance, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, Color.WHITE, <span class="hljs-literal">null</span>);
        <span class="hljs-keyword">return</span> newBufferedImage;
    }



`}
`

pom.xml

<dependency>
    <groupId>net.ifok.image</groupId>
    <artifactId>image4j</artifactId>
    <version>0.7.2</version>
</dependency>
赞(1)
未经允许不得转载:工具盒子 » 图片转ico - java