最近寫了幾個網站的驗證碼圖片自動識別程序,儘管每一個網站的驗證碼圖片都不相同,識別的方法有所差異。但寫得多了,也總結出很多相同之處。今天抽空封裝出一個基礎類來,發現能夠很好地重複利用,編寫不一樣的驗證碼識別程序,效率提升了很多。好東東不能獨享,現放出來供你們共同研究,請網友們妥善用之。
封裝後的類使用很簡單,針對不一樣的驗證碼,相應繼承修改某些方法,便可簡單幾句代碼就能夠實現驗證碼識別了:
GrayByPixels(); //灰度處理
GetPicValidByValue(128, 4); //獲得有效空間
Bitmap[] pics = GetSplitPics(4, 1); //分割
數組
string code = GetSingleBmpCode(pics[i], 128); //獲得代碼串ide
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace BallotAiying2
{
class UnCodebase
{
public Bitmap bmpobj;
public UnBitmap pic)
{
bmpobj =new Bitmap(pic); //轉換爲Format32bppRgb
}
///<summary>
/// 根據RGB,計算灰度值
///</summary>
///<param name="posClr">Color值</param>
///<returns>灰度值,整型</returns>
privateint GetGrayNumColor(System.Drawing.Color posClr)
{
return (posClr.R *19595+ posClr.G *38469+ posClr.B *7472) >>16;
}
///<summary>
/// 灰度轉換,逐點方式
///</summary>
publicvoid 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));
}
}
}
///<summary>
/// 去圖形邊框
///</summary>
///<param name="borderWidth"></param>
publicvoid 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));
}
}
}
///<summary>
/// 灰度轉換,逐行方式
///</summary>
publicvoid GrayByLine()
{
Rectangle rec =new Rectangle(0, 0, bmpobj.Width, bmpobj.Height);
BitmapData bmpData = bmpobj.LockBits(rec, ImageLockMode.ReadWrite, bmpobj.PixelFormat);// PixelFormat.Format32bppPArgb);
// bmpData.PixelFormat = PixelFormat.Format24bppRgb;
IntPtr scan0 = bmpData.Scan0;
int len = bmpobj.Width * bmpobj.Height;
int[] pixels =newint[len];
Marshal.Copy(scan0, pixels, 0, len);
//對圖片進行處理
int GrayValue =0;
for (int i =0; i < len; i++)
{
GrayValue = GetGrayNumColor(Color.FromArgb(pixels[i]));
pixels[i] = (byte)(Color.FromArgb(GrayValue, GrayValue, GrayValue)).ToArgb(); //Color轉byte
}
bmpobj.UnlockBits(bmpData);
}
///<summary>
/// 獲得有效圖形並調整爲可平均分割的大小
///</summary>
///<param name="dgGrayValue">灰度背景分界值</param>
///<param name="CharsCount">有效字符數</param>
///<returns></returns>
publicvoid GetPicValidByValue(int dgGrayValue, int CharsCount)
{
int posx1 = bmpobj.Width; int posy1 = bmpobj.Height;
int posx2 =0; int posy2 =0;
for (int i =0; i < bmpobj.Height; i++) //找有效區
{
for (int j =0; j < bmpobj.Width; j++)
{
int pixelValue = bmpobj.GetPixel(j, i).R;
if (pixelValue < dgGrayValue) //根據灰度值
{
if (posx1 > j) posx1 = j;
if (posy1 > i) posy1 = i;
if (posx2 < j) posx2 = j;
if (posy2 < i) posy2 = i;
};
};
};
// 確保能整除
int Span = CharsCount - (posx2 - posx1 +1) % CharsCount; //可整除的差額數
if (Span < CharsCount)
{
int leftSpan = Span /2; //分配到左邊的空列 ,如span爲單數,則右邊比左邊大1
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);
}
///<summary>
/// 獲得有效圖形,圖形爲類變量
///</summary>
///<param name="dgGrayValue">灰度背景分界值</param>
///<param name="CharsCount">有效字符數</param>
///<returns></returns>
publicvoid GetPicValidByValue(int dgGrayValue)
{
int posx1 = bmpobj.Width; int posy1 = bmpobj.Height;
int posx2 =0; int posy2 =0;
for (int i =0; i < bmpobj.Height; i++) //找有效區
{
for (int j =0; j < bmpobj.Width; j++)
{
int pixelValue = bmpobj.GetPixel(j, i).R;
if (pixelValue < dgGrayValue) //根據灰度值
{
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);
}
///<summary>
/// 獲得有效圖形,圖形由外面傳入
///</summary>
///<param name="dgGrayValue">灰度背景分界值</param>
///<param name="CharsCount">有效字符數</param>
///<returns></returns>
public Bitmap GetPicValidByValue(Bitmap singlepic, int dgGrayValue)
{
int posx1 = singlepic.Width; int posy1 = singlepic.Height;
int posx2 =0; int posy2 =0;
for (int i =0; i < singlepic.Height; i++) //找有效區
{
for (int j =0; j < singlepic.Width; j++)
{
int pixelValue = singlepic.GetPixel(j, i).R;
if (pixelValue < dgGrayValue) //根據灰度值
{
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);
}
///<summary>
/// 平均分割圖片
///</summary>
///<param name="RowNum">水平上分割數</param>
///<param name="ColNum">垂直上分割數</param>
///<returns>分割好的圖片數組</returns>
public Bitmap [] GetSplitPics(int RowNum,int ColNum)
{
if (RowNum ==0|| ColNum ==0)
returnnull;
int singW = bmpobj.Width / RowNum;
int singH = bmpobj.Height / ColNum;
Bitmap [] PicArray=new Bitmap[RowNum*ColNum];
Rectangle cloneRect;
for (int i =0; i < ColNum; i++) //找有效區
{
for (int j =0; j < RowNum; j++)
{
cloneRect =new Rectangle(j*singW, i*singH, singW , singH);
PicArray[i*RowNum+j]=bmpobj.Clone(cloneRect, bmpobj.PixelFormat);//複製小塊圖
}
}
return PicArray;
}
///<summary>
/// 返回灰度圖片的點陣描述字串,1表示灰點,0表示背景
///</summary>
///<param name="singlepic">灰度圖</param>
///<param name="dgGrayValue">背前景灰色界限</param>
///<returns></returns>
publicstring 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);
if (piexl.R < dgGrayValue) // Color.Black )
code = code +"1";
else
code = code +"0";
}
return code;
}
}
}網站