Java 2D 图像调整大小忽略双三次/双线性插值渲染提示(OS X + linux) [英] Java 2D Image resize ignoring bicubic/bilinear interpolation rendering hints (OS X + linux)

查看:28
本文介绍了Java 2D 图像调整大小忽略双三次/双线性插值渲染提示(OS X + linux)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Image Voodoo 插件在 JRuby/Rails 应用程序中为上传的图像创建缩略图 - 问题是调整后的缩略图看起来像......屁股.

I'm trying to create thumbnails for uploaded images in a JRuby/Rails app using the Image Voodoo plugin - the problem is the resized thumbnails look like... ass.

似乎生成缩略图的代码完全正确地将插值渲染提示设置为双三次",但在我们的开发环境(OS X)或生产网络服务器上并没有兑现它们(Linux).

It seems that the code to generate the thumbnails is absolutely doing everything correctly to set the interpolation rendering hint to "bicubic", but it isn't honoring them on our dev environment (OS X), or on the production web server (Linux).

我已经提取了生成缩略图的代码,将其重写为一个直接的 Java 应用程序(即从 main() 方法开始),并将插值渲染提示显式设置为bicubic",并复制了 (缺乏)双三次和双线性调整大小.

I've extracted out the code to generate the thumbnails, rewritten it as a straight Java app (ie kicked off from a main() method) with the interpolation rendering hint explicitly set to "bicubic", and have reproduced the (lack of) bicubic and bilinear resizing.

正如预期的那样,在 OS X 和 Linux 上,缩略图很难看且像素化,但在 Windows 上,它使用双三次插值很好地调整了图像大小.

As expected on both OS X and Linux the thumbanils are ugly and pixelated, but on Windows, it resizes the images nicely with bicubic interpolation used.

是否有任何 JVM 环境设置和/或其他库使我无法使其正常工作?为了这个,我做了很多头撞墙.

Is there any JVM environment setting and/or additional libraries that I'm missing to make it work? I'm doing a lot of banging of head against wall for this one.

推荐答案

我意识到这个问题是在不久前提出的,但以防万一其他人仍然遇到这个问题.

I realize this question was asked a while ago, but incase anyone else is still running into this.

缩略图看起来像屁股的原因有两个(主要是第一个):

The reason the thumbnails look like ass are caused by two things (primarily the first one):

  • Java 中的非增量图像缩放非常粗糙,无论渲染提示如何,都会抛出大量像素数据并平均一次结果.
  • 在 Java2D(通常是 GIF)中处理支持不佳的 BufferedImage 类型可能会导致非常糟糕的外观/抖动结果.

事实证明旧的 AreaAveragingScaleFilter 在制作好看的缩略图方面做得不错,但它很慢并且被 Java2D 团队弃用——不幸的是,他们没有用任何很好的开箱即用的替代品来代替它,这让我们有点不知所措自己的.

As it turns out the old AreaAveragingScaleFilter does a decent job of making good looking thumbnails, but it is slow and deprecated by the Java2D team -- unfortunately they didn't replace it with any nice out-of-the-box alternative and left us sort of on our own.

Chris Campbell(来自 Java2D 团队)在几年前用增量缩放的概念解决了这个问题——不是在一次操作中从起始分辨率到目标分辨率,而是分步进行,结果看起来好多了.

Chris Campbell (from the Java2D team) addressed this a few years ago with the concept of incremental scaling -- instead of going from your starting resolution to the target resolution in one operation, you do it in steps, and the result looks much better.

鉴于此代码相当大,我将所有最佳实践都写进了一个名为 imgscalr 并在 Apache 2 许可下发布.

Given that the code for this is decently large, I wrote all the best-practices up into a library called imgscalr and released it under the Apache 2 license.

最基本的用法如下:

BufferedImage img = ImageIO.read(...); // load image
BufferedImage scaledImg = Scalr.resize(img, 640);

在这个用例中,库使用所谓的自动"缩放模式,并将生成的图像(尊重它的比例)放入 640x640 的边界框内.因此,如果图像不是正方形而是标准的 4:3 图像,它会将其大小调整为 640x480 -- 参数只是它的最大尺寸.

In this use-case the library uses what is called it's "automatic" scaling mode and will fit the resulting image (honoring it's proportions) within a bounding box of 640x640. So if the image is not a square and is a standard 4:3 image, it will resize it to 640x480 -- the argument is just it's largest dimension.

Scalr 上有许多其他方法类(都是静态的且易于使用),让您可以控制一切.

There are a slew of other methods on the Scalr class (all static and easy to use) that allow you to control everything.

为了获得尽可能最好看的缩略图,命令看起来像这样:

For the best looking thumbnails possible, the command would look like this:

BufferedImage img = ImageIO.read(...); // load image
BufferedImage scaledImg = Scalr.resize(img, Method.QUALITY, 
                                       150, 100, Scalr.OP_ANTIALIAS);

Scalr.OP_ANTIALIAS 是可选的,但很多用户觉得当你在 Java 中缩小到足够小的缩略图时,像素值之间的一些过渡有点过于离散,使图像看起来锐利",所以很多用户要求一种稍微软化缩略图的方法.

The Scalr.OP_ANTIALIAS is optional, but a lot of users feel that when you scale down to a small enough thumbnail in Java, some of the transitions between pixel values are a little too discrete and make the image look "sharp", so a lot of users asked for a way to soften the thumbnail a bit.

这是通过 ConvolveOp 如果您以前从未使用过它们,那么尝试找出要使用的正确内核"是……麻烦.OP_ANTIALIAS 常量定义在类上,这是我在与另一位将 imgscalr 部署到巴西社交网络(用于缩放个人资料照片)的用户进行了一周的测试后发现的最好看的抗锯齿操作.我加入它是为了让每个人的生活更轻松.

That is done through a ConvolveOp and if you have never used them before, trying to figure out the right "kernel" to use is... a pain in the ass. That OP_ANTIALIAS constant defined on the class it the best looking anti-aliasing op I found after a week of testing with another user who had deployed imgscalr into their social network in Brazil (used to scale the profile photos). I included it to make everyone's life a bit easier.

此外,在所有这些示例之上,您可能已经注意到,当您缩放 GIF 和其他一些类型的图像 (BMP) 时,有时缩放的结果与原始图像相比看起来很糟糕……这是因为图像在一种支持不佳的 BufferedImage 类型和 Java2D 回退到使用它的软件渲染管道而不是硬件加速的管道以获得更好的支持图像类型.

Also, ontop of all these examples, you might have noticed when you scale GIFs and some other types of images (BMPs) that sometimes the scaled result looks TERRIBLE compared to the original... that is because of the image being in a poorly supported BufferedImage type and Java2D falling back to using it's software rendering pipeline instead of the hardware accelerated one for better supported image types.

imgscalr 会为您处理所有这些,并尽可能将图像保持在最佳支持的图像类型中以避免这种情况发生.

imgscalr will take care of all of that for you and keep the image in the best supported image type possible to avoid that.

无论如何,这是说您可以使用 imgscalr 为您完成所有工作而无需担心任何事情"的长篇大论.

Anyway, that is a REALLY long way of saying "You can use imgscalr to do all that for you and not have to worry about anything".

这篇关于Java 2D 图像调整大小忽略双三次/双线性插值渲染提示(OS X + linux)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆