2010-12-26 45 views
5

Tôi cần phải thực hiện cắt xén hình ảnh java và thay đổi kích cỡ mà không cần máy chủ X.Hình ảnh bicubic Java không đầu đổi kích thước

Tôi đã thử một số phương pháp. phương pháp đầu tiên bên dưới hoạt động, nhưng kết quả đầu ra một hình ảnh thay đổi kích cỡ khá xấu xí (có thể sử dụng thuật toán người hàng xóm gần nhất để thay đổi kích cỡ:

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

Vì vậy, tôi quyết định sử dụng bicubic thay đổi kích thước, mà cho kết quả tốt hơn:

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

Điều này làm việc tốt cho đến khi tôi cố gắng để đặt nó trên máy chủ, và tại thời điểm này tôi va vào java.awt.HeadlessException nỗ lực của tôi để chơi với java.awt.headless = true thất bại. đây là câu hỏi: Làm cách nào để thay đổi kích thước và cắt và hình ảnh trong Java mà không cần máy chủ X, sử dụng thuật toán nội suy bicubic?

Trả lời: Sử dụng mã từ Bozho bình luận, tôi tạo ra chức năng này mà hiện các trick (nội suy nên được 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; 
} 

Trả lời

3

Kiểm tra this code. Ngoài ra, hãy kiểm tra xem Image.getScaledInstance(..) (với quy mô "mịn") không giải quyết được sự cố hay không. Và cuối cùng, hãy xem java-image-scaling-library.

+0

hoạt động, cảm ơn! cập nhật câu hỏi của tôi với phiên bản cuối cùng của chức năng thay đổi kích thước. –

Các vấn đề liên quan