2012-02-23 11 views
30

मैं एक bufferedimage का आकार बदलने की कोशिश कर रहा हूँ। मैं इसे स्टोर करने में सक्षम हूं और jframe पर कोई समस्या नहीं दिखाता लेकिन मुझे इसका आकार बदलने का प्रतीत नहीं होता है। मैं यह कैसे बदल सकते हैं पर कोई सुझाव यह काम और छवि दिखाने के एक 200 * 200 फ़ाइल बहुत अच्छा होगा के रूप में बनाने के लिएBufferedimage आकार

private void profPic(){ 
    String path = factory.getString("bottle"); 
    BufferedImage img = ImageIO.read(new File(path)); 
} 


public static BufferedImage resize(BufferedImage img, int newW, int newH) { 
    int w = img.getWidth(); 
    int h = img.getHeight(); 
    BufferedImage dimg = new BufferedImage(newW, newH, img.getType()); 
    Graphics2D g = dimg.createGraphics(); 
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
    RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
    g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null); 
    g.dispose(); 
    return dimg; 
} 

उत्तर

39

अपडेट किया गया जवाब

मुझे याद नहीं कर सकते क्यों my original answer काम किया, लेकिन यह परीक्षण किया हो रही है एक अलग वातावरण में, मैं मानता हूं, मूल स्वीकार्य उत्तर काम नहीं करता है (मैंने ऐसा क्यों कहा कि मैंने इसे याद नहीं किया)। यह, दूसरे हाथ पर, किया काम:

public static BufferedImage resize(BufferedImage img, int newW, int newH) { 
    Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH); 
    BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB); 

    Graphics2D g2d = dimg.createGraphics(); 
    g2d.drawImage(tmp, 0, 0, null); 
    g2d.dispose(); 

    return dimg; 
} 
+2

यह संकलित नहीं करता है - getScaledInstance एक छवि को एक BufferedImage नहीं देता है। – I82Much

+0

मेरे द्वारा उपयोग किए जाने वाले उदाहरण को मेरे पास एक काम करने वाले उदाहरण से निकाला गया था, लेकिन हां, यह संकलित नहीं होगा। मैं इसे अभी अपडेट करूंगा। – Ocracoke

+6

अभी भी सुरक्षित नहीं है - मेरी मैकबुक पर, उदाहरण के लिए, मुझे क्लासकास्ट अपवाद मिलता है - यह गारंटीकृत रूपांतरण नहीं है। – I82Much

15

कि सभी आवश्यक है resize विधि में एक BufferedImage आकार बदलने के लिए है, तो Thumbnailator पुस्तकालय है कि काफी आसानी से कर सकते हैं:

public static BufferedImage resize(BufferedImage img, int newW, int newH) { 
    return Thumbnails.of(img).size(newW, newH).asBufferedImage(); 
} 

मूल छवि के पहलू अनुपात को बनाए रखते हुए उपरोक्त कोड और newH के आयामों को फिट करने के लिए img का आकार बदल देगा।

public static BufferedImage resize(BufferedImage img, int newW, int newH) { 
    return Thumbnails.of(img).forceSize(newW, newH).asBufferedImage(); 
} 

Image.getScaledInstance पद्धति का उपयोग करना:

तो पहलू अनुपात को बनाए रखने की आवश्यकता नहीं है और वास्तव में दिए गए आयामों को आकार बदलने की आवश्यकता है, तो forceSize विधि size विधि के स्थान पर इस्तेमाल किया जा सकता यह गारंटी नहीं देगा कि मूल छवि का पहलू अनुपात आकार की छवि के लिए बनाए रखा जाएगा, और इसके अलावा, यह सामान्य रूप से बहुत धीमी है।

थंबनेलेटर छवि की गुणवत्ता को प्राप्त करने के दौरान several times faster than Image.getScaledInstance हो सकता है जो आमतौर पर तुलनीय छवि गुणवत्ता प्राप्त करने के दौरान छवि का क्रमिक आकार बदलने के लिए एक तकनीक का उपयोग करता है।

अस्वीकरण: मैं इस पुस्तकालय का रखरखाव हूं।

+0

थंबनेलेटर भयानक। –

3

इस वर्ग के एक फ़ाइल से आकार बदलने और प्रारूप नाम:

import java.awt.Image; 
import java.awt.image.BufferedImage; 
import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Iterator; 
import javax.imageio.ImageIO; 
import javax.imageio.ImageReader; 
import javax.imageio.ImageWriter; 
import javax.imageio.stream.ImageInputStream; 
import org.apache.commons.io.IOUtils; 

public class ImageResizer { 

public static void main(String as[]) throws IOException{ 

    File f = new File("C:/Users/samsungrob/Desktop/shuttle.jpg"); 

    byte[] ba = resize(f, 600, 600); 

    IOUtils.write(ba, new FileOutputStream(new File("C:/Users/samsungrob/Desktop/shuttle_resized.jpg"))); 

} 




public static byte[] resize(File file, 
          int maxWidth, int maxHeight) throws IOException{ 
    int scaledWidth = 0, scaledHeight = 0; 

    BufferedImage img = ImageIO.read((ImageInputStream) file); 

    scaledWidth = maxWidth; 
    scaledHeight = (int) (img.getHeight() * ((double) scaledWidth/img.getWidth())); 

    if (scaledHeight> maxHeight) { 
     scaledHeight = maxHeight; 
     scaledWidth= (int) (img.getWidth() * ((double) scaledHeight/ img.getHeight())); 

     if (scaledWidth > maxWidth) { 
      scaledWidth = maxWidth; 
      scaledHeight = maxHeight; 
     } 
    } 

    Image resized = img.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_SMOOTH); 

    BufferedImage buffered = new BufferedImage(scaledWidth, scaledHeight, Image.SCALE_REPLICATE); 

    buffered.getGraphics().drawImage(resized, 0, 0 , null); 

    String formatName = getFormatName(file) ; 

    ByteArrayOutputStream out = new ByteArrayOutputStream(); 

    ImageIO.write(buffered, 
      formatName, 
      out); 

    return out.toByteArray(); 
} 


private static String getFormatName(ImageInputStream iis) { 
    try { 

     // Find all image readers that recognize the image format 
     Iterator iter = ImageIO.getImageReaders(iis); 
     if (!iter.hasNext()) { 
      // No readers found 
      return null; 
     } 

     // Use the first reader 
     ImageReader reader = (ImageReader)iter.next(); 

     // Close stream 
     iis.close(); 

     // Return the format name 
     return reader.getFormatName(); 
    } catch (IOException e) { 
    } 

    return null; 
} 

private static String getFormatName(File file) throws IOException { 
    return getFormatName(ImageIO.createImageInputStream(file)); 
} 

private static String getFormatName(InputStream is) throws IOException { 
    return getFormatName(ImageIO.createImageInputStream(is)); 
} 

}

2

imgscalr पुस्तकालय का प्रयास करें। बेस्ट lib मैं

BufferedImage thumbnail = Scalr.resize(image, 150); 

http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/

उपयोग करने के लिए बहुत तेजी से, अच्छी गुणवत्ता और सरल found- अपाचे 2 लाइसेंस

+1

धन्यवाद! सही काम करता है! – Harsha

0

इस चेक बाहर है, यह मदद करता है:

BufferedImage bImage = ImageIO.read(new File(C:\image.jpg); 

BufferedImage thumbnail = Scalr.resize(bImage, Scalr.Method.SPEED, Scalr.Mode.FIT_TO_WIDTH, 
             750, 150, Scalr.OP_ANTIALIAS); 
1

यहां कुछ कोड है जिसे मैंने bufferedimages का आकार बदलने के लिए उपयोग किया है, कोई फ्रिल्स नहीं, बहुत तेज़:

public static BufferedImage scale(BufferedImage src, int w, int h) 
{ 
    BufferedImage img = 
      new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 
    int x, y; 
    int ww = src.getWidth(); 
    int hh = src.getHeight(); 
    int[] ys = new int[h]; 
    for (y = 0; y < h; y++) 
     ys[y] = y * hh/h; 
    for (x = 0; x < w; x++) { 
     int newX = x * ww/w; 
     for (y = 0; y < h; y++) { 
      int col = src.getRGB(newX, ys[y]); 
      img.setRGB(x, y, col); 
     } 
    } 
    return img; 
} 
संबंधित मुद्दे