我正在使用Java AWT缩放JPEG图像,以创建缩略图。当图像具有正常的采样因子(2x2,1x1,1x1)时,代码可以正常工作

但是,具有此采样因子(1x1,1x1,1x1)的图像在缩放时会产生问题。尽管可以识别这些功能,但颜色仍然损坏。

original和缩略图:
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;
}


(代码由this page提供)

有一个更好的方法吗?

这是一个采样率为[1x1,1x1,1x1]的test image

最佳答案

我相信问题不在于缩放,而是在构造BufferedImage时使用不兼容的颜色模型(“图像类型”)。

用Java创建像样的缩略图非常困难。这是detailed discussion

10-08 13:35