-2

是这样的:

public Color colorMoreTimesRepeated()
{    


}

而且我不知道如何创建一个变量来计算不同的颜色并将重复次数更多的变量返回给我。

这个想法是计算图像的所有颜色并给出重复次数更多的颜色,我尝试使用 *2 旅程和 for 并且当任何颜色重复时它开始计数,最后它返回一个更重复。

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

1 回答 1

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());
}
于 2013-05-06T09:05:28.753 回答