当前位置: 移动技术网 > IT编程>开发语言>c# > C#识别出图片里的数字和字母

C#识别出图片里的数字和字母

2019年07月18日  | 移动技术网IT编程  | 我要评论

三鹿奶粉广告,ipv6,江西宜春中考成绩查询

一个图片识别小工具,原先主要是识别以前公司的软件注册码截图里的数字和字母(每次要一个一个框复制出来粘贴到注册器里,很麻烦!),因为注册码出现的字母和数字基本就那几个,所以识别库的范围设定的比较少。

原理和算法在代码中做了详细说明,功能存在很大的局限性,但我的想法是把这个思路和实现的办法共享出来。

源码下载地址:

/*
 * 开发思路:图片灰度处理,二进制,然后和图片中的字二进制库精确对比
 * 
 * 获取字库:通过下面代码中generatelicense(bitmap singlepic)方法获得,具体操作:
 *   从图片中截图出(抠出)一个字符,然后处理得到二维的二进制矩阵,比如下面的字符1对应的二维矩阵
 *   00000
 *   00100
 *   11100
 *   00100
 *   00100
 *   00100
 *   00100
 *   00100
 *   00100
 *   11111
 *   00000
 *   00000
 *   
 * 注意:【相同字符,比如1,不同字体,字号,不同缩放大小的图片,获得到的二位矩阵中0、1排列和数量都是不同的!
 *     故按照此方法来写出匹配所有字的话,那字库就大了。。。】
 * 
 * 
*/
/// <summary>
/// 提取出该图片内的字符(将进过灰度处理的图片转化为0、1的二位数组)
/// </summary>
/// <param name="singlepic">图片来源</param>
public void generatelicense(bitmap singlepic)
{
  try
  {
    char[,] chararray = new char[singlepic.height, singlepic.width];  //定义个chai型的二维数组记录每个像素上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的值用数字保存,以便于进行循环处理
      //code += codecache + "\n";
      for (imagewidth = 0; imagewidth < array.length; imagewidth++)
        chararray[imageheight, imagewidth] = array[imagewidth];  //通过循环将每行值转存到二维数组中
      imageheight++;
    }    //*********************以上代码可用来获取一个字的图片二进制数组,即字库*****************************
 
    //开始和字库进行匹配(我的工具中只需要下面的几个字符)
    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();  //执行identifysort方法,将我需要的格式在richtextboxlicense文本框中显示
    richtextboxlicense.selectionstart = richtextboxlicense.textlength;  //将光标移到最后面
  }
  catch { }
}

以上所述就是本文的全部内容了,希望大家能够喜欢。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网