Here you can find the source of trimImage(BufferedImage image)
Parameter | Description |
---|---|
image | - Image to trim |
private static Image trimImage(BufferedImage image)
//package com.java2s; /**//from w w w . j av a2s. c o m * This file is part of VoteBox. * * VoteBox is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as published by * the Free Software Foundation. * * You should have received a copy of the GNU General Public License * along with VoteBox, found in the root of any distribution or * repository containing all or part of VoteBox. * * THIS SOFTWARE IS PROVIDED BY WILLIAM MARSH RICE UNIVERSITY, HOUSTON, * TX AND IS PROVIDED 'AS IS' AND WITHOUT ANY EXPRESS, IMPLIED OR * STATUTORY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, WARRANTIES OF * ACCURACY, COMPLETENESS, AND NONINFRINGEMENT. THE SOFTWARE USER SHALL * INDEMNIFY, DEFEND AND HOLD HARMLESS RICE UNIVERSITY AND ITS FACULTY, * STAFF AND STUDENTS FROM ANY AND ALL CLAIMS, ACTIONS, DAMAGES, LOSSES, * LIABILITIES, COSTS AND EXPENSES, INCLUDING ATTORNEYS' FEES AND COURT * COSTS, DIRECTLY OR INDIRECTLY ARISING OUR OF OR IN CONNECTION WITH * ACCESS OR USE OF THE SOFTWARE. */ import java.awt.Image; import java.awt.image.BufferedImage; import java.awt.image.PixelGrabber; public class Main { /** * Trims the given image so that there is no leading white/transparent block. * * @param image - Image to trim * @return trimmed image */ private static Image trimImage(BufferedImage image) { try { 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 lastClearRow = 0; out: for (int x = 1; x < image.getWidth(); x++) { for (int y = 0; y < image.getHeight(); y++) { int i = y * image.getWidth() + x; int pixel = pix[i]; int alpha = (pixel >> 24) & 0xff; int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel) & 0xff; if (alpha == 0) continue; if (red == 255 && green == 255 && blue == 255) continue; break out; }//for lastClearRow = x; }//for return image.getSubimage(lastClearRow, 0, image.getWidth() - lastClearRow, image.getHeight()); } catch (InterruptedException e) { return image; } } }