Java ByteBuffer Size adaptImageSize(final BufferedImage img, final int width, final int height)

Here you can find the source of adaptImageSize(final BufferedImage img, final int width, final int height)

Description

Creates a new image with a changed image size if it doesn't match the given width and height

License

Open Source License

Parameter

Parameter Description
img The original image
width The width that the image should have
height The height that the image should have

Return

A BufferedImage copy of the original image that has been adjusted to the given width and height

Declaration

protected static BufferedImage adaptImageSize(final BufferedImage img, final int width, final int height) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Color;
import java.awt.Graphics;

import java.awt.image.BufferedImage;

public class Main {
    /**/*from  www  .j  av a2 s  .  co m*/
     * Creates a new image with a changed image size if it doesn't match the given width and height
     * @param img The original image
     * @param width The width that the image should have
     * @param height The height that the image should have
     * @return A BufferedImage copy of the original image that has been adjusted to the given width and height
     */
    protected static BufferedImage adaptImageSize(final BufferedImage img, final int width, final int height) {
        if (img.getWidth() != width || img.getHeight() != height) {
            return increaseImageSize(img, width, height);
        }

        return img;
    }

    /**
     * Increases an images width and height, the old image will be in the top left corner of the new image; the rest
     * will be transparent black
     * 
     * @param img The original image
     * @param width The width of the new image
     * @param height The height of the new image
     * @return A new BufferedImage that holds the original image in the top left corner and the rest is filled with black
     */
    protected static BufferedImage increaseImageSize(final BufferedImage img, final int width, final int height) {
        final BufferedImage newImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        final Graphics g = newImg.createGraphics();

        g.setColor(new Color(0, 0, 0, 0));
        g.fillRect(0, 0, width, height);

        g.drawImage(img, 0, 0, null);
        g.dispose();
        return newImg;
    }
}

Related

  1. addToStringBuilder(StringBuilder sb, ByteBuffer buffer, int size)
  2. calcSize(ByteBuffer buffer)
  3. check(ByteBuffer buffer, int size)
  4. checkBounds(BufferedImage image, int xStart, int yStart, int width, int height)