C#識別出圖片里的數(shù)字和字母
一個圖片識別小工具,原先主要是識別以前公司的軟件注冊碼截圖里的數(shù)字和字母(每次要一個一個框復(fù)制出來粘貼到注冊器里,很麻煩!),因為注冊碼出現(xiàn)的字母和數(shù)字基本就那幾個,所以識別庫的范圍設(shè)定的比較少。
原理和算法在代碼中做了詳細(xì)說明,功能存在很大的局限性,但我的想法是把這個思路和實現(xiàn)的辦法共享出來。
源碼下載地址:
http://git.oschina.net/bobo2cj/iamge2text

/*
* 開發(fā)思路:圖片灰度處理,二進(jìn)制,然后和圖片中的字二進(jìn)制庫精確對比
*
* 獲取字庫:通過下面代碼中g(shù)enerateLicense(Bitmap singlepic)方法獲得,具體操作:
* 從圖片中截圖出(摳出)一個字符,然后處理得到二維的二進(jìn)制矩陣,比如下面的字符1對應(yīng)的二維矩陣
* 00000
* 00100
* 11100
* 00100
* 00100
* 00100
* 00100
* 00100
* 00100
* 11111
* 00000
* 00000
*
* 注意:【相同字符,比如1,不同字體,字號,不同縮放大小的圖片,獲得到的二位矩陣中0、1排列和數(shù)量都是不同的!
* 故按照此方法來寫出匹配所有字的話,那字庫就大了。。?!?
*
*
*/
/// <summary>
/// 提取出該圖片內(nèi)的字符(將進(jìn)過灰度處理的圖片轉(zhuǎn)化為0、1的二位數(shù)組)
/// </summary>
/// <param name="singlepic">圖片來源</param>
public void generateLicense(Bitmap singlepic)
{
try
{
char[,] charArray = new char[singlepic.Height, singlepic.Width]; //定義個chai型的二維數(shù)組記錄每個像素上0/1的值,形成一個矩形
int imageWidth = 0; //記錄圖片的像素寬度
int imageHeight = 0; //記錄圖片的像素高度
int dgGrayValue = 128; //灰度值
Color piexl;
//string code = ""; //存儲每個像素的0/1
for (int posy = 0; posy < singlepic.Height; posy++)
{//從上到下
string codeCache = ""; //存儲每行的像素的0/1
for (int posx = 0; posx < singlepic.Width; posx++)
{//從左到右
piexl = singlepic.GetPixel(posx, posy);
if (piexl.R < dgGrayValue)
{// 如果該像素的顏色為黑色,值就為“1”
codeCache = codeCache + "1";
}
else
{// 否則該像素的顏色為白色,值就為“0”
codeCache = codeCache + "0";
}
}
char[] array = codeCache.ToCharArray(); //每行的0/1的值用數(shù)字保存,以便于進(jìn)行循環(huán)處理
//code += codeCache + "\n";
for (imageWidth = 0; imageWidth < array.Length; imageWidth++)
charArray[imageHeight, imageWidth] = array[imageWidth]; //通過循環(huán)將每行值轉(zhuǎn)存到二維數(shù)組中
imageHeight++;
} //*********************以上代碼可用來獲取一個字的圖片二進(jìn)制數(shù)組,即字庫*****************************
//開始和字庫進(jìn)行匹配(我的工具中只需要下面的幾個字符)
findWord(charArray, char0, imageHeight, imageWidth, BinaryWidth0, BinaryHeight0, '0');
findWord(charArray, char1, imageHeight, imageWidth, BinaryWidth1, BinaryHeight1, '1');
findWord(charArray, char2, imageHeight, imageWidth, BinaryWidth2, BinaryHeight2, '2');
findWord(charArray, char3, imageHeight, imageWidth, BinaryWidth3, BinaryHeight3, '3');
findWord(charArray, char4, imageHeight, imageWidth, BinaryWidth4, BinaryHeight4, '4');
findWord(charArray, char5, imageHeight, imageWidth, BinaryWidth5, BinaryHeight5, '5');
findWord(charArray, char6, imageHeight, imageWidth, BinaryWidth6, BinaryHeight6, '6');
findWord(charArray, char7, imageHeight, imageWidth, BinaryWidth7, BinaryHeight7, '7');
findWord(charArray, char8, imageHeight, imageWidth, BinaryWidth8, BinaryHeight8, '8');
findWord(charArray, char9, imageHeight, imageWidth, BinaryWidth9, BinaryHeight9, '9');
findWord(charArray, charA, imageHeight, imageWidth, BinaryWidthA, BinaryHeightA, 'a');
findWord(charArray, charB, imageHeight, imageWidth, BinaryWidthB, BinaryHeightB, 'b');
findWord(charArray, charC, imageHeight, imageWidth, BinaryWidthC, BinaryHeightC, 'c');
findWord(charArray, charD, imageHeight, imageWidth, BinaryWidthD, BinaryHeightD, 'd');
findWord(charArray, charE, imageHeight, imageWidth, BinaryWidthE, BinaryHeightE, 'e');
findWord(charArray, charF, imageHeight, imageWidth, BinaryWidthF, BinaryHeightF, 'f');
findWord(charArray, charP, imageHeight, imageWidth, BinaryWidthP, BinaryHeightP, 'p');
findWord(charArray, charY, imageHeight, imageWidth, BinaryWidthY, BinaryHeightY, 'y');
//------------------------------------END---------------------------------------------
richTextBoxLicense.Text += identifySort(); //執(zhí)行identifySort方法,將我需要的格式在richTextBoxLicense文本框中顯示
richTextBoxLicense.SelectionStart = richTextBoxLicense.TextLength; //將光標(biāo)移到最后面
}
catch { }
}

以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
相關(guān)文章
C#中使用Lambda表達(dá)式自定義比較器實現(xiàn)兩個列表合并實例
這篇文章主要介紹了C#中使用Lambda表達(dá)式自定義比較器實現(xiàn)兩個列表的合并實例,本文給出示例代碼和運(yùn)行效果,需要的朋友可以參考下2014-10-10
C#實現(xiàn)通過程序自動抓取遠(yuǎn)程Web網(wǎng)頁信息的代碼
C#實現(xiàn)通過程序自動抓取遠(yuǎn)程Web網(wǎng)頁信息的代碼...2007-04-04
C#實現(xiàn)目錄跳轉(zhuǎn)(TreeView和SplitContainer)的示例代碼
本文主要介紹了C#實現(xiàn)目錄跳轉(zhuǎn)(TreeView和SplitContainer)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

