Source code for identifying QQ verification codes (C #/net1.1)
Source: Internet
Author: User
Notsamenum ++;
}
}
If (notsamenum <4)
{
Char Cj = (char) datachar [II];
Return CJ. tostring ();
}
}
Return jieguo;
}
/// <Summary>
/// Check whether related records exist in the feature database
/// </Summary>
Bool ischardatain ()
{
Bool jieguo = false;
For (int ii = 0; II <datanum; II ++)
{
// Calculate the number of rows with different pixels. If the number of rows is less than four, the record exists.
// This method is relatively primitive, but it is suitable for running in multiple threads, because Program Only perform simple logical comparison
// If more feature databases can be collected, the recognition rate can reach more than 80%.
// (In this case, you may need to increase the feature library capacity to or more)
// Of course, the product configuration can also be improved Algorithm (For example, key-Point configuration), with a small number of feature libraries to achieve a high recognition rate,
// There is a large chance of identifying errors and it takes a lot of CPU time for multithreading.
Int notsamenum = 0;
If (system. Math. Abs (datax [II, 0]-xlpic)> 1 | system. Math. Abs (datax [II, 1]-ylpic)> 1)
{
Continue;
}
For (int jj = 0; JJ <20; JJ ++)
{
If (datap [II, JJ]! = Datapic [JJ])
{
Notsamenum ++;
}
}
If (notsamenum <4)
{
String asdasd = (char) datachar [II]). tostring ();
Return true;
}
}
Return jieguo;
}
/// <Summary>
/// Add it to the feature library and temporarily set the corresponding characters to spaces for manual recognition
/// </Summary>
Void adddatawithnullchar ()
{
If (this. ischardatain ())
{
Return;
}
For (int ii = 0; II <20; II ++)
{
Datap [datanum, ii] = This. datapic [II];
}
// Temporarily set the corresponding characters to spaces for manual recognition
Datachar [datanum] = 32;
Datasync [datanum, 0] = This. xlpic;
Datasync [datanum, 1] = This. ylpic;
Datanum ++;
}
/// <Summary>
/// Check whether the verification code image can be divided into four parts. If yes, check whether the four characters already exist in the feature library. If not,
/// Add it to the feature library, and temporarily set the corresponding characters to spaces for manual recognition.
/// </Summary>
Public void writetodata ()
{
Bool [,] picpixel = new bool [49,20];
For (int ii = 0; II <49; II ++)
{
For (int jj = 0; JJ <20; JJ ++)
{
If (BP. getpixel (II, JJ). getbrightness () <0.999)
{
Picpixel [II, JJ] = true;
}
}
}
Int [] Index = new int [8];
Int indexnum = 0;
Bool black = false;
For (int ii = 0; II <49; II ++)
{
Bool haveblack = false;
For (int jj = 0; JJ <20; JJ ++)
{
If (picpixel [II, JJ])
{
Haveblack = true;
Break;
}
}
If (haveblack & black = false)
{
Index [indexnum] = II;
Indexnum ++;
Black = true;
}
If (! Haveblack & black)
{
Index [indexnum] = II;
Indexnum ++;
Black = false;
}
}
If (indexnum <7)
{
Return;
}
If (indexnum = 7)
{
Index [7] = 49;
}
//****
For (int ii = 0; II <4; II ++)
{
Int X1 = index [II * 2];
Int X2 = index [II * 2 + 1];
Int Y1 = 0, y2 = 19;
Bool MB = false;
For (int jj = 0; JJ <20; JJ ++)
{
For (int kk = x1; KK <X2; KK ++)
{
If (picpixel [Kk, JJ])
{
MB = true;
Break;
}
}
If (MB)
{
Y1 = JJ;
Break;
}
}
MB = false;
For (int jj = 19; JJ> = 0; jj --)
{
For (int kk = x1; KK <X2; KK ++)
{
If (picpixel [Kk, JJ])
{
MB = true;
Break;
}
}
If (MB)
{
Y2 = JJ;
Break;
}
}
// ** The preceding figure shows the range of valid regions.
For (int jj = 0; JJ <20; JJ ++)
{
This. datapic [JJ] = 0;
This. datapic [JJ] = 0;
}
This. xlpic = (byte) (x2-x1 );
// If the character width exceeds 16 pixels, it will not be processed
If (xlpic> 16)
{
Continue;
}
This. ylpic = (byte) (y2-y1 + 1 );
Int ys =-1;
Ushort [] addin = new ushort [] {64,128,256,512,102 };
For (int jj = Y1; JJ <= Y2; JJ ++)
{
YS ++;
Int xs =-1;
For (int kk = x1; KK <X2; KK ++)
{
Xs ++;
If (picpixel [Kk, JJ])
{
This. datapic [YS] = (ushort) (This. datapic [YS] + addin [Xs]);
}
}
}
This. adddatawithnullchar ();
}
//****
}
/// <Summary>
/// Recognize images
/// </Summary>
/// <Returns> returns the recognition result (if the returned string length is less than 4, the recognition fails) </returns>
Public String ocrpic ()
{
String jieguo = "";
Bool [,] picpixel = new bool [49,20];
For (int ii = 0; II <49; II ++)
{
For (int jj = 0; JJ <20; JJ ++)
{
If (BP. getpixel (II, JJ). getbrightness () <0.999)
{
Picpixel [II, JJ] = true;
}
}
}
Int [] Index = new int [8];
Int indexnum = 0;
Bool black = false;
For (int ii = 0; II <49; II ++)
{
Bool haveblack = false;
For (int jj = 0; JJ <20; JJ ++)
{
If (picpixel [II, JJ])
{
Haveblack = true;
Break;
}
}
If (haveblack & black = false)
{
Index [indexnum] = II;
Indexnum ++;
Black = true;
}
If (! Haveblack & black)
{
Index [indexnum] = II;
Indexnum ++;
Black = false;
}
}
If (indexnum <7)
{
Return jieguo;
}
If (indexnum = 7)
{
Index [7] = 49;
}
//****
For (int ii = 0; II <4; II ++)
{
Int X1 = index [II * 2];
Int X2 = index [II * 2 + 1];
Int Y1 = 0, y2 = 19;
Bool MB = false;
For (int jj = 0; JJ <20; JJ ++)
{
For (int kk = x1; KK <X2; KK ++)
{
If (picpixel [Kk, JJ])
{
MB = true;
Break;
}
}
If (MB)
{
Y1 = JJ;
Break;
}
}
MB = false;
For (int jj = 19; JJ> = 0; jj --)
{
For (int kk = x1; KK <X2; KK ++)
{
If (picpixel [Kk, JJ])
{
MB = true;
Break;
}
}
If (MB)
{
Y2 = JJ;
Break;
}
}
// ** The preceding figure shows the range of valid regions.
For (int jj = 0; JJ <20; JJ ++)
{
This. datapic [JJ] = 0;
This. datapic [JJ] = 0;
}
This. xlpic = (byte) (x2-x1 );
// If the character width exceeds 16 pixels, it will not be processed
If (xlpic> 16)
{
Continue;
}
This. ylpic = (byte) (y2-y1 + 1 );
Int ys =-1;
Ushort [] addin = new ushort [] {64,128,256,512,102 };
For (int jj = Y1; JJ <= Y2; JJ ++)
{
YS ++;
Int xs =-1;
For (int kk = x1; KK <X2; KK ++)
{
Xs ++;
If (picpixel [Kk, JJ])
{
This. datapic [YS] = (ushort) (This. datapic [YS] + addin [Xs]);
}
}
}
Jieguo = jieguo + this. getchar ();
}
Return jieguo;
}
}
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service