using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
namespace ConsoleApplication1
{
class MyImage
{
//需要进行分析的图片
private Bitmap bmpobj;
//需要继承并且重写的数字比对串,在子类里面需要用到这个去比对数字
public Dictionary<string, char> numDic = new Dictionary<string, char>();
//得到图片中某一点的灰度数值
private int GetGrayNumColor(System.Drawing.Color posClr)
{
return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16;
}
//进行灰度处理
private 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));
}
}
}
//得到一个图片的01代码序列
private 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);
if (piexl.R < dgGrayValue) // Color.Black )
code = code + "1";
else
code = code + "0";
} return code;
}
//从一个图片里面得到几个分开的数字小图片
private Bitmap[] GetPicValidByValue(int dgGrayValue)
{
List<Bitmap> PicList = new List<Bitmap>();
Rectangle cloneRect;
int posx1 = bmpobj.Width, posy1 = bmpobj.Height, posx2 = 0, posy2 = 0;
bool cut = false;
int last = -1, lastx = 0;
for (int j = 0; j < bmpobj.Width; j++) //找有效区
{
cut = false;
for (int i = 0; i < bmpobj.Height; i++)
{
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;
cut = true;
}
};
if (cut)
continue;
if (last + 1 == j)
{
last++;
continue;
}
cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
lastx = j;
last = j;
posx1 = bmpobj.Width;
posy1 = bmpobj.Height;
posx2 = 0;
posy2 = 0;
PicList.Add(bmpobj.Clone(cloneRect, bmpobj.PixelFormat));//复制小块图
};
return PicList.ToArray();
}
//得到一个图片的数字串
private string GetPicNumber()
{
GrayByPixels(); //灰度处理
Bitmap[] pics = GetPicValidByValue(128); //得到有效值
StringBuilder sb = new StringBuilder();
char c;
for (int i = 0; i < pics.Length; ++i)
{
string code = GetSingleBmpCode(pics[i], 128); //得到代码串
if (numDic.TryGetValue(code, out c))
{
sb.Append(c);
}
}
return sb.ToString();
}
//外部调用,得到一个图片的数字串
public string GetPicNum(Bitmap pic)
{
bmpobj = new Bitmap(pic); //转换为Format32bppRgb
return GetPicNumber();
}
//外部调用,得到一个图片的数字串(重载)
public string GetPicNum(string fileName)
{
bmpobj = new Bitmap(fileName);
return GetPicNumber();
}
//输出在一幅图里面找到的数字,测试或者找到数字比对串时用
private void TestNumber()
{
GrayByPixels(); //灰度处理
Bitmap[] pics = GetPicValidByValue(128); //得到有效值
for (int i = 0; i < pics.Length; ++i)
{
string code = GetSingleBmpCode(pics[i], 128); //得到代码串
Console.WriteLine(i); Console.WriteLine(code);
}
}
//外部调用,输出在一幅图里面找到的数字,测试或者找到数字比对串时用
public void TestNum(Bitmap pic)
{
bmpobj = new Bitmap(pic); //转换为Format32bppRgb
TestNumber();
}
//外部调用,输出在一幅图里面找到的数字,测试或者找到数字比对串时用(重载)
public void TestNum(string fileName)
{
bmpobj = new Bitmap(fileName);
TestNumber();
}
}
class JingdongImage : MyImage
{
public JingdongImage()
{
//numDic.Add("111101111011100110001110100001110100000111000000111000001111100000111000000111100", '¥');
numDic.Add("011110110011110011110011110011110011110011011110", '0');
numDic.Add("01101110011001100110011001101111", '1');
numDic.Add("011110110011000011000110001100011000110000111111", '2');
numDic.Add("011110110011000011001110000011000011110011011110", '3');
numDic.Add("000010000110001110010110100110111111000110000110", '4');
numDic.Add("011111011000011000011110000011000011110011011110", '5');
numDic.Add("001110011000110000111110110011110011110011011110", '6');
numDic.Add("111111000011000110000110001100001100011000011000", '7');
numDic.Add("011110110011110011011110110011110011110011011110", '8');
numDic.Add("011110110011110011110011011111000011000110011100", '9');
numDic.Add("1111", '.');
}
}
class Program
{
static void Main(string[] args)
{
DirectoryInfo dInfo = new DirectoryInfo(@"D:/Images");
JingdongImage test = new JingdongImage();
foreach (var file in dInfo.GetFiles())
{
Console.WriteLine(file.FullName);
Console.WriteLine(test.GetPicNum(file.FullName));
}
Console.ReadLine();
}
}
}