我曾试图转换,该版的《 Java成典代码,该图画从图像中获取主要颜色,但迄今没有成功。 我对<代码>colorCount &color
变量有错误。 我不知道这些变量所用的适当和对应数据类型。 我的守则是:
public string dominantColor(Bitmap img)
{
int[] colorCount = new int[0];
int maxCount = 0;
string dominantColor = "";
// data is an array of a series of 4 one-byte values representing the rgba values of each pixel
Bitmap Bmp = new Bitmap(img);
BitmapData BmpData = Bmp.LockBits(new Rectangle(0, 0, Bmp.Width, Bmp.Height), ImageLockMode.ReadOnly, Bmp.PixelFormat);
byte[] data = new byte[BmpData.Stride * Bmp.Height];
for (int i = 0; i < data.Length; i += 4)
{
// ignore transparent pixels
if (data[i+3] == 0)
continue;
string color = data[i] + "." + data[i+1] + "," + data[i+2];
// ignore white
if (color == "255,255,255")
continue;
if (colorCount[color] != 0)
colorCount[color] = colorCount[color] + 1;
else
colorCount[color] = 0;
// keep track of the color that appears the most times
if (colorCount[color] > maxCount)
{
maxCount = colorCount[color];
dominantColor = color.ToString;
}
}
string rgb = dominantColor.Split(",");
return rgb;
}