Java Utililty Methods BufferedImage Trim

List of utility methods to do BufferedImage Trim

Description

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

Method

BufferedImagetrim(BufferedImage image)
Trim excess whitespace from the given image
Rectangle clip = getClipRectangle(image, 0, 0);
return crop(image, clip);
BufferedImagetrim(BufferedImage image)
trim
int len;
int x;
int y;
if (image.getWidth() > image.getHeight()) {
    len = image.getHeight();
    x = (image.getWidth() - len) / 2;
    y = 0;
} else {
...
BufferedImagetrim(BufferedImage image, int trimTop, int trimLeft, int trimBottom, int trimRight)
trim
int width = image.getWidth();
int height = image.getHeight();
return image.getSubimage(trimLeft, trimTop, width - trimLeft - trimRight, height - trimTop - trimBottom);
BufferedImagetrim(BufferedImage image, Rectangle trimRect)
trim
return image.getSubimage(trimRect.x, trimRect.y, trimRect.width, trimRect.height);
BufferedImagetrim(BufferedImage img)
Removes the white borders of a BufferedImage .
final int width = img.getWidth();
final int height = img.getHeight();
if (width == 0 || height == 0)
    return img;
int topY = Integer.MAX_VALUE;
int topX = Integer.MAX_VALUE;
int bottomY = Integer.MIN_VALUE;
int bottomX = Integer.MIN_VALUE;
...
BufferedImagetrim(final BufferedImage img)
Trims white margins from a BufferedImage .
int xMin = -1, xMax = -1, yMin = -1, yMax = -1;
final int w = img.getWidth(), h = img.getHeight();
x1: for (int x = 0; x < w; x++) {
    for (int y = 0; y < h; y++) {
        if (!isWhite(img, x, y)) {
            xMin = x;
            break x1;
x2: for (int x = w; x-- > 0;) {
    for (int y = 0; y < h; y++) {
        if (!isWhite(img, x, y)) {
            xMax = x;
            break x2;
y1: for (int y = 0; y < h; y++) {
    for (int x = xMin; x <= xMax; x++) {
        if (!isWhite(img, x, y)) {
            yMin = y;
            break y1;
y2: for (int y = h; y-- > 0;) {
    for (int x = xMin; x <= xMax; x++) {
        if (!isWhite(img, x, y)) {
            yMax = y;
            break y2;
return img.getSubimage(xMin, yMin, xMax - xMin + 1, yMax - yMin + 1);
BufferedImagetrimAroundCenter(BufferedImage img, Point center, Color bgColor)
trim Around Center
int width = img.getWidth(null);
width = 2 * Math.max(center.x, (width - center.x));
int height = img.getHeight(null);
height = 2 * Math.max(center.y, (height - center.y));
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bi.createGraphics();
g2.setColor(bgColor);
g2.fillRect(0, 0, width, height);
...
ImagetrimImage(BufferedImage image)
Trims the given image so that there is no leading white/transparent block.
try {
    int[] pix = new int[image.getWidth() * image.getHeight()];
    PixelGrabber grab = new PixelGrabber(image, 0, 0, image.getWidth(), image.getHeight(), pix, 0,
            image.getWidth());
    if (!grab.grabPixels())
        return image;
    int lastClearRow = 0;
    out: for (int x = 1; x < image.getWidth(); x++) {
...
BufferedImagetrimImage(BufferedImage imageToTrim)
This method trims the input image and returns it as a BufferedImage
int y1 = trimLockup(imageToTrim);
int y2 = trimLockdown(imageToTrim, y1);
int x1 = 0;
int x2 = imageToTrim.getWidth();
return imageToTrim.getSubimage(x1, y1, x2 - x1, y2 - y1);
BufferedImagetrimImageHorizontally(BufferedImage image, boolean trimFromEnd, int maxToTrim)
Trims the given image so that there is no trailing white/transparent block.
BufferedImage outImage;
if (trimFromEnd) {
    outImage = flipImageHorizontally(image);
    outImage = trimImageHorizontallyHelper(outImage, maxToTrim);
    outImage = flipImageHorizontally(outImage);
else
    outImage = trimImageHorizontallyHelper(image, maxToTrim);
...