Java BufferedImage Trim trimImage(BufferedImage image)

Here you can find the source of trimImage(BufferedImage image)

Description

Trims the given image so that there is no leading white/transparent block.

License

Open Source License

Parameter

Parameter Description
image - Image to trim

Return

trimmed image

Declaration

private static Image trimImage(BufferedImage image) 

Method Source Code

//package com.java2s;
/**//from w w w  .  j av  a2s. c o m
 * This file is part of VoteBox.
 * 
 * VoteBox is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as published by
 * the Free Software Foundation.
 * 
 * You should have received a copy of the GNU General Public License
 * along with VoteBox, found in the root of any distribution or
 * repository containing all or part of VoteBox.
 * 
 * THIS SOFTWARE IS PROVIDED BY WILLIAM MARSH RICE UNIVERSITY, HOUSTON,
 * TX AND IS PROVIDED 'AS IS' AND WITHOUT ANY EXPRESS, IMPLIED OR
 * STATUTORY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, WARRANTIES OF
 * ACCURACY, COMPLETENESS, AND NONINFRINGEMENT.  THE SOFTWARE USER SHALL
 * INDEMNIFY, DEFEND AND HOLD HARMLESS RICE UNIVERSITY AND ITS FACULTY,
 * STAFF AND STUDENTS FROM ANY AND ALL CLAIMS, ACTIONS, DAMAGES, LOSSES,
 * LIABILITIES, COSTS AND EXPENSES, INCLUDING ATTORNEYS' FEES AND COURT
 * COSTS, DIRECTLY OR INDIRECTLY ARISING OUR OF OR IN CONNECTION WITH
 * ACCESS OR USE OF THE SOFTWARE.
 */

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;

public class Main {
    /**
     * Trims the given image so that there is no leading white/transparent block.
     * 
     * @param image - Image to trim
     * @return trimmed image
     */
    private static Image trimImage(BufferedImage 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;
            out: for (int x = 1; x < image.getWidth(); x++) {
                for (int y = 0; y < image.getHeight(); y++) {
                    int i = y * image.getWidth() + x;
                    int pixel = pix[i];

                    int alpha = (pixel >> 24) & 0xff;
                    int red = (pixel >> 16) & 0xff;
                    int green = (pixel >> 8) & 0xff;
                    int blue = (pixel) & 0xff;

                    if (alpha == 0)
                        continue;
                    if (red == 255 && green == 255 && blue == 255)
                        continue;

                    break out;
                }//for
                lastClearRow = x;
            }//for

            return image.getSubimage(lastClearRow, 0, image.getWidth()
                    - lastClearRow, image.getHeight());
        } catch (InterruptedException e) {
            return image;
        }
    }
}

Related

  1. trim(BufferedImage image, int trimTop, int trimLeft, int trimBottom, int trimRight)
  2. trim(BufferedImage image, Rectangle trimRect)
  3. trim(BufferedImage img)
  4. trim(final BufferedImage img)
  5. trimAroundCenter(BufferedImage img, Point center, Color bgColor)
  6. trimImage(BufferedImage imageToTrim)
  7. trimImageHorizontally(BufferedImage image, boolean trimFromEnd, int maxToTrim)
  8. trimImageVerticallyHelper(BufferedImage image, int maxToTrim)
  9. trimImg(BufferedImage img, Color backgroundColor)