java - imageio - Bufferedimage调整大小



java bufferedimage用法 (5)

更新的答案

我不记得为什么我的原始答案有效,但在一个单独的环境中进行了测试,我同意,原来接受的答案不起作用(为什么我说它确实我也记不起来了)。 另一方面,这确实有效:

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

https://src-bin.com

我正在尝试调整缓冲图像的大小。 我能够存储它并显示在一个jframe没有问题,但我似乎无法调整大小。 有关如何更改此功能以使其工作并将图像显示为200 * 200文件的任何提示都会很棒

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

Answer #1

如果只需要在resize方法中调整BufferedImageresize ,那么Thumbnailator库可以相当容易地完成:

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

上面的代码将调整img大小以适应newWnewH的尺寸,同时保持原始图像的宽高比。

如果不需要保持纵横比并且需要精确调整到给定尺寸,则可以使用forceSize方法代替size方法:

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

使用Image.getScaledInstance方法不能保证为调整大小的图像保持原始图像的宽高比,而且,它通常非常慢。

Thumbnailator使用一种技术逐步调整图像大小,这可以Image.getScaledInstanceImage.getScaledInstance同时实现通常可比较的图像质量。

免责声明:我是这个库的维护者。


Answer #2

此类从文件调整大小并获取格式名称:

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

}


Answer #3

看看这个,它有帮助:

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

Answer #4

这是我用来调整缓冲图像大小的一些代码,没有多余的装饰,很快:

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