2012-02-23 294 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 2013-02-18 02:46:24

+0

我使用的例子是從我的一個工作示例中提取的,但是,這不會按原樣編譯。我現在就更新它。 – Ocracoke 2013-02-18 12:18:10

+6

仍然不安全 - 在我的Macbook上,例如,我收到了ClassCastException - 這不是保證轉換。 – I82Much 2013-02-18 17:08:44

15

如果所需要的只是在resize方法來調整一個BufferedImage,那麼Thumbnailator庫可以做到這一點很容易:

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

的上面的代碼將調整img的大小以適應newWnewH的尺寸,同時保持原始圖像的高寬比。

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

使用Image.getScaledInstance方法:

如果保持縱橫比不是必需的,調整到完全相同的給定尺寸是必需的,則forceSize方法可以代替size方法的使用不能保證原始圖像的寬高比將保持爲調整大小的圖像,並且此外,通常非常慢。

Thumbnailator使用一種技術逐步調整圖像的大小,該圖像可以是several times faster than Image.getScaledInstance,同時實現通常可比的圖像質量。

聲明:我是該庫的維護者。

+0

Thumbnailator真棒。! – 2016-01-21 08:52:02

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

}

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