2012-02-23 261 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; 
}