2010-12-26 16 views
5

Potrzebuję wykonać przycinanie obrazu java i zmienić rozmiar bez serwera X.Bezgłowe powiększanie obrazu JAVA

Próbowałem kilku metod. poniżej pierwsza metoda działa, ale generuje dość brzydki obraz o zmienionym rozmiarze (prawdopodobnie stosując algorytm najbliższego sąsiada do zmiany rozmiaru:

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

Więc postanowiłem wykorzystać bicubic rozmiaru, który daje lepsze wyniki:

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

To działało w porządku, dopóki nie próbował umieścić go na serwerze, i w tym momencie wpadłem na java.awt.HeadlessException. moje próby do zabawy java.awt.headless = true nie powiodło się.

Więc, tutaj jest pytanie: Jak zmienić rozmiar i przyciąć i obrazu w Javie bez serwera X, przy użyciu algorytmu bicubic interpolation?

Odpowiedź: Korzystanie kod z Bozho komentarzu, stworzyłem tę funkcję, która załatwia sprawę (interpolacja należy 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; 
} 

Odpowiedz

3

Sprawdź this code. Sprawdź również, czy Image.getScaledInstance(..) (z "gładkim" skalowaniem) nie rozwiązuje problemu. I na koniec spójrz na java-image-scaling-library.

+0

która działa, dziękuję! aktualizowanie mojego pytania z ostateczną wersją funkcji zmiany rozmiaru. –