Java examples for 2D Graphics:BufferedImage Effect
get BufferedImage Average Point
/*/*from w ww. ja v a 2 s . c o m*/ * (C) Copyright 2000-2011, by Scott Preston and Preston Research LLC * * Project Info: http://www.scottsbots.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ import java.awt.Color; import java.awt.Image; import java.awt.Point; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.awt.image.renderable.ParameterBlock; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import javax.imageio.ImageIO; import javax.media.jai.Histogram; import javax.media.jai.JAI; import javax.media.jai.PlanarImage; import com.scottsbots.core.utils.Utils; public class Main{ public static Point getAvgPoint(BufferedImage srcImg) { return getAvgPoint(srcImg, 0); } public static Point getAvgPoint(BufferedImage srcImg, int threshhold) { int h = srcImg.getHeight(); int w = srcImg.getWidth(); // difference image double meanX = 0.0; double meanY = 0.0; int meanThresh = 10; int count = 0; for (int y = 0; y < h; ++y) { int rowY = 0; for (int x = 0; x < w; ++x) { int srcPixel = getGrey(srcImg.getRGB(x, y)); if (srcPixel > meanThresh) { rowY = rowY + srcPixel; meanY = meanY + y; count++; } } } if (count > 0) { meanY = meanY / count; } count = 0; for (int x = 0; x < w; ++x) { int rowX = 0; for (int y = 0; y < h; ++y) { int srcPixel = getGrey(srcImg.getRGB(x, y)); if (srcPixel > meanThresh) { rowX = rowX + srcPixel; meanX = meanX + x; count++; } } } if (count > 0) { meanX = meanX / count; } // Utils.log("x=" + meanX + ",y=" + meanY + ",count=" + count); // adding threshhold Utils.log("count=" + count); Point pt = null; if (threshhold > 0) { if (count > threshhold) { pt = new Point(new Double(meanX).intValue(), new Double( meanY).intValue()); } else { pt = new Point(w / 2, h / 2); } } else { pt = new Point(new Double(meanX).intValue(), new Double(meanY).intValue()); } return pt; } public static Color getGrey(Color color) { int r = color.getRed(); int g = color.getGreen(); int b = color.getBlue(); int gray = (int) ((r + g + b) / 3.0); return new Color(gray, gray, gray); } /** * @deprecated **/ public static int getGrey(int colorInt) { return getGrey255(colorInt); } public static int getGrey255(int c) { Color color = new Color(c); int r = color.getRed(); int g = color.getGreen(); int b = color.getBlue(); int gray = (int) ((r + g + b) / 3.0); return gray; } }