效果:
为什么会有ico转换png的想法,因为vue引用ico文件会报错,解析不了。。。。
不想跑代码可以用这个免费的网站:
参考:
java ico图片转png_Java 图片处理: ico 格式转 PNG/JPG 等格式_乔治·华尔特的博客-CSDN博客
优化代码:
package com.yx.img;
import com.mortennobel.imagescaling.AdvancedResizeOp;
import com.mortennobel.imagescaling.ResampleOp;
import net.ifok.image.image4j.codec.ico.ICODecoder;
import net.ifok.image.image4j.codec.ico.ICOImage;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class ImgTool {
public static int SIZE_32=32;
public static int SIZE_64=64;
public static int SIZE_128=128;
public static int SIZE_256=256;
public static int SIZE_512=512;
private static SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
/**
* ico文件转png文件
* @param icoFilePath ico文件路径
* @param saveDirPath png存放文件夹路径
* @param size 转换大小
* @throws Exception
*/
public static void ico_png(String icoFilePath,String saveDirPath,int size) throws Exception {
log("欢迎使用ico转png程序");
InputStream in=getInputStreamByFilePath(icoFilePath);
List<ICOImage> list= ICODecoder.readExt(in);// 从响应结果集中获取 ico 图片流
if(list.size()<1)throw new Exception("没有找到相关的ico文件");
ICOImage icoImage=list.get(0);
log("ico文件准备就绪,路径:"+icoFilePath);
log("预转换大小为:"+size+"x"+size);
/**
* 因为ico矢量放大不模糊,png放大会模糊
* 应该先放大ico然后再转png
*/
ResampleOp resampleOp=new ResampleOp(size,size);//设置放大后大小
resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Oversharpened);
BufferedImage bi=resampleOp.filter(icoImage.getImage(),null);
BufferedImage result=new BufferedImage(size,size,BufferedImage.TYPE_INT_RGB);
Graphics2D g=result.createGraphics();
/**
* 默认背景是黑色的,这里设置透明背景
* 设置透明背景 (start)
*/
result = g.getDeviceConfiguration().createCompatibleImage(size, size, Transparency.TRANSLUCENT);
g.dispose();
g = result.createGraphics();
/**
* 设置透明背景 (end)
*/
g.setColor(Color.WHITE);
g.drawImage(bi.getScaledInstance(size,size,Image.SCALE_SMOOTH),0,0,null);
ByteArrayOutputStream bos=new ByteArrayOutputStream();
ImageIO.write(result,"PNG",bos);
OutputStream out=getOutputStream(saveDirPath+getFileName(icoFilePath).replace(".","_")+"_"+size+".png");
out.write(bos.toByteArray());
out.flush();
out.close();
bos.close();
in.close();
log("转换成功");
}
private static InputStream getInputStreamByFilePath(String path) throws FileNotFoundException {
return getInputStreamByFile(new File(path));
}
private static InputStream getInputStreamByFile(File file) throws FileNotFoundException {
if(!file.exists())throw new FileNotFoundException("文件不存在");
return new FileInputStream(file);
}
private static OutputStream getOutputStream(String path) throws IOException {
return getOutputStream(new File(path));
}
private static OutputStream getOutputStream(File file) throws IOException {
if (!file.getParentFile().exists()) file.mkdirs();
if(!file.exists())file.createNewFile();
else{
String fileName1=file.getName().substring(0, file.getName().lastIndexOf("."));
String fileName2=file.getName().substring(file.getName().lastIndexOf("."));
int count=1;
while (true){
File f=new File(file.getParent()+"/"+fileName1+"("+count+")"+fileName2);
if(f.exists()){
count++;
continue;
} else {
f.createNewFile();
file = f;
break;
}
}
}
log("创建写入文件:"+file.getAbsolutePath());
return new FileOutputStream(file);
}
/**
* 获取文件名
*/
private static String getFileName(String path){
return new File(path).getName();
}
private static void log(String name){
System.out.println(sf.format(new Date())+"-> "+name);
}
public static void main(String[] args) throws Exception {
String path="D:/tmp/";
String savePath="D:/tmp/";
ImgTool.ico_png(path+"imageres_1027.ico",savePath,ImgTool.SIZE_64);
ImgTool.ico_png(path+"imageres_1027.ico",savePath,ImgTool.SIZE_512);
}
}
依赖引入:
如何找jar
开源jar包被各网站封锁收费?教你如何搜索并下载想要jar_学习使我快乐——玉祥的博客-CSDN博客
控制台:
2022-10-24 15:59:10:731-> 欢迎使用ico转png程序
2022-10-24 15:59:11:120-> ico文件准备就绪,路径:D:/tmp/imageres_1027.ico
2022-10-24 15:59:11:120-> 预转换大小为:64x64
2022-10-24 15:59:11:229-> 创建写入文件:D:\tmp\imageres_1027_ico_64.png
2022-10-24 15:59:11:229-> 转换成功
2022-10-24 15:59:11:229-> 欢迎使用ico转png程序
2022-10-24 15:59:11:292-> ico文件准备就绪,路径:D:/tmp/imageres_1027.ico
2022-10-24 15:59:11:292-> 预转换大小为:512x512
2022-10-24 15:59:11:569-> 创建写入文件:D:\tmp\imageres_1027_ico_512.png
2022-10-24 15:59:11:569-> 转换成功
效果: