Java BufferedImage Crop autoCrop(BufferedImage source, double tolerance)

Here you can find the source of autoCrop(BufferedImage source, double tolerance)

Description

Auto crops an image.

License

Apache License

Parameter

Parameter Description
source a parameter
tolerance range from 0.0 to 0.5

Declaration

public static BufferedImage autoCrop(BufferedImage source, double tolerance) 

Method Source Code

//package com.java2s;
/**//  w  w w . ja v  a2  s  .  co m
 * Copyright @ 2008
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import java.awt.*;
import java.awt.image.*;

public class Main {
    private static final int margin = 10;

    /**
     * Auto crops an image.
     *
     * @param source
     * @param tolerance range from 0.0 to 0.5
     * @return
     */
    public static BufferedImage autoCrop(BufferedImage source, double tolerance) {
        // Get top-left pixel color as the "baseline" for cropping
        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;

        // Immediately break the loops when encountering a non-white pixel.
        lable1: for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if (colorWithinTolerance(baseColor, source.getRGB(x, y), tolerance)) {
                    minY = y;
                    break lable1;
                }
            }
        }

        lable2: for (int x = 0; x < width; x++) {
            for (int y = minY; y < height; y++) {
                if (colorWithinTolerance(baseColor, source.getRGB(x, y), tolerance)) {
                    minX = x;
                    break lable2;
                }
            }
        }

        // Get lower-left pixel color as the "baseline" for cropping
        baseColor = source.getRGB(minX, height - 1);

        lable3: for (int y = height - 1; y >= minY; y--) {
            for (int x = minX; x < width; x++) {
                if (colorWithinTolerance(baseColor, source.getRGB(x, y), tolerance)) {
                    maxY = y;
                    break lable3;
                }
            }
        }

        lable4: for (int x = width - 1; x >= minX; x--) {
            for (int y = minY; y < maxY; y++) {
                if (colorWithinTolerance(baseColor, source.getRGB(x, y), tolerance)) {
                    maxX = x;
                    break lable4;
                }
            }
        }

        if ((minX - margin) >= 0) {
            minX -= margin;
        }

        if ((minY - margin) >= 0) {
            minY -= margin;
        }

        if ((maxX + margin) < width) {
            maxX += margin;
        }

        if ((maxY + margin) < height) {
            maxY += margin;
        }

        int newWidth = maxX - minX + 1;
        int newHeight = maxY - minY + 1;

        // if same size, return the original
        if (newWidth == width && newHeight == height) {
            return source;
        }

        BufferedImage target = new BufferedImage(newWidth, newHeight, source.getType());

        Graphics g = target.getGraphics();
        g.drawImage(source, 0, 0, target.getWidth(), target.getHeight(), minX, minY, maxX + 1, maxY + 1, null);

        g.dispose();

        return target;
    }

    /**
     * Determines color distance.
     * http://stackoverflow.com/questions/10678015/how-to-auto-crop-an-image-white-border-in-java
     *
     * @param a a RGB value
     * @param b a RGB value
     * @param tolerance
     * @return
     */
    private static boolean colorWithinTolerance(int a, int b, double tolerance) {
        int aAlpha = (int) ((a & 0xFF000000) >>> 24); // Alpha level
        int aRed = (int) ((a & 0x00FF0000) >>> 16); // Red level
        int aGreen = (int) ((a & 0x0000FF00) >>> 8); // Green level
        int aBlue = (int) (a & 0x000000FF); // Blue level

        int bAlpha = (int) ((b & 0xFF000000) >>> 24); // Alpha level
        int bRed = (int) ((b & 0x00FF0000) >>> 16); // Red level
        int bGreen = (int) ((b & 0x0000FF00) >>> 8); // Green level
        int bBlue = (int) (b & 0x000000FF); // Blue level

        double distance = Math.sqrt((aAlpha - bAlpha) * (aAlpha - bAlpha) + (aRed - bRed) * (aRed - bRed)
                + (aGreen - bGreen) * (aGreen - bGreen) + (aBlue - bBlue) * (aBlue - bBlue));

        // 510.0 is the maximum distance between two colors 
        // (0,0,0,0 -> 255,255,255,255)
        double percentAway = distance / 510.0d;

        return (percentAway > tolerance);
    }
}

Related

  1. adjustRectToFitImage(BufferedImage image, Rectangle cropRect)
  2. autoCrop(BufferedImage source)
  3. crop(BufferedImage image, Rectangle clip)
  4. crop(BufferedImage input, int x, int y, int width, int height)
  5. crop(BufferedImage original, int newSize)
  6. crop(BufferedImage source, File to, int x1, int y1, int x2, int y2)