【问题标题】:Image resize not reducing kiloBytes of the image - using imgscalr-lib-3.1图像调整大小不减少图像的千字节 - 使用 imgscalr-lib-3.1
【发布时间】:2011-08-09 09:31:12
【问题描述】:

我正在使用 imgscalr-lib-3.1 创建缩略图,以在我的 Galleria-classic 相册中使用。 它正在减小大小(我的意思是像 100 * 100 的像素),但不是千字节(像 10K、20K)。 这是我的代码:

    public static void createThumbNails(String path) throws ImageFormatException, IOException{

    File image ;
    FileInputStream in = null ;
    JPEGImageDecoder decoder;
    BufferedImage bufferedImage ;
    BufferedImage outBufferedImage ;
    Scalr scalr = new Scalr();
    File folder = new File(path+"/images/album/award/");
    File[] files = folder.listFiles();  
    File outputfile ;



    for(File file : files){
        image = file;
        in = new FileInputStream(file);
        decoder = JPEGCodec.createJPEGDecoder(in);
        bufferedImage = decoder.decodeAsBufferedImage();
        outBufferedImage = scalr.resize(bufferedImage, Scalr.Method.SPEED,100);


        ImageIO.write(outBufferedImage, "jpg", file);
    }

    in.close();

}

你们知道该怎么做吗? 谢谢。

【问题讨论】:

  • 快速提示 Dave,imgscalr 使用所有静态方法,因此无需实例化它的实例——我应该将构造函数设为私有。对混乱感到抱歉。只需调用 Scalr.resize(...) 就是预期用途。

标签: java thumbnails resize scaletransform scalr


【解决方案1】:

您将缩略图写入您从中读取原始图像的相同文件中。这可能不是你想要的。

如果你想要这样,但是你需要在读取输入文件后删除或截断输入文件,以确保你不会用缩略图覆盖前几个字节,留下其余部分包含旧图像数据。

截断文件的一种简单方法是使用RandomAccessFile

RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.setLength(0);
raf.close();

【讨论】:

  • ImageIO.write(RenderedImage, String, File) 将替换任何现有文件,所以这显然不是问题。
  • @jarnbjo : joachim 是对的!我更改了目标文件夹,这成功了,问题解决了,给他打分:D ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-04
  • 1970-01-01
  • 2019-10-24
  • 1970-01-01
  • 2021-07-28
  • 2013-02-14
相关资源
最近更新 更多