level 7
const int SCAN_GAP = 10;
private String GetBMPContext(Bitmap bmp)
{
Boolean bool1stScan = true;
int ax = 0, ay = 0, bx = 0, by = 0;
String result = "";
for (int i = 1; i < bmp.Width; i = i + SCAN_GAP)
{
for (int j = 1; j < bmp.Height; j = j + SCAN_GAP)
{
if (bmp.GetPixel(i, j).ToArgb() == Color.Black.ToArgb())
{
if (bool1stScan == false)
{
if (i <= ax) ax = i;
if (i >= bx) bx = i;
if (j <= ay) ay = j;
if (j >= by) by = j;
}
else
{
bool1stScan = false;
ax = i;
bx = i;
ay = j;
by = j;
}
}
}
}
Bitmap bmp2 = new Bitmap(20, 20);
Graphics g2 = Graphics.FromImage((Image)bmp2);
g2.Clear(Color.White);
g2.DrawImage(bmp, new Rectangle(0, 0, bmp2.Width, bmp2.Height),
new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
g2.Dispose();
// pictureBox1.Image = bmp2;
int a = 0, b = 0;
for (int i = 0; i < bmp2.Width; i++)
{
for (int j = 0; j < bmp2.Height; j++)
{
if (bmp2.GetPixel(i, j).ToArgb() == Color.Black.ToArgb())
result = result + "0";
else
result = result + "1";
}
}
return result;
}
public void Learn(String name)
{
StreamWriter sw = new StreamWriter(fileName, true);
sw.WriteLine(name + " " + GetBMPContext(bmpDraw));
sw.Close();
}
public String Recognise()
{
String current = GetBMPContext(bmpDraw);
StreamReader sr = new StreamReader(fileName);
int max = 0;
String result = "";
while (sr.EndOfStream == false)
{
String[] key = sr.ReadLine().Split(' ');
String name = key[0];
String data = key[1];
int match = 0;
for (int i = 0; i < current.Length; i++)
{
if (current[i] == data[i])
match++;
}
if (match >= max)
{
result = name;
max = match;
}
//Trace.WriteLine(result + ":" + match + "," + max);
}
sr.Close();
return result;
}
2013年06月03日 05点06分
3