Java 我如何才能使用一种方法返回重复次数更多的颜色?
有点像:Java 我如何才能使用一种方法返回重复次数更多的颜色?,java,count,Java,Count,有点像: public Color colorMoreTimesRepeated() { } 我不知道如何创建一个变量来计算不同的颜色,并返回一个重复次数更多的变量 其思想是计算图像的所有颜色,并给出重复次数更多的颜色,我尝试使用*2行程for,当重复任何颜色时,它开始计数,最后返回重复次数更多的颜色 *for(int i=0;i< high;i++){ for(int j=0;j<wide;j++){* 从您的问题中,我了解到您想要识别填充给定图
public Color colorMoreTimesRepeated()
{
}
我不知道如何创建一个变量来计算不同的颜色,并返回一个重复次数更多的变量
其思想是计算图像的所有颜色,并给出重复次数更多的颜色,我尝试使用*2行程for,当重复任何颜色时,它开始计数,最后返回重复次数更多的颜色
*for(int i=0;i< high;i++){
for(int j=0;j<wide;j++){*
从您的问题中,我了解到您想要识别填充给定图像中最大像素数的颜色 如果我是对的,你可以使用以下方法
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());
}
你试过什么?你到底想要什么?阅读你的问题,试着想想除了你自己,其他人是否能理解。你如何定义颜色?是颜色、白色等,还是具有各种rgb值的颜色?图像中可以找到的所有rgb值