2010-12-26 7 views
5

私はJavaの画像の切り抜きを実行し、Xサーバなしでサイズを変更する必要があります。Javaのヘッドレスバイキュービック画像のサイズ変更

私はいくつかの方法を試しました。 作品下の第一の方法、おそらくリサイズのための最近傍アルゴリズムを使用して、かなり醜いリサイズした画像(出力:

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(); 
} 
:だから私は良い結果を与える bicubicリサイズを、使用することを決めた
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; 
} 

これはサーバ上に置くまではうまくいきましたが、この時点で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; 
} 

答えて

3

チェックthis code。また、Image.getScaledInstance(..)(「スムーズ」スケーリングで)問題を解決できない場合もチェックしてください。最後に、java-image-scaling-libraryをご覧ください。

+0

ありがとうございました! 私の質問は、最終版のresize関数で更新しています。 –

関連する問題