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

voidadjustRectToFitImage(BufferedImage image, Rectangle cropRect)
Adjust the crop rectangle to fit within the image it is cropping.
if (cropRect.width > image.getWidth()) {
    cropRect.width = image.getWidth();
if (cropRect.height > image.getHeight()) {
    cropRect.height = image.getHeight();
int dimension = Math.min(cropRect.width, cropRect.height);
cropRect.setSize(dimension, dimension);
...
BufferedImageautoCrop(BufferedImage source)
Auto crops an image.
int width = source.getWidth();
int height = source.getHeight();
int minX = 0;
int minY = 0;
int maxX = width;
int maxY = height;
lable1: for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
...
BufferedImageautoCrop(BufferedImage source, double tolerance)
Auto crops an image.
int baseColor = source.getRGB(0, 0);
int width = source.getWidth();
int height = source.getHeight();
int minX = 0;
int minY = 0;
int maxX = width;
int maxY = height;
lable1: for (int y = 0; y < height; y++) {
...
BufferedImagecrop(BufferedImage image, Rectangle clip)
crop
BufferedImage retval = createImage(clip.width, clip.height);
Graphics2D g2 = retval.createGraphics();
g2.drawImage(image.getSubimage(clip.x, clip.y, clip.width, clip.height), 0, 0, null);
g2.dispose();
retval.flush();
return retval;
BufferedImagecrop(BufferedImage input, int x, int y, int width, int height)
crop
assert input != null;
if (width <= 0) {
    throw new IllegalArgumentException("width = " + width);
if (height <= 0) {
    throw new IllegalArgumentException("height = " + height);
BufferedImage output = new BufferedImage(width, height, input.getType());
...
BufferedImagecrop(BufferedImage original, int newSize)
crop
BufferedImage image = new BufferedImage(newSize, newSize, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.drawImage(original, 0, 0, null);
g.dispose();
return image;
voidcrop(BufferedImage source, File to, int x1, int y1, int x2, int y2)
Crop an image
try {
    String mimeType = "image/jpeg";
    if (to.getName().endsWith(".png")) {
        mimeType = "image/png";
    if (to.getName().endsWith(".gif")) {
        mimeType = "image/gif";
    int width = x2 - x1;
    int height = y2 - y1;
    BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Image croppedImage = source.getSubimage(x1, y1, width, height);
    Graphics graphics = dest.getGraphics();
    graphics.setColor(Color.WHITE);
    graphics.fillRect(0, 0, width, height);
    graphics.drawImage(croppedImage, 0, 0, null);
    ImageWriter writer = ImageIO.getImageWritersByMIMEType(mimeType).next();
    writer.setOutput(new FileImageOutputStream(to));
    IIOImage image = new IIOImage(dest, null, null);
    writer.write(null, image, createImageWriteParam(writer));
} catch (Exception e) {
    throw new RuntimeException(e);
BufferedImagecrop(BufferedImage source, int startX, int startY, int endX, int endY)
crop
int width = source.getWidth();
int height = source.getHeight();
if (startX <= -1) {
    startX = 0;
if (startY <= -1) {
    startY = 0;
if (endX <= -1) {
    endX = width - 1;
if (endY <= -1) {
    endY = height - 1;
BufferedImage result = new BufferedImage(endX, endY, source.getType());
for (int y = startY; y < endY + startY; y++) {
    for (int x = startX; x < endX + startX; x++) {
        int rgb = source.getRGB(x, y);
        result.setRGB(x - startX, y - startY, rgb);
return result;
BufferedImagecrop(BufferedImage src, int width, int height)
crop
int x = src.getWidth() / 2 - width / 2;
int y = src.getHeight() / 2 - height / 2;
BufferedImage clipping = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D area = (Graphics2D) clipping.getGraphics().create();
area.drawImage(src, 0, 0, clipping.getWidth(), clipping.getHeight(), x, y, x + clipping.getWidth(),
        y + clipping.getHeight(), null);
area.dispose();
return clipping;
...
BufferedImagecrop(BufferedImage src, int x, int y, int width, int height)
Helper methode to create a sub copy of the src input image.
BufferedImage returnImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics returnGraphics = returnImage.createGraphics();
for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
        returnGraphics.setColor(new Color(src.getRGB(x + j, y + i)));
        returnGraphics.drawLine(j, i, j, i);
return returnImage;