【问题标题】:ImageIO save picture and the size gets small [duplicate]ImageIO保存图片并且尺寸变小[重复]
【发布时间】:2016-10-20 15:59:14
【问题描述】:

我尝试使用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。 所以我不明白为什么图片的尺寸很小。

【问题讨论】:

    标签: java javax.imageio


    【解决方案1】:

    您可以尝试使用 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();
        }
    }
    

    【讨论】:

    • 是的,我知道这种方式不会改变图片的大小,但是我需要使用ImageIO类,因为我正在编写与像素操作相关的缝雕刻算法.所以我需要用这种方式保存图片。
    • 我明白了,那么请阅读this post。输出图像以 0.75 比例压缩,因此尺寸相应减小。
    • 哦,我明白了。谢谢你的回答!
    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-23
    相关资源
    最近更新 更多