2010-12-26 10 views
5

मुझे जावा छवि फसल करने और एक्स सर्वर के बिना आकार बदलने की आवश्यकता है।जावा हेडलेस बाइबिक छवि का आकार

मैंने कई तरीकों की कोशिश की। पहली विधि नीचे काम करता है, लेकिन (एक काफी बदसूरत resized छवि आउटपुट शायद आकार बदलने के लिए निकटतम पड़ोसी कलन विधि का उपयोग:

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

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 आकार है, जो बेहतर परिणाम देता है का उपयोग करने का निर्णय लिया

यह ठीक काम किया जब तक मैं सर्वर पर डालने की कोशिश की है, और इस बिंदु पर मैं में java.awt.HeadlessException। java.awt.headless साथ खेलने के लिए मेरी प्रयास टकरा = सच में विफल रहा है।

तो, यहां प्रश्न है: एक बाइबिक इंटरपोलेशन एल्गोरिदम का उपयोग करते हुए, मैं एक्स सर्वर के बिना जावा में आकार और फसल और छवि का आकार कैसे बदलूं?

उत्तर: 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

जो काम करता है, धन्यवाद! आकार बदलने के अंतिम संस्करण के साथ मेरा प्रश्न अपडेट कर रहा है। –

संबंधित मुद्दे