Auto crops an image. - Java 2D Graphics

Java examples for 2D Graphics:Image

Description

Auto crops an image.

Demo Code

/**/*w w  w.ja  v  a  2 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.
 */
//package com.java2s;
import java.awt.*;
import java.awt.image.*;

public class Main {
    private static final int COLOR_WHITE = Color.WHITE.getRGB();
    private static final int margin = 10;

    /**
     * Auto crops an image.
     * @param source
     * @return 
     */
    public static BufferedImage autoCrop(BufferedImage source) {
        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 (source.getRGB(x, y) != COLOR_WHITE) {
                    minY = y;
                    break lable1;
                }
            }
        }

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

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

        lable4: for (int x = width - 1; x >= minX; x--) {
            for (int y = minY; y < maxY; y++) {
                if (source.getRGB(x, y) != COLOR_WHITE) {
                    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;
        }

        // if same size, return the original
        if (minX == 0 && minY == 0 && maxX == width && maxY == height) {
            return source;
        }

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

        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;
    }
}

Related Tutorials