Bufferedimage resize

2019-01-08 20:30发布

I am trying to resized a bufferedimage. I am able to store it and show up on a jframe no problems but I can't seem to resize it. Any tips on how I can change this to make it work and show the image as a 200*200 file would be great

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

7条回答
别忘想泡老子
2楼-- · 2019-01-08 20:36

Check this out, it helps:

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);
查看更多
在下西门庆
3楼-- · 2019-01-08 20:37

If all that is required is to resize a BufferedImage in the resize method, then the Thumbnailator library can do that fairly easily:

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

The above code will resize the img to fit the dimensions of newW and newH while maintaining the aspect ratio of the original image.

If maintaining the aspect ratio is not required and resizing to exactly the given dimensions is required, then the forceSize method can be used in place of the size method:

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

Using the Image.getScaledInstance method will not guarantee that the aspect ratio of the original image will be maintained for the resized image, and furthermore, it is in general very slow.

Thumbnailator uses a technique to progressively resize the image which can be several times faster than Image.getScaledInstance while achieving an image quality which generally is comparable.

Disclaimer: I am the maintainer of this library.

查看更多
Anthone
4楼-- · 2019-01-08 20:39

Updated answer

I cannot recall why my original answer worked but having tested it in a separate environment, I agree, the original accepted answer doesn't work (why I said it did I cannot remember either). This, on the other hand, did work:

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;
}  
查看更多
我命由我不由天
5楼-- · 2019-01-08 20:41

Here's some code that I have used to resize bufferedimages, no frills, pretty quick:

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;
}
查看更多
虎瘦雄心在
6楼-- · 2019-01-08 20:42

This is a shortened version of what is actually happening in imgscalr, if you just want to use the "balanced" smoothing:

/**
 * Takes a BufferedImage and resizes it according to the provided targetSize
 *
 * @param src the source BufferedImage
 * @param targetSize maximum height (if portrait) or width (if landscape)
 * @return a resized version of the provided BufferedImage
 */
private BufferedImage resize(BufferedImage src, int targetSize) {
    if (targetSize <= 0) {
        return src; //this can't be resized
    }
    int targetWidth = targetSize;
    int targetHeight = targetSize;
    float ratio = ((float) src.getHeight() / (float) src.getWidth());
    if (ratio <= 1) { //square or landscape-oriented image
        targetHeight = (int) Math.ceil((float) targetWidth * ratio);
    } else { //portrait image
        targetWidth = Math.round((float) targetHeight / ratio);
    }
    BufferedImage bi = new BufferedImage(targetWidth, targetHeight, src.getTransparency() == Transparency.OPAQUE ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = bi.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); //produces a balanced resizing (fast and decent quality)
    g2d.drawImage(src, 0, 0, targetWidth, targetHeight, null);
    g2d.dispose();
    return bi;
}
查看更多
该账号已被封号
7楼-- · 2019-01-08 20:44

try the imgscalr library. Best lib i found- very fast, good quality and simple to use

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

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

Apache 2 License

查看更多
登录 后发表回答