Java BufferedImage Operation findDominantColor(BufferedImage paramBufferedImage)

Here you can find the source of findDominantColor(BufferedImage paramBufferedImage)

Description

find Dominant Color

License

Apache License

Declaration

public static Color findDominantColor(BufferedImage paramBufferedImage) 

Method Source Code

//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;
    }
}

Related

  1. fadeImages(BufferedImage source1, BufferedImage source2, BufferedImage target, int relX, int targetX)
  2. fakeAOI(final BufferedImage pImage, final Rectangle pSourceRegion)
  3. findavg(BufferedImage bimg)
  4. findColor(BufferedImage image, int startX, int startY, int dirX, int dirY, int colorIndex)
  5. findDifference(BufferedImage img2, BufferedImage img1)
  6. findLegacyColorModel(BufferedImage image)
  7. findLegendLH(BufferedImage bufferedImage)
  8. findTranslation(AffineTransform at, BufferedImage bi)
  9. fixImage(BufferedImage img, String ext)