Here you can find the source of cutTransparentBorder(BufferedImage src)
Parameter | Description |
---|---|
src | the source image |
public static BufferedImage cutTransparentBorder(BufferedImage src)
//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); } }