get Average Color from BufferedImage - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Color

Description

get Average Color from BufferedImage

Demo Code


import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class Main{
    /**// w  w  w. ja v  a2 s .c  om
     * 
     * @param b
     *            the BufferedImage to search it.
     * @return the color of the most common in the given BufferedImage.
     */
    public static Color getAverage(BufferedImage b) {
        Color c[][] = ImageUtil.getColors(b);
        int[] tot = new int[3];
        int x = 0;
        for (Color[] d : c) {
            for (Color e : d) {
                x++;
                tot[0] += e.getRed();
                tot[1] += e.getGreen();
                tot[2] += e.getBlue();
            }
        }
        return new Color(tot[0] / x, tot[1] / x, tot[2] / x);
    }
}

Related Tutorials