Java headless (headless)双三次图像调整大小

标签 java image-processing awt headless

我需要在没有 X 服务器的情况下执行 java 图像裁剪和调整大小。

我尝试了几种方法。 下面的第一种方法有效,但输出了一个相当难看的调整大小的图像(可能使用最近邻算法来调整大小:

static BufferedImage createResizedCopy(Image originalImage, int scaledWidth, int scaledHeight, boolean preserveAlpha)
{
    int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
    BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, imageType);
    Graphics2D g = scaledBI.createGraphics();
    if (preserveAlpha)
    {
        g.setComposite(AlphaComposite.Src);
    }
    g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null);
    g.dispose();
    return scaledBI;
}

所以我决定使用bicubic调整大小,这会提供更好的结果:

public static BufferedImage createResizedCopy(BufferedImage source, int destWidth, int destHeight, Object interpolation)
{
    if (source == null) throw new NullPointerException("source image is NULL!");
    if (destWidth <= 0 && destHeight <= 0) throw new IllegalArgumentException("destination width & height are both <=0!");
    int sourceWidth = source.getWidth();
    int sourceHeight = source.getHeight();
    double xScale = ((double) destWidth) / (double) sourceWidth;
    double yScale = ((double) destHeight) / (double) sourceHeight;
    if (destWidth <= 0)
    {
        xScale = yScale;
        destWidth = (int) Math.rint(xScale * sourceWidth);
    }
    if (destHeight <= 0)
    {
        yScale = xScale;
        destHeight = (int) Math.rint(yScale * sourceHeight);
    }
    GraphicsConfiguration gc = getDefaultConfiguration();
    BufferedImage result = gc.createCompatibleImage(destWidth, destHeight, source.getColorModel().getTransparency());
    Graphics2D g2d = null;
    try
    {
        g2d = result.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, interpolation);
        AffineTransform at = AffineTransform.getScaleInstance(xScale, yScale);
        g2d.drawRenderedImage(source, at);
    }
    finally
    {
        if (g2d != null) g2d.dispose();
    }
    return result;
}

public static GraphicsConfiguration getDefaultConfiguration()
{
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();
    return gd.getDefaultConfiguration();
}

在我尝试将它放在服务器上之前,它工作正常,此时我遇到了 java.awt.HeadlessException。 我尝试使用 java.awt.headless=true 失败了。

那么问题来了: 如何在没有 X 服务器的情况下使用双三次插值算法在 Java 中调整大小、裁剪和图像?

答案: 使用 Bozho 评论中的代码,我创建了这个函数来实现这个目的(插值应该是 RenderingHints.VALUE_INTERPOLATION_*)。

public static BufferedImage createResizedCopy(BufferedImage source, int destWidth, int destHeight, Object interpolation)
{
    BufferedImage bicubic = new BufferedImage(destWidth, destHeight, source.getType());
    Graphics2D bg = bicubic.createGraphics();
    bg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, interpolation);
    float sx = (float)destWidth / source.getWidth();
    float sy = (float)destHeight / source.getHeight();
    bg.scale(sx, sy);
    bg.drawImage(source, 0, 0, null);
    bg.dispose();
    return bicubic;
}

最佳答案

检查 this code .还要检查 Image.getScaledInstance(..)(使用“平滑”缩放)是否不能解决问题。最后,看看 java-image-scaling-library .

关于Java headless (headless)双三次图像调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4534680/

相关文章:

java - 如何在单击按钮时显示文本

java - NoSuchMethodError 错误创建 session 工厂

java - Freemarker 循环似乎没有按顺序打印

ios - 使用 vImageHistogramCalculation 计算图像直方图的问题

c - 如何测量图像中的环境光水平?

java - 为什么我收到错误: cannot find symbol for JComponent?

java - GridBagLayout 困难

java - 添加第二列与第一列中的重复元素

java - 查询没有实体类的数据库表

c++ - 在灰度 (B/W) 图像上绘制彩色点