30行代码解析滑块验证码图片缺口的位置
有这样一种图片验证码
话不多说,直接上代码:
/// <summary>
/// 获取滑块图片水平缺口的位置
/// </summary>
/// <param name="bitmapA"></param>
/// <returns></returns>
public static int GetDiffPixel(Bitmap bitmapA)
{
for (int w = 0; w < bitmapA.Width - 10; w++)
{
int iDiffPix = 0;
int iDiffPixMax = 0;
for (int h = 0; h < bitmapA.Height; h++)
{
Color piexl1 = bitmapA.GetPixel(w, h);
bool isCoherentH = true;
for (int i = 0; i < 10; i++)
{
Color piexl2 = bitmapA.GetPixel(w + i, h);
if (Math.Sqrt(3 * Math.Pow(Convert.ToDouble(piexl1.R) - Convert.ToDouble(piexl2.R), 2) + 4 * Math.Pow(Convert.ToDouble(piexl1.G) - Convert.ToDouble(piexl2.G), 2) + 2 * Math.Pow(Convert.ToDouble(piexl1.B) - Convert.ToDouble(piexl2.B), 2)) > 1)
{
isCoherentH = false;
break;
}
}
if (isCoherentH)
{
iDiffPix++;
}
else
{
iDiffPixMax = Math.Max(iDiffPix, iDiffPixMax);
iDiffPix = 0;
}
}
if (iDiffPixMax > 29)
return w - 10;
}
return 0;
}