我编写了以下代码,用于从一个驱动器复制图像并压缩它然后将其粘贴到另一个驱动器中。
public class JPEGCompressor {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
File originalImage = new File("C:\Users\Jhonny\Pictures\pics");
File compressedImage = new File("E:\\");
try{
compressJpegImage(originalImage, compressedImage, 0.5f);
System.out.println("done!");
}
catch(IOException e){
System.out.println("error");
}
}
public static void compressJpegImage(File originalImage, File CompressedImage,float compressionQuality )throws IOException{
RenderedImage image = ImageIO.read(originalImage);
ImageWriter jpegWriter = ImageIO.getImageWritersByFormatName("jpg").next();
ImageWriteParam jpegWriteParam = jpegWriter.getDefaultWriteParam();
jpegWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpegWriteParam.setCompressionQuality(compressionQuality);
try(ImageOutputStream output = ImageIO.createImageOutputStream(CompressedImage)){
jpegWriter.setOutput(output);
IIOImage outputImage = new IIOImage(image, null, null);
jpegWriter.write(null, outputImage, jpegWriteParam);
}
jpegWriter.dispose();
}
}
但我的问题是,如果图像是多个我们会怎么做,那么我们如何从一个文件夹进行复制然后压缩然后保存到另一个驱动器?我从网上尝试了很多东西,但对我来说没什么用。