1

I try to save a picture by using the class ImageIO, but I have found a problem that the size of the picture will small. The code is like this:

public class SeamCarving {
    static String path="C:/Users/lenovo/Desktop/h4/01.jpg";
    public static void main(String[] args)throws Exception {
        File file1=new File(path);
        System.out.println(file1.length());
        BufferedImage image=ImageIO.read(file1);
        File file2=new File("d:/02.jpg");
        ImageIO.write(image, "jpg",file2);
        image.flush();
        System.out.println(file2.length());
    }
}

After the operation, I found the size is 4788268 and 1529534. So I can't understand why the size of the picture is small.

OverDrive
  • 27
  • 5

1 Answers1

0

You can try to do it with FileInputStream and FileOutputStream classes:

public class SeamCarving {
    static String pathFrom = "C:/Users/lenovo/Desktop/h4/01.jpg";
    static String pathTo = "d:/02.jpg";

    public static void main(String[] args) throws Exception {
        File file1 = new File(pathFrom);
        File file2 = new File(pathTo);

        FileInputStream fis = new FileInputStream(pathFrom);
        FileOutputStream fos = new FileOutputStream(pathTo);

        System.out.println(file1.length());
        int buffSize;
        byte[] buffer = new byte[1024];
        while ((buffSize = fis.read(buffer)) > 0) {
            fos.write(buffer, 0, buffSize);
        }
        System.out.println(file2.length());

        fis.close();
        fos.close();
    }
}
DimaSan
  • 12,264
  • 11
  • 65
  • 75
  • Yes, I know this way will not change the size of the picture, but I need to use the ImageIO class, because I am writing the algorithm of seam carving which will related to the operation of the pixel. So I need to use this way to save the picture. – OverDrive Oct 20 '16 at 08:05
  • I see, then please read [this post](http://stackoverflow.com/questions/29273672/image-size-getting-decreased-after-converting-it-into-byte-using-bufferedimage). Output image compresses with 0.75 ratio, so the size reduces accordingly. – DimaSan Oct 20 '16 at 08:07
  • oh, I see. Thank you for your answer! – OverDrive Oct 20 '16 at 08:16