I have written the following code that copy image from one drive and compress it then paste it into another drive.
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();
}
}
but my question is , what we do if the images are multiple, then how can we do the copy from one folder and then compress then save into another drive? i tried many things from internetbut nothing works for me.