Java BufferedImage Trim trimImageHorizontally(BufferedImage image, boolean trimFromEnd, int maxToTrim)

Here you can find the source of trimImageHorizontally(BufferedImage image, boolean trimFromEnd, int maxToTrim)

Description

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

License

Open Source License

Parameter

Parameter Description
image image to be evaluated for trimming
trimFromEnd whether the image should have whitespace at the end removed instead of that at the front
maxToTrim the maximum amount of whitespace to trim, necessary for uniform scaling later on

Return

trimmed image

Declaration


public static BufferedImage trimImageHorizontally(BufferedImage image, boolean trimFromEnd, int maxToTrim) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;

public class Main {
    /**/*from w  w w  .  j av a2s  .c om*/
     * Trims the given image so that there is no trailing white/transparent block.
     *
     * @param image             image to be evaluated for trimming
     *
     * @param trimFromEnd       whether the image should have whitespace at the end removed instead of
     *                          that at the front
     *
     * @param maxToTrim         the maximum amount of whitespace to trim, necessary for uniform scaling
     *                          later on
     *
     * @return                  trimmed image
     *
     * @see tap.BallotImageHelper
     */

    public static BufferedImage trimImageHorizontally(BufferedImage image, boolean trimFromEnd, int maxToTrim) {

        BufferedImage outImage;

        /* Check if we need to trim from the end or the front */
        if (trimFromEnd) {

            /* Flip the image*/
            outImage = flipImageHorizontally(image);

            /* Trim the image */
            outImage = trimImageHorizontallyHelper(outImage, maxToTrim);

            /* Flip it back */
            outImage = flipImageHorizontally(outImage);

        } /* Otherwise, just trim it normally */
        else
            outImage = trimImageHorizontallyHelper(image, maxToTrim);

        return outImage;
    }

    /**
     * A method which will invert an image with respect to its y-axis
     *
     * @param image     image to be flipped
     * @return          a flipped image
     */
    public static BufferedImage flipImageHorizontally(BufferedImage image) {

        /* Create a new clean image of the same size/type */
        BufferedImage flipped = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());

        /* Instantiate Affine transformation for flipping and translating */
        AffineTransform tran = AffineTransform.getTranslateInstance(image.getWidth(), 0);
        AffineTransform flip = AffineTransform.getScaleInstance(-1d, 1d);

        /* Merge these */
        tran.concatenate(flip);

        /* Creates a Graphics2D object linked  */
        Graphics2D g = flipped.createGraphics();

        /* Set the transformation on the graphic */
        g.setTransform(tran);

        /* Draw the image onto the graphic */
        g.drawImage(image, 0, 0, null);

        /* Now dispose of the graphic */
        g.dispose();

        /* Return the flipped image */
        return flipped;
    }

    /**
     * A helper method which actually trims an image. This method trims columns.
     *
     * @param image             image to be trimmed
     * @param maxToTrim         the maximum whitespace that can be trimmed off this image
     * @return                  a trimmed image
     */
    private static BufferedImage trimImageHorizontallyHelper(BufferedImage image, int maxToTrim) {

        try {

            /* Create an one dimensional pixel array to store the image pixels */
            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 lastClearColumn = 0;

            int x = 1;
            int y;

            /* Image is transparent*/
            int alpha = 0;

            /* Image is white (RGB) */
            int red = 255;
            int green = 255;
            int blue = 255;

            /* Iterate over the width of the image */
            while (x < image.getWidth()) {

                /* Reset the height counter */
                y = 0;

                /* Check whether the image is transparent or white if yes perform the trim operation until the entire height of the image */
                while (y < image.getHeight() && ((alpha == 0) || (red == 255 && green == 255 && blue == 255))) {

                    int i = y * image.getWidth() + x;
                    int pixel = pix[i];
                    alpha = (pixel >> 24) & 0xff;
                    red = (pixel >> 16) & 0xff;
                    green = (pixel >> 8) & 0xff;
                    blue = (pixel) & 0xff;
                    y++;
                }

                /* Executed only when the entire column has been checked*/
                if (y == image.getHeight())
                    lastClearColumn = x;
                x++;
            }

            int trimmable = Math.min(lastClearColumn, maxToTrim);
            /* Trim the image horizontally */
            return image.getSubimage(trimmable, 0, image.getWidth() - trimmable, image.getHeight());
        } catch (InterruptedException e) {
            return image;
        }
    }
}

Related

  1. trim(BufferedImage img)
  2. trim(final BufferedImage img)
  3. trimAroundCenter(BufferedImage img, Point center, Color bgColor)
  4. trimImage(BufferedImage image)
  5. trimImage(BufferedImage imageToTrim)
  6. trimImageVerticallyHelper(BufferedImage image, int maxToTrim)
  7. trimImg(BufferedImage img, Color backgroundColor)
  8. trimLockdown(BufferedImage Img, int y1)
  9. trimLockup(BufferedImage Img)