ImageIO保存图片,尺寸变小

ImageIO save picture and the size gets small

我尝试使用class ImageIO 保存图片,但我发现图片尺寸会变小。代码是这样的:

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());
    }
}

运行后发现大小为4788268和1529534。 所以搞不懂为什么图片尺寸小

您可以尝试使用 FileInputStreamFileOutputStream 类:

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();
    }
}