使用JDK5带的ImageIO进行图片格式转换发现有些图片转换失败,原转换代码为
我在PNG转换BMP时发现原图type=BufferedImage.TYPE_CUSTOM时转换失败,把代码改为:
就好了
public static boolean convert(String srcFilePath, String destFilePath) throws IOException{
BufferedImage image = ImageIO.read(new File(srcFilePath));
return ImageIO.write(image, IMAGE_TYPE, new File(destFilePath));
}
我在PNG转换BMP时发现原图type=BufferedImage.TYPE_CUSTOM时转换失败,把代码改为:
public static boolean convert(String srcFilePath, String destFilePath) throws IOException{
BufferedImage image = ImageIO.read(new File(srcFilePath));
if(!ImageIO.write(image, IMAGE_TYPE, new File(destFilePath))){
BufferedImage img = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
img.getGraphics().drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
return ImageIO.write(img, IMAGE_TYPE, new File(destFilePath));
}
return true;
}
就好了