Java Utililty Methods BufferedImage Crop

List of utility methods to do BufferedImage Crop

Description

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

Method

BufferedImagecropImage(BufferedImage image, int width, int height)
Crops an image to a specified size.
if (width > 0 && height > 0) {
    BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics g = result.getGraphics();
    g.drawImage(image, 0, 0, width, height, null);
    g.dispose();
    return result;
return image;
...
BufferedImagecropImage(BufferedImage image, int x, int y, int width, int height)
Crop image
BufferedImage cropped = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
Graphics g = cropped.getGraphics();
g.drawImage(image, x, y, null);
return cropped;
BufferedImagecropImage(BufferedImage image, int x1, int y1, int x2, int y2)
Crops (returns subimage) of specified input image at specified points.
return image.getSubimage(x1, y1, x2 - x1, y2 - y1);
BufferedImagecropImage(BufferedImage src, int x, int y, int w, int h)
crop Image
BufferedImage dest = new BufferedImage(w, h, src.getType());
Graphics g = dest.getGraphics();
g.drawImage(src, 0, 0, w, h, x, y, x + w, y + h, null);
g.dispose();
return dest;
BufferedImagecropImage(BufferedImage src, int x, int y, int w, int h)
crop Image
BufferedImage dest = src.getSubimage(x, y, w, h);
return dest;
BufferedImagecropImage(final BufferedImage img, int x, int y, int w, int h, double xScale, double yScale)
crop Image
x = (int) (x * xScale);
w = (int) (w * xScale);
y = (int) (y * yScale);
h = (int) (h * yScale);
if (img.getHeight() < (y + h)) {
    h = img.getHeight() - y;
if (img.getWidth() < (x + w)) {
...
BufferedImagecropImageRelative(BufferedImage image, double leftCropFactor, double rightCropFactor, double topCropFactor, double bottomCropFactor)
crop Image Relative
int lc = (int) (leftCropFactor * image.getWidth());
int rc = (int) (rightCropFactor * image.getWidth());
int tc = (int) (topCropFactor * image.getHeight());
int bc = (int) (bottomCropFactor * image.getHeight());
BufferedImage dest = new BufferedImage(image.getWidth() - (lc + rc), image.getHeight() - (tc + bc),
        BufferedImage.TYPE_BYTE_GRAY);
Graphics g = dest.getGraphics();
g.drawImage(image, 0, 0, image.getWidth() - (lc + rc), image.getHeight() - (tc + bc), lc, tc,
...
BufferedImagecroppedCopy(BufferedImage image, int startX, int startY, int endX, int endY)
cropped Copy
int width = endX - startX;
int height = endY - startY;
BufferedImage cropped = new BufferedImage(width, height, image.getType());
Graphics2D graphics2d = cropped.createGraphics();
graphics2d.drawImage(image, 0, 0, width, height, startX, startY, endX, endY, null);
graphics2d.dispose();
return cropped;
BufferedImagecropScale(BufferedImage source, int width, int height)
This function crops the image to the aspect ratio of the width and height and then scales the image to the given values
float aspect = width / height;
BufferedImage tmp = cropToAspectRatio(source, aspect);
tmp = scale(tmp, width, height, false);
return tmp;
BufferedImagecropToAspectRatio(BufferedImage image, float aspect)
Crops a image to a specific aspect ration
int x = 0, y = 0;
int width = image.getWidth();
int height = image.getHeight();
if (width > height) {
    width = (int) (height * aspect);
    x = image.getWidth() / 2 - width / 2;
} else {
    height = (int) (width * aspect);
...