2012-12-15 29 views
2

我想调整大小的图像在java中。Java - 好的和免费的库来调整图像大小

到现在为止我用这个:

BufferedImage resizedImage = new BufferedImage((int) new_dim.getWidth(), (int) new_dim.getHeight(), type); 
Graphics2D g = resizedImage.createGraphics(); 
g.drawImage(originalImage, 0, 0, (int) new_dim.getWidth(), (int) new_dim.getHeight(), null); 
g.dispose(); 

与newDim作为一个维度。

但结果是质量很差......

有没有做这个简单的工作的一些商业自由库?

回答

6

Thumbnailator是一个开源的(MIT许可证)库,专业图像尺寸调整操作,尤其是在缩小图像。

例如,下面将调整一个BufferedImage

BufferedImage sourceImage = // ... 
BufferedImage resizedImage = 
    Thumbnails.of(sourceImage) 
     .size(newWidth, newHeight) 
     .asBufferedImage(); 

Thumbnailator提供了灵活的输入和输出,所以有可能混合和匹配FileBufferedImage输入和输出:

BufferedImage sourceImage = // ... 
Thumbnails.of(sourceImage) 
    .size(newWidth, newHeight) 
    .toFile(destinationFile); 

File sourceFile = // ... 
BufferedImage resizedImage = 
    Thumbnails.of(sourceFile) 
     .size(newWidth, newHeight) 
     .asBufferedImage(); 

图书馆使用技术来改善尺寸调整图像的质量,并在保持速度和质量之间的良好平衡的同时进行。 Thumbnailator项目页面有一个image quality comparison

免责声明:我是图书馆的维护者

+1

感谢您的答案,但我在调整动画gif时调整了这个问题: https://code.google.com/p/thumbnailator/issues/detail?id=30 –

+0

我也不得不放弃这个lib,因为它有麻烦透明的GIFs。 –

1

试图说服你重新考虑,至少约质量

你可以尝试用g.setRenderingHints(KEY_INTERPOLATION, VALUE_INTERPOLATION_BICUBIC);看到this

也有ImageFilters,用于锐化等。