Here you can find the source of findDominantColor(BufferedImage paramBufferedImage)
public static Color findDominantColor(BufferedImage paramBufferedImage)
//package com.java2s; //License from project: Apache License import java.awt.Color; import java.awt.Rectangle; import java.awt.image.BufferedImage; public class Main { public static Color findDominantColor(BufferedImage paramBufferedImage) { return findDominantColor(paramBufferedImage, false, -1); }//from w w w . ja v a2 s .c o m public static Color findDominantColor(BufferedImage paramBufferedImage, boolean getBoundaryColor, int boundThickness) { // margin here indicates boundary thickness int margin = boundThickness; int totRed = 0; int totGreen = 0; int totBlue = 0; int totAlpha = 0; int imgWt = paramBufferedImage.getWidth(), imgHt = paramBufferedImage.getHeight(); Rectangle north = null, south = null, west = null, east = null; if (margin > 0) { north = new Rectangle(0, 0, imgWt, margin); west = new Rectangle(0, 0, margin, imgHt); south = new Rectangle(0, imgHt - margin, imgWt, margin); east = new Rectangle(imgWt - margin, 0, margin, imgHt); } for (int irow = 0; irow < imgHt; irow++) { for (int icol = 0; icol < imgWt; icol++) { if (getBoundaryColor && margin > 0) { boolean validPixels = false; if (north.contains(icol, irow) || south.contains(icol, irow) || west.contains(icol, irow) || east.contains(icol, irow)) { validPixels = true; } if (!validPixels) continue; } if (paramBufferedImage.getRGB(icol, irow) == 0) totAlpha++; else { Color pixelColor = new Color(paramBufferedImage.getRGB(icol, irow)); totRed += pixelColor.getRed(); totGreen += pixelColor.getGreen(); totBlue += pixelColor.getBlue(); } } } int totPixels = (imgHt * imgWt - totAlpha); if (getBoundaryColor) totPixels = 2 * (imgHt * margin) + 2 * (imgWt * margin) - totAlpha; int red = totRed / totPixels, green = totGreen / totPixels, blue = totBlue / totPixels; Color localColor2 = new Color(red, green, blue); return localColor2; } }