Java 2D的图像调整大小忽略双立方/双线性插值呈现提示(OS X + LINUX) [英] Java 2D Image resize ignoring bicubic/bilinear interpolation rendering hints (OS X + linux)

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

问题描述

我试图创建一个使用影像巫毒插件缩略图上传的图片中的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.

看来,code生成缩略图绝对做的一切正确设置插值呈现提示为双三次,但它没有履行他们在我们的开发环境(OS X),或在生产Web服务器(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).

我已经提取了code生成缩略图,改写它作为一个直接的Java应用程序(即由一个main()方法中拉开序幕)与插值呈现提示明确设置为双三次,并有转载(缺少)双立方和双线性调整大小。

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的thumbanils是丑陋和像素化,但在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中
  • 非增量图像缩放非常粗糙,引发了大量像素数据出来,平均的结果,一旦不管呈现提示的。
  • 在处理的支持较差的BufferedImage类型的Java2D(通常为GIF格式),可导致很差寻找/抖动结果。

事实证明老 AreaAveragingScaleFilter这做制作好看的缩略图一份体面的工作,但它是缓慢的,由Java2D的团队pcated德$ P $ - 不幸的是,他们没有与任何漂亮的即开即用现成的替代取代它给我们留下排序对我们自己的。

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.

克里斯·坎贝尔(从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.

鉴于code这是体面的大,我写的所有最佳实践上升到一个名为的 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.

最基本的用法是这样的:

The most basic usage looks like this:

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

在此用例库采用的是所谓它的自动缩放模式,将适合生成的图像(履行它的比例)内的640x640边界框。因此,如果图像不是方形,是一个标准的4:3的图像,将其调整为640×480 - 的说法就是它的最大尺寸

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.

此外,ontop的所有这些例子,你可能当你扩大GIF和一些其他类型的图像(骨形成蛋白),有时比原来经过换算的结果看起来很可怕......那是因为在图像被发现支持不佳的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天全站免登陆