我正在使用 Java AWT 来缩放 JPEG 图像,以创建缩略图。当图像具有正常的采样因子( 2x2,1x1,1x1 )时,代码可以正常工作
但是,具有此采样因子 ( 1x1, 1x1, 1x1 ) 的图像在缩放时会产生问题。尽管特征是可识别的,但颜色会损坏。
原文和缩略图: alt text http://otherplace.in/thumb1.jpg
我使用的代码大致相当于:
static BufferedImage awtScaleImage(BufferedImage image,
int maxSize, int hint) {
// We use AWT Image scaling because it has far superior quality
// compared to JAI scaling. It also performs better (speed)!
System.out.println("AWT Scaling image to: " + maxSize);
int w = image.getWidth();
int h = image.getHeight();
float scaleFactor = 1.0f;
if (w > h)
scaleFactor = ((float) maxSize / (float) w);
else
scaleFactor = ((float) maxSize / (float) h);
w = (int)(w * scaleFactor);
h = (int)(h * scaleFactor);
// since this code can run both headless and in a graphics context
// we will just create a standard rgb image here and take the
// performance hit in a non-compatible image format if any
Image i = image.getScaledInstance(w, h, hint);
image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawImage(i, null, null);
g.dispose();
i.flush();
return image;
}
(代码由本页提供)
有一个更好的方法吗?
这是一个采样因子为 [ 1x1, 1x1, 1x1 ]的测试图像。