Java Utililty Methods BufferedImage Resize

List of utility methods to do BufferedImage Resize

Description

The list of methods to do BufferedImage Resize are organized into topic(s).

Method

ImagedownsizeImage(Image imgSource, int w, int h)

Resizes the given image using a Graphics2D object backed by a BufferedImage object.

BufferedImage imgResized = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2dResized = imgResized.createGraphics();
g2dResized.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
        RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
g2dResized.drawImage(imgSource, 0, 0, w, h, null);
g2dResized.dispose();
return imgResized;
BufferedImagefitImage(JLabel label, BufferedImage image)
To resize the image to fit in the JLabel, and set it to the JLabel
int newWidth = label.getWidth();
int newHeight = label.getHeight();
if (newWidth < 1 || newHeight < 1) {
    return null;
BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(image, 0, 0, newWidth, newHeight, null);
...
BufferedImagegetResizedImage(BufferedImage image)
get Resized Image
if (image == null) {
    return null;
if (image.getWidth() != image.getHeight()) {
    BufferedImage nImage = new BufferedImage(Math.max(image.getWidth(), image.getHeight()),
            Math.max(image.getWidth(), image.getHeight()), 2);
    nImage.getGraphics().drawImage(image, nImage.getWidth() / 2 - image.getWidth() / 2,
            nImage.getHeight() / 2 - image.getHeight() / 2, null);
...
floatgetResizeFactor(BufferedImage source, int maxWidth, int maxHeight)
get Resize Factor
if (source == null) {
    return 1;
float wfactor = maxWidth == 0 ? 0 : ((float) maxWidth) / source.getWidth();
float hfactor = maxHeight == 0 ? 0 : ((float) maxHeight) / source.getHeight();
float factor = 1;
if (wfactor != 0 && hfactor != 0) {
    factor = Math.min(1, Math.min(wfactor, hfactor));
...
BufferedImageimageResize(BufferedImage image, int width, int height)
Code taken from a stackoverflow answer http://stackoverflow.com/a/14550112/1696114
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT);
Graphics2D g2d = (Graphics2D) bi.createGraphics();
g2d.addRenderingHints(
        new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
g2d.drawImage(image, 0, 0, width, height, null);
g2d.dispose();
return bi;
BufferedImagelinearResizeImage(BufferedImage origin, int width, int height)
linear Resize Image
BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = resizedImage.createGraphics();
float xScale = (float) width / origin.getWidth();
float yScale = (float) height / origin.getHeight();
AffineTransform at = AffineTransform.getScaleInstance(xScale, yScale);
g.drawRenderedImage(origin, at);
g.dispose();
return resizedImage;
...
BufferedImageresizeToContainer(BufferedImage imagen, int wCont, int hCont)
Redimensiona una imagen para ajustarla a un contenedor
widthImage = new ImageIcon(imagen).getIconWidth();
heightImage = new ImageIcon(imagen).getIconHeight();
double scaleW, scaleH; 
if (widthImage > wCont && heightImage > hCont) {
    scaleH = (hCont * 1.0) / heightImage;
    scaleW = scaleH;
    imagen = getScaledImage(imagen, scaleW, scaleH);
    setImageDimension(imagen);
...