直播系统搭建,java实现 图片转ico
直播系统搭建,java实现 图片转ico
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class imgUtil {
public static void main(String[] args) throws Exception {
//File file = new File("C:/Users/yys/Desktop/gggh.jpg");
InputStream in = new FileInputStream("C:\\Users\\yys\\Desktop\\chengxu\\sxfdgh\\16620232335584.png");
BufferedImage bufferedImage = ImageIO.read(in);
if(bufferedImage == null){
System.out.println("File not find");
return;
}
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("C:\\Users\\yys\\Desktop\\logo.ico"));
}
/**
*
* @param imgName 源图名称
* @param imgPath 图片路径
* @param imgFormat 要修改成的图片格式
* @param imgNewName 新图片名称
* @return
*/
public static Map<String,Object> updateImgs(String imgName,String imgPath,String imgFormat,String imgNewName){
Map<String,Object> map = new HashMap<>();
try {
InputStream in = new FileInputStream(imgPath+imgName);
map = updateImgs(in,imgPath,imgFormat,imgNewName);
}catch (Exception e){
map.put("status",false);
System.out.println("更改图片格式异常:"+e.getMessage());
return map;
}
return map;
}
/**
*
* @param in 原图片流
* @param imgPath 图片路径
* @param imgFormat 要修改成的图片格式
* @param imgNewName 新图片名称
* @return
*/
public static Map<String,Object> updateImgs(InputStream in,String imgPath,String imgFormat,String imgNewName){
Map<String,Object> map = new HashMap<>();
try {
BufferedImage bufferedImage = ImageIO.read(in);
if(bufferedImage == null){
System.out.println("File not find");
map.put("status",false);
return map;
}
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(imgPath+imgNewName));
map.put("status",true);
map.put("imgUrl",imgPath+imgNewName);
}catch (Exception e){
map.put("status",false);
System.out.println("更改图片格式异常:"+e.getMessage());
return map;
}
return map;
}
public static BufferedImage getScaledInstance(BufferedImage bufferedImage, int toWidth, int toHeight){
Image scaledInstance = bufferedImage.getScaledInstance(toWidth, toHeight, 0);
BufferedImage newBufferedImage = new BufferedImage(toWidth,
toHeight, BufferedImage.TYPE_INT_RGB);
newBufferedImage.createGraphics().drawImage(scaledInstance, 0, 0, Color.WHITE, null);
return newBufferedImage;
}
}
需引入jar
<dependency>
<groupId>net.ifok.image</groupId>
<artifactId>image4j</artifactId>
<version>0.7.2</version>
</dependency>
以上就是 直播系统搭建,java实现 图片转ico,更多内容欢迎关注之后的文章