2013-05-05 18 views
-2

是一樣的東西:我該如何做一個返回更多次重複的顏色的方法?

public Color colorMoreTimesRepeated() 
{  


} 

,我不知道如何使那些重要的不同的顏色,並返回我被重複多次的一個變量。

這個想法是計算一個圖像的所有顏色,並給予更多次重複的顏色,我已經嘗試使用* 2旅程,並且當anycolor重複時,它開始計數,並在最後返回更重複的那個。

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

你試過了什麼?你究竟想要什麼?閱讀你的問題,並嘗試去思考,除了你自己之外,任何人都可以理解。 – sashkello 2013-05-05 23:24:48

+0

你如何定義顏色?是Color.WHITE等還是它具有各種rgb值的顏色? – 2013-05-05 23:29:22

+0

您可以在圖像中找到的所有rgb值 – user2352968 2013-05-05 23:33:34

回答

0

從你的問題,我明白,你想確定填充給定圖像的最大像素數的顏色。

如果我是對的,你可以使用下面的方法!

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; 

    for (int x = 0; x < width; x++) 
    { 
     for (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()); 
} 
相關問題