Java BufferedImage Transparent cutTransparentBorder(BufferedImage src)

Here you can find the source of cutTransparentBorder(BufferedImage src)

Description

Remove transparent border around an image.

License

LGPL

Parameter

Parameter Description
src the source image

Return

the cut new image

Declaration

public static BufferedImage cutTransparentBorder(BufferedImage src) 

Method Source Code


//package com.java2s;
/*// w w  w.ja  v a2  s . com
 * Copyright 2008-2014, David Karnok 
 * The file is part of the Open Imperium Galactica project.
 * 
 * The code should be distributed under the LGPL license.
 * See http://www.gnu.org/licenses/lgpl.html for details.
 */

import java.awt.image.BufferedImage;

public class Main {
    /**
     * Remove transparent border around an image.
     * @param src the source image
     * @return the cut new image
     */
    public static BufferedImage cutTransparentBorder(BufferedImage src) {
        int top = 0;
        int left = 0;
        int bottom = 0;
        int right = 0;

        topcheck: for (int y = 0; y < src.getHeight(); y++) {
            for (int x = 0; x < src.getWidth(); x++) {
                int c = src.getRGB(x, y);
                if ((c & 0xFF000000) != 0) {
                    break topcheck;
                }
            }
            top++;
        }
        leftcheck: for (int x = 0; x < src.getWidth(); x++) {
            for (int y = 0; y < src.getHeight(); y++) {
                int c = src.getRGB(x, y);
                if ((c & 0xFF000000) != 0) {
                    break leftcheck;
                }
            }
            left++;
        }
        bottomcheck: for (int y = src.getHeight() - 1; y >= top; y--) {
            for (int x = 0; x < src.getWidth(); x++) {
                int c = src.getRGB(x, y);
                if ((c & 0xFF000000) != 0) {
                    break bottomcheck;
                }
            }
            bottom++;
        }
        rightcheck: for (int x = src.getWidth() - 1; x >= left; x--) {
            for (int y = 0; y < src.getHeight(); y++) {
                int c = src.getRGB(x, y);
                if ((c & 0xFF000000) != 0) {
                    break rightcheck;
                }
            }
            right++;
        }

        return subimage(src, left, top, src.getWidth() - right - left, src.getHeight() - bottom - top);
    }

    /**
     * Returns a subimage of the given main image. If the sub-image
     * tends to be smaller in area then the original image, a new buffered image is returned instead of the
     * shared sub image
     * @param src the source image.
     * @param x the x coordinate
     * @param y the y coordinate
     * @param w the width
     * @param h the height
     * @return the extracted sub-image
     */
    public static BufferedImage subimage(BufferedImage src, int x, int y, int w, int h) {
        return src.getSubimage(x, y, w, h);
    }
}

Related

  1. ApplyTransparency(BufferedImage image, Image mask)
  2. applyTransparency(BufferedImage src, float alpha)
  3. bordersNonTransparentPixel(BufferedImage data, int wid, int hei, boolean[] traced, int x, int y)
  4. ConvToTransparentImage(BufferedImage src, float alpha)
  5. fixTransparency(String var0, BufferedImage var1)
  6. makeTransparency(BufferedImage image, int color)
  7. makeTranspBufferedImage(Image image)
  8. nonTransparentPixels(BufferedImage image)