PngToIco.java

发布时间:2024年01月17日

PngToIco.java [PNG转ICO]为了解决今天项目PNG弄成ICO写的

package image;


import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.imaging.ImageFormats;
import org.apache.commons.imaging.ImageWriteException;
//Apache Commons Imaging
/*
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-imaging -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-imaging</artifactId>
    <version>1.0-alpha3</version>
</dependency>
*/
import org.apache.commons.imaging.Imaging;

/**
 * PNG转ICO
 * 
 * @author ZengWenFeng
 * @email 117791303@QQ.com
 * @mobile 13805029595
 * @date 2024.01.16
 */
public class PngToIco
{

	public PngToIco()
	{

	}

	public static void main(String[] args)
	{

		// 加载PNG文件
		File pngFile = new File("C:/Users/Administrator/Desktop/further_logo.rp.png");
		BufferedImage bufferedImage = null;
		try
		{
			bufferedImage = ImageIO.read(pngFile);
		}
		catch (IOException e1)
		{
			e1.printStackTrace();
		}

		// 设置输出ICO文件路径及名称
		String pathIco = "C:/Users/Administrator/Desktop/further_logo.rp.ico";

		// 调用Imaging类的write()方法将BufferedImage保存为ICO文件
		try
		{
			Imaging.writeImage(bufferedImage, new File(pathIco), ImageFormats.ICO);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		catch (ImageWriteException e)
		{
			e.printStackTrace();
		}
		
		System.out.println("ok");

	}

}

package image;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;

import net.sf.image4j.codec.ico.ICODecoder;
/*
<!-- https://mvnrepository.com/artifact/net.sf.image4j/image4j -->
<dependency>
    <groupId>net.sf.image4j</groupId>
    <artifactId>image4j</artifactId>
    <version>0.7zensight1</version>
</dependency>
*/
import net.sf.image4j.codec.ico.ICOEncoder;

public class PngToIco2
{

	public PngToIco2()
	{

	}

	public static void main(String[] args)
	{
		// 加载PNG文件
		String pathPng = "C:/Users/Administrator/Desktop/further_logo.rp.png";
		
		System.out.println(pathPng.substring(pathPng.lastIndexOf(".") + 1));

		// 设置输出ICO文件路径及名称
		String pathIco = "C:/Users/Administrator/Desktop/further_logo.rp.ico";

		BufferedImage bi = null;
		try
		{
			bi = ImageIO.read(new File(pathPng));
			ICOEncoder.write(bi, new File(pathIco));
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}

	}

	/**
	 * PNG转ICO
	 * 
	 * @author ZengWenFeng
	 * @email 117791303@QQ.com
	 * @mobile 13805029595
	 * @date 2024.01.16
	 * @param png
	 * @param ico
	 */
	public static void convertPngToIco(String png, String ico)
	{
		try
		{
			BufferedImage bi = ImageIO.read(new File(png));
			ICOEncoder.write(bi, new File(ico));
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * ICO转PNG
	 * 
	 * @author ZengWenFeng
	 * @email 117791303@QQ.com
	 * @mobile 13805029595
	 * @date 2024.01.16
	 * @param png
	 * @param ico
	 */
	public static void convertIcoToPng(String ico, String png)
	{
		try
		{
			List<BufferedImage> images = ICODecoder.read(new File(ico));
	        ImageIO.write(images.get(0), "png", new File(png));
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

}

文章来源:https://blog.csdn.net/spencer_tseng/article/details/135638626
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。