8

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;
}

そこで、より良い結果が得られるバイキュービックサイズ変更を使用することにしました。

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;
}
4

1 に答える 1

4

このコードを確認してください。また、Image.getScaledInstance(..)(「スムーズな」スケーリングで)問題が解決しないかどうかを確認します。そして最後に、java-image-scaling-libraryを見てください。

于 2010-12-26T17:18:03.750 に答える