如下内容内容是关于C#验证码识别类的内容。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices;
namespace 验证码处理 { class VerifyCode { public Bitmap bmpobj;
public VerifyCode(Bitmap pic)
{
}
private int GetGrayNumColor(System.Drawing.Color posClr)
{
}
public void GrayByPixels()
{
for (int i = 0; i < bmpobj.Height; i++)
{
for (int j = 0; j < bmpobj.Width; j++)
{
int tmpValue = GetGrayNumColor(bmpobj.GetPixel(j, i));
bmpobj.SetPixel(j, i, Color.FromArgb(tmpValue, tmpValue, tmpValue));
}
}
}
public void ClearPicBorder(int borderWidth)
{
for (int i = 0; i < bmpobj.Height; i++)
{
for (int j = 0; j < bmpobj.Width; j++)
{
if (i < borderWidth || j < borderWidth || j > bmpobj.Width - 1 - borderWidth || i > bmpobj.Height - 1 - borderWidth)
bmpobj.SetPixel(j, i, Color.FromArgb(255, 255, 255));
}
}
}
public void GrayByLine()
{
Rectangle rec = new Rectangle(0, 0, bmpobj.Width, bmpobj.Height);
IntPtr scan0 = bmpData.Scan0;
int[] pixels = new int[len];
Marshal.Copy(scan0, pixels, 0, len);
int GrayValue = 0;
for (int i = 0; i < len; i++)
{
GrayValue = GetGrayNumColor(Color.FromArgb(pixels[i]));
}
bmpobj.UnlockBits(bmpData);
}
public void GetPicValidByValue(int dgGrayValue, int CharsCount)
{
int posx1 = bmpobj.Width; int posy1 = bmpobj.Height;
int posx2 = 0; int posy2 = 0;
{
for (int j = 0; j < bmpobj.Width; j++)
{
int pixelValue = bmpobj.GetPixel(j, i).R;
{
if (posx1 > j) posx1 = j;
if (posy1 > i) posy1 = i;
if (posx2 < j) posx2 = j;
if (posy2 < i) posy2 = i;
};
};
};
if (Span < CharsCount)
{
if (posx1 > leftSpan)
posx1 = posx1 - leftSpan;
if (posx2 + Span - leftSpan < bmpobj.Width)
posx2 = posx2 + Span - leftSpan;
}
Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat);
}
public void GetPicValidByValue(int dgGrayValue)
{
int posx1 = bmpobj.Width; int posy1 = bmpobj.Height;
int posx2 = 0; int posy2 = 0;
{
for (int j = 0; j < bmpobj.Width; j++)
{
int pixelValue = bmpobj.GetPixel(j, i).R;
{
if (posx1 > j) posx1 = j;
if (posy1 > i) posy1 = i;
if (posx2 < j) posx2 = j;
if (posy2 < i) posy2 = i;
};
};
};
Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat);
}
public Bitmap GetPicValidByValue(Bitmap singlepic, int dgGrayValue)
{
int posx1 = singlepic.Width; int posy1 = singlepic.Height;
int posx2 = 0; int posy2 = 0;
{
for (int j = 0; j < singlepic.Width; j++)
{
int pixelValue = singlepic.GetPixel(j, i).R;
{
if (posx1 > j) posx1 = j;
if (posy1 > i) posy1 = i;
if (posx2 < j) posx2 = j;
if (posy2 < i) posy2 = i;
};
};
};
Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
return singlepic.Clone(cloneRect, singlepic.PixelFormat);
}
public Bitmap [] GetSplitPics(int RowNum,int ColNum)
{
if (RowNum == 0 || ColNum == 0)
return null;
int singW = bmpobj.Width / RowNum;
int singH = bmpobj.Height / ColNum;
Rectangle cloneRect;
{
for (int j = 0; j < RowNum; j++)
{
}
}
return PicArray;
}
public string GetSingleBmpCode(Bitmap singlepic, int dgGrayValue)
{
Color piexl;
string code = "";
for (int posy = 0; posy < singlepic.Height; posy++)
for (int posx = 0; posx < singlepic.Width; posx++)
{
piexl = singlepic.GetPixel(posx, posy);
code = code + "1";
else
code = code + "0";
}
return code;
}
public int GetDgGrayValue()
{
int n, n1, n2;
int k, t, q;
for (int i = 0; i < bmpobj.Width; i++)
{
for (int j = 0; j < bmpobj.Height; j++)
{
}
}
for (k = 0; k <= 255; k++)
{
total = 0;
{
q = k + t;
q = 0;
if (q > 255)
q = 255;
}
}
sum = csum = 0.0;
n = 0;
for (k = 0; k <= 255; k++)
{
}
n1 = 0;
{
{
}
}
return threshValue;
}
public void ClearNoise(int dgGrayValue, int MaxNearPoints)
{
Color piexl;
int nearDots = 0;
for (int i = 0; i < bmpobj.Width; i++)
for (int j = 0; j < bmpobj.Height; j++)
{
piexl = bmpobj.GetPixel(i, j);
if (piexl.R < dgGrayValue)
{
nearDots = 0;
{
bmpobj.SetPixel(i, j, Color.FromArgb(255, 255, 255));
}
else
{
if (bmpobj.GetPixel(i - 1, j - 1).R < dgGrayValue) nearDots++;
if (bmpobj.GetPixel(i, j - 1).R < dgGrayValue) nearDots++;
if (bmpobj.GetPixel(i + 1, j - 1).R < dgGrayValue) nearDots++;
if (bmpobj.GetPixel(i - 1, j).R < dgGrayValue) nearDots++;
if (bmpobj.GetPixel(i + 1, j).R < dgGrayValue) nearDots++;
if (bmpobj.GetPixel(i - 1, j + 1).R < dgGrayValue) nearDots++;
if (bmpobj.GetPixel(i, j + 1).R < dgGrayValue) nearDots++;
if (bmpobj.GetPixel(i + 1, j + 1).R < dgGrayValue) nearDots++;
}
if (nearDots < MaxNearPoints)
}
bmpobj.SetPixel(i, j, Color.FromArgb(255, 255, 255));
}
}
public void ClearNoise(int dgGrayValue)
{
int x, y;
byte s;
int i, j;
{
for (x = 1; x < bmpobj.Width - 1; x++)
{
p[0] = bmpobj.GetPixel(x - 1, y - 1).R;
p[1] = bmpobj.GetPixel(x, y - 1).R;
p[2] = bmpobj.GetPixel(x + 1, y - 1).R;
p[3] = bmpobj.GetPixel(x - 1, y).R;
p[4] = bmpobj.GetPixel(x, y).R;
p[5] = bmpobj.GetPixel(x + 1, y).R;
p[6] = bmpobj.GetPixel(x - 1, y + 1).R;
p[7] = bmpobj.GetPixel(x, y + 1).R;
p[8] = bmpobj.GetPixel(x + 1, y + 1).R;
for (j = 0; j < 5; j++)
{
for (i = j + 1; i < 9; i++)
{
if (p[j] > p[i])
{
s = p[j];
p[j] = p[i];
p[i] = s;
}
}
}
}
}
}
public void ErosionPic(int dgGrayValue, int nMode, bool[,] structure)
{
int lWidth = bmpobj.Width;
int lHeight = bmpobj.Height;
Bitmap newBmp = new Bitmap(lWidth, lHeight);
if (nMode == 0)
{
for (j = 0; j < lHeight; j++)
{
for (i = 1; i < lWidth - 1; i++)
{
newBmp.SetPixel(i, j, Color.Black);
if (bmpobj.GetPixel(i - 1, j).R > dgGrayValue ||
bmpobj.GetPixel(i, j).R > dgGrayValue ||
bmpobj.GetPixel(i + 1, j).R > dgGrayValue)
newBmp.SetPixel(i, j, Color.White);
}
}
}
else if (nMode == 1)
{
for (j = 1; j < lHeight - 1; j++)
{
for (i = 0; i < lWidth; i++)
{
newBmp.SetPixel(i, j, Color.Black);
if (bmpobj.GetPixel(i, j - 1).R > dgGrayValue ||
bmpobj.GetPixel(i, j).R > dgGrayValue ||
bmpobj.GetPixel(i, j + 1).R > dgGrayValue)
newBmp.SetPixel(i, j, Color.White);
}
}
}
else
{
return;
for (j = 1; j < lHeight - 1; j++)
{
for (i = 1; i < lWidth - 1; i++)
{
newBmp.SetPixel(i, j, Color.Black);
for (m = 0; m < 3; m++)
{
for (n = 0; n < 3; n++)
{
if (!structure[m, n])
continue; if (bmpobj.GetPixel(i + m - 1, j + n - 1).R > dgGrayValue) { newBmp.SetPixel(i, j, Color.White); break; } } } } } } bmpobj = newBmp; }
public void ThiningPic(int dgGrayValue)
{
int lWidth = bmpobj.Width;
int lHeight = bmpobj.Height;
bool bCondition1;
bool bCondition2;
bool bCondition3;
bool bCondition4;
bModified = true;
while (bModified)
{
bModified = false;
for (j = 2; j < lHeight - 2; j++)
{
for (i = 2; i < lWidth - 2; i++)
{
bCondition1 = false;
bCondition2 = false;
bCondition3 = false;
bCondition4 = false;
if (bmpobj.GetPixel(i, j).R > dgGrayValue)
{
if (bmpobj.GetPixel(i, j).R < 255)
bmpobj.SetPixel(i, j, Color.White);
continue;
}
for (m = 0; m < 5; m++)
{
for (n = 0; n < 5; n++)
{
neighbour[m, n] = bmpobj.GetPixel(i + m - 2, j + n - 2).R < dgGrayValue ? 1 : 0;
}
}
nCount = neighbour[1, 1] + neighbour[1, 2] + neighbour[1, 3]
+ neighbour[2, 1] + neighbour[2, 3] +
+neighbour[3, 1] + neighbour[3, 2] + neighbour[3, 3];
if (nCount >= 2 && nCount <= 6)
{
bCondition1 = true;
}
nCount = 0;
if (neighbour[1, 2] == 0 && neighbour[1, 1] == 1)
nCount++;
if (neighbour[1, 1] == 0 && neighbour[2, 1] == 1)
nCount++;
if (neighbour[2, 1] == 0 && neighbour[3, 1] == 1)
nCount++;
if (neighbour[3, 1] == 0 && neighbour[3, 2] == 1)
nCount++;
if (neighbour[3, 2] == 0 && neighbour[3, 3] == 1)
nCount++;
if (neighbour[3, 3] == 0 && neighbour[2, 3] == 1)
nCount++;
if (neighbour[2, 3] == 0 && neighbour[1, 3] == 1)
nCount++;
if (neighbour[1, 3] == 0 && neighbour[1, 2] == 1)
nCount++;
if (nCount == 1)
bCondition2 = true;
{
bCondition3 = true;
}
else
{
nCount = 0;
if (neighbour[0, 2] == 0 && neighbour[0, 1] == 1)
nCount++;
if (neighbour[0, 1] == 0 && neighbour[1, 1] == 1)
nCount++;
if (neighbour[1, 1] == 0 && neighbour[2, 1] == 1)
nCount++;
if (neighbour[2, 1] == 0 && neighbour[2, 2] == 1)
nCount++;
if (neighbour[2, 2] == 0 && neighbour[2, 3] == 1)
nCount++;
if (neighbour[2, 3] == 0 && neighbour[1, 3] == 1)
nCount++;
if (neighbour[1, 3] == 0 && neighbour[0, 3] == 1)
nCount++;
if (neighbour[0, 3] == 0 && neighbour[0, 2] == 1)
nCount++;
if (nCount != 1)
bCondition3 = true;
}
{
bCondition4 = true;
}
else
{
nCount = 0;
if (neighbour[1, 1] == 0 && neighbour[1, 0] == 1)
nCount++;
if (neighbour[1, 0] == 0 && neighbour[2, 0] == 1)
nCount++;
if (neighbour[2, 0] == 0 && neighbour[3, 0] == 1)
nCount++;
if (neighbour[3, 0] == 0 && neighbour[3, 1] == 1)
nCount++;
if (neighbour[3, 1] == 0 && neighbour[3, 2] == 1)
nCount++;
if (neighbour[3, 2] == 0 && neighbour[2, 2] == 1)
nCount++;
if (neighbour[2, 2] == 0 && neighbour[1, 2] == 1)
nCount++;
if (neighbour[1, 2] == 0 && neighbour[1, 1] == 1)
nCount++;
if (nCount != 1)
bCondition4 = true;
}
if (bCondition1 && bCondition2 && bCondition3 && bCondition4)
{
bmpobj.SetPixel(i, j, Color.White);
bModified = true;
}
else
{
bmpobj.SetPixel(i, j, Color.Black);
}
}
}
}
}
public void Sharpen(float val)
{
int w = bmpobj.Width;
int h = bmpobj.Height;
Bitmap bmpRtn = new Bitmap(w, h, PixelFormat.Format24bppRgb);
BitmapData srcData = bmpobj.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData dstData = bmpRtn.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
unsafe
{
int stride = srcData.Stride;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
if (x == 0 || x == w - 1 || y == 0 || y == h - 1)
{
pOut[0] = pIn[0];
pOut[1] = pIn[1];
pOut[2] = pIn[2];
}
else
{
int r1, r2, r3, r4, r5, r6, r7, r8, r0;
int g1, g2, g3, g4, g5, g6, g7, g8, g0;
int b1, b2, b3, b4, b5, b6, b7, b8, b0;
float vR, vG, vB;
p = pIn - stride - 3;
r1 = p[2];
g1 = p[1];
b1 = p[0];
p = pIn - stride;
r2 = p[2];
g2 = p[1];
b2 = p[0];
p = pIn - stride + 3;
r3 = p[2];
g3 = p[1];
b3 = p[0];
p = pIn - 3;
r4 = p[2];
g4 = p[1];
b4 = p[0];
p = pIn + 3;
r5 = p[2];
g5 = p[1];
b5 = p[0];
p = pIn + stride - 3;
r6 = p[2];
g6 = p[1];
b6 = p[0];
p = pIn + stride;
r7 = p[2];
g7 = p[1];
b7 = p[0];
p = pIn + stride + 3;
r8 = p[2];
g8 = p[1];
b8 = p[0];
p = pIn;
r0 = p[2];
g0 = p[1];
b0 = p[0];
vR = (float)r0 - (float)(r1 + r2 + r3 + r4 + r5 + r6 + r7 + r8) / 8;
vG = (float)g0 - (float)(g1 + g2 + g3 + g4 + g5 + g6 + g7 + g8) / 8;
vB = (float)b0 - (float)(b1 + b2 + b3 + b4 + b5 + b6 + b7 + b8) / 8;
if (vR > 0)
{
vR = Math.Min(255, vR);
}
else
{
vR = Math.Max(0, vR);
}
if (vG > 0)
{
vG = Math.Min(255, vG);
}
else
{
vG = Math.Max(0, vG);
}
if (vB > 0)
{
vB = Math.Min(255, vB);
}
else
{
vB = Math.Max(0, vB);
}
pOut[0] = (byte)vB;
pOut[1] = (byte)vG;
pOut[2] = (byte)vR;
}
pIn += 3;
pOut += 3;
}
bmpobj.UnlockBits(srcData);
bmpRtn.UnlockBits(dstData);
bmpobj = bmpRtn;
}
public void BitmapTo1Bpp(Double hsb)
{
int w = bmpobj.Width;
int h = bmpobj.Height;
Bitmap bmp = new Bitmap(w, h, PixelFormat.Format1bppIndexed);
BitmapData data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
for (int y = 0; y < h; y++)
{
byte[] scan = new byte[(w + 7) / 8];
for (int x = 0; x < w; x++)
{
Color c = bmpobj.GetPixel(x, y);
if (c.GetBrightness() >= hsb) scan[x / 8] |= (byte)(0x80 >> (x % 8));
}
}
bmp.UnlockBits(data);
bmpobj = bmp;
}
}
}