java - How can I do a method that return the Color that is more times repeated? -


is like:

public color colormoretimesrepeated() {       } 

and don't know how make variable count different colors , return me 1 repeated more times.

the idea count colors of image , give color repeated more times, have tried using *2 journeys , when anycolor repeated begins count , @ end return 1 more repeated.

   *for(int i=0;i< high;i++){        for(int j=0;j<wide;j++){* 

from question, understand want identify color filling maximum number of pixels in given image.

if right, can use following method!

private static color getcoloroccuringmaxtimesinimage(file imagefile) throws ioexception {     bufferedimage image = imageio.read(imagefile);     int width = image.getwidth();     int height = image.getheight();      map<integer, integer> colors = new hashmap<integer, integer>();      int maxcount = 0;     integer maxcolor = 0;      (int x = 0; x < width; x++)     {         (int y = 0; y < height; y++)         {             integer color = image.getrgb(x, y);             integer count = colors.get(color);             if (count == null)                 count = 0;              integer next = count + 1;             colors.put(color, next);              if (next > maxcount)             {                 maxcount = next;                 maxcolor = color;             }         }     }      return new color(maxcolor.intvalue()); } 

Comments