Java examples for 2D Graphics:BufferedImage Effect
get BufferedImage Histogram
/*// ww w .ja va 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 int[] getHistogram(BufferedImage bufImg) { // Set up the parameters for the Histogram object. int[] bins = { 256, 256, 256 }; // The number of bins. double[] low = { 0.0D, 0.0D, 0.0D }; // The low value. double[] high = { 256.0D, 256.0D, 256.0D }; // The high value. // Construct the Histogram object. Histogram hist = new Histogram(bins, low, high); // Create the parameter block. ParameterBlock pb = new ParameterBlock(); pb.addSource(bufImg); // Specify the source image pb.add(null); // No ROI pb.add(1); // Sampling pb.add(1); // periods // Perform the histogram operation. PlanarImage output = (PlanarImage) JAI .create("histogram", pb, null); // Retrieve the histogram data. hist = (Histogram) output.getProperty("histogram"); // Print 3-band histogram. int[] mean = new int[] { (int) hist.getMean()[0], (int) hist.getMean()[1], (int) hist.getMean()[2] }; System.out.println("histogram2=" + mean[0] + "," + mean[1] + "," + mean[2]); return mean; } public static int[] getMean(BufferedImage srcImg) { int h = srcImg.getHeight(); int w = srcImg.getWidth(); BufferedImage dstImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); double red = 0; double green = 0; double blue = 0; double count = 0.0; for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { int srcPixel = srcImg.getRGB(x, y); // System.out.print(srcPixel); int thisRed = new Color(srcPixel).getRed(); int thisGreen = new Color(srcPixel).getGreen(); int thisBlue = new Color(srcPixel).getBlue(); if (thisRed > 0 || thisGreen > 0 || thisBlue > 0) { // tally total colors for 3 components red = red + thisRed; green = green + thisGreen; blue = blue + thisBlue; count++; } } } // get averages int redAvg = (int) Math.round(red / count); int greenAvg = (int) Math.round(green / count); int blueAvg = (int) Math.round(blue / count); // System.out.println("color mean=" + redAvg + "," + greenAvg + "," // + blueAvg + ",count=" + count); return new int[] { redAvg, greenAvg, blueAvg }; } public static int[] getMean(ArrayList colorList) { double red = 0; double green = 0; double blue = 0; int count = 0; for (int i = 0; i < colorList.size(); i++) { Color c = (Color) colorList.get(i); int thisRed = c.getRed(); int thisGreen = c.getGreen(); int thisBlue = c.getBlue(); // System.out.println(thisRed); if (thisRed > 0 || thisGreen > 0 || thisBlue > 0) { // tally total colors for 3 components red = red + thisRed; green = green + thisGreen; blue = blue + thisBlue; count++; } } int redAvg = (int) (red / count); int greenAvg = (int) (green / count); int blueAvg = (int) (blue / count); // System.out.println("color mean=" + redAvg + "," + greenAvg + "," // + blueAvg + ",count=" + count); return new int[] { redAvg, greenAvg, blueAvg }; } }