我有一个可将图像从PNG转换为JPG的类(出于节省空间的原因)。我的问题是,大多数图像都是以此为。 [
目前,我发现只有两张不受转换gallery影响的图片,但是如果您使用的是默认Windows,则10张图片应用窗口会在浏览图片时忽略它们。如果我打开它们,则应用程序的行为就像这只是文件夹中的图片,即使事实并非如此。
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.stream.FileImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.*;
public class PNG2JPG {
boolean status;
PNG2JPG(String path,float quality){
File file = new File(path);
try {
BufferedImage image = ImageIO.read(file);
String fileName = file.getName();
path = path.substring(0,path.lastIndexOf('\\'));
fileName = fileName.substring(0,fileName.lastIndexOf('.'));
JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(null);
jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpegParams.setCompressionQuality(quality); //Quality from 0 to 1
final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
writer.setOutput(new FileImageOutputStream(
new File(path + "/" + fileName + ".jpg")));
writer.write(null, new IIOImage(image, null, null), jpegParams);
status = true;
}
catch (IOException e){
System.out.println("No file found");
status = false;
}
}
}