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

BufferedImagetrimImageVerticallyHelper(BufferedImage image, int maxToTrim)
A helper method which actually trims an image.
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;
    int x;
...
BufferedImagetrimImg(BufferedImage img, Color backgroundColor)
trim Img
int imgHeight = img.getHeight();
int imgWidth = img.getWidth();
int startWidth = 0;
for (int x = 0; x < imgWidth; x++) {
    if (startWidth == 0) {
        for (int y = 0; y < imgHeight; y++) {
            if (img.getRGB(x, y) != backgroundColor.getRGB()) {
                startWidth = x;
...
inttrimLockdown(BufferedImage Img, int y1)
This method reads the input image from the input from start pixel height (y1) until it reads the first next row where all pixel are white by height and return that value
for (int j = y1 + 1; j < Img.getHeight(); j++) {
    int counterWhite = 0;
    for (int i = 0; i < Img.getWidth(); i++) {
        if (Img.getRGB(i, j) == -1) {
            counterWhite++;
    if (counterWhite == Img.getWidth()) {
...
inttrimLockup(BufferedImage Img)
This method reads the image pixels until it reads the first black pixel by height and then returns that value
for (int j = 0; j < Img.getHeight(); j++) {
    for (int i = 0; i < Img.getWidth(); i++) {
        if (Img.getRGB(i, j) == -16777216) {
            return j;
return 0;
...
BufferedImagetrimmedImage(BufferedImage source)
Trims the transparent pixels from the given BufferedImage (returns a sub-image).
final int minAlpha = 1;
final int srcWidth = source.getWidth();
final int srcHeight = source.getHeight();
Raster raster = source.getRaster();
int l = srcWidth, t = srcHeight, r = 0, b = 0;
int alpha, x, y;
int[] pixel = new int[4];
for (y = 0; y < srcHeight; y++) {
...
BufferedImagetrimWhiteSpace(BufferedImage img)
Trims the white border around an image.
WritableRaster raster = getGrayscaleImage(img).getRaster();
int[] pixels = new int[Math.max(raster.getWidth(), raster.getHeight())];
int thresholdWhite = 253;
int trimTop = 0, trimBottom = 0, trimLeft = 0, trimRight = 0;
boolean white = true;
while (white) {
    raster.getPixels(0, trimTop, raster.getWidth(), 1, pixels);
    for (int i = 0; i < raster.getWidth(); i++) {
...