我有这种方法来读取图像并进行复制,但令我困扰的是可以使用.jpg,.gif,.jpeg来实现,而不能使用.png。这是为什么?它不会引发异常,例如,如果我尝试使用.doc之类的东西,它将打印“读取完成”和“写入完成”,就像什么都没有阻止它,但是目标文件夹中没有图像。
public void copyImage(File f, String filename){ //Takes the file chosen and the name of it
BufferedImage image = null;
int width = 963; //width of the image
int height = 640; //height of the image
//read image
try{
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
image = ImageIO.read(f);
System.out.println("Reading complete.");
}catch(IOException e){
System.out.println("Error: "+e);
}
//write image, here is the naming of the new image going, currently if same image often it is like image.jgp, image(1).jgp, image(1)(2).jpg......
try{
int imageNumer = 1; //For updating the number in the parenthesis based on how many with the same name in resources folder
String imageName = filename;
String outPath = "src/main/resources/images/"; //Path for saving, have special events folder now so if timeline guys are doing something they don't override copies
while (folderHasImage(imageName)==true){ //Check if our folder has the imagename already if so, add (int) untill no more true
int index = imageName.indexOf("."); ;
imageName = imageName.substring(0, index)+ "("+ imageNumer+")" + ".jpg";
imageNumer++;
}
f = new File(outPath + imageName); //output file path
ImageIO.write(image, "jpg", f);
System.out.println("Writing complete.");
}catch(IOException e){
System.out.println("Error: "+e);
}
谢谢,我会这样做。但是我可以采用jpg并将其更改为png,但不能反过来吗?原因是,当我将其从“ jpg”更改为“ png”时,它们对于jpg,jpeg,gif和png都工作正常(png不能正常工作,但是其余的都可以)]