0

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.

Community
  • 1
  • 1
shaun tait
  • 11
  • 4
  • @SergiyMedvynskyy my question is entirely different, i make an edit to understand it properly for you. – shaun tait Jun 04 '18 at 08:15
  • @SergiyMedvynskyy kindly see my question once again – shaun tait Jun 04 '18 at 08:19
  • sorry, but I cannot understand the difference between the question posted as duplicate and the your case. – Sergiy Medvynskyy Jun 04 '18 at 09:03
  • @SergiyMedvynskyy sorry i cann't explain my difference clearly, the difference is, in the dublicate he has one image and he compress it, in my case i have a folder in which there are multiple images i want to compress them, thats the difference, now i hope you understand the difference – shaun tait Jun 04 '18 at 09:15
  • You can iterate over all files in the folder and compress them – Sergiy Medvynskyy Jun 04 '18 at 09:36
  • @SergiyMedvynskyy i did but it gave me the, i was wondering it will be great if you can show me how i can do it – shaun tait Jun 04 '18 at 09:42
  • @SergiyMedvynskyy can you make an edit to code, so i can see where i was doing wrong. – shaun tait Jun 04 '18 at 09:43
  • Look [here](https://stackoverflow.com/questions/5368724/how-to-copy-a-folder-and-all-its-subfolders-and-files-into-another-folder) to see how you should iterate over files in a folder. – Sergiy Medvynskyy Jun 04 '18 at 09:49
  • @SergiyMedvynskyy sorry but i have to ask again the link which you share is for copy and paste , i already did that work , now i want that in the paste folder(which contain multiple images), i want that images to compress – shaun tait Jun 04 '18 at 09:55
  • You must try to put it together by yourself. We cannot write programs instead of you. – Sergiy Medvynskyy Jun 04 '18 at 11:01

0 Answers0