最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c#實現(xiàn)識別圖片上的驗證碼數(shù)字

 更新時間:2015年11月08日 16:36:39   投稿:hebedich  
這篇文章主要介紹了c#實現(xiàn)識別圖片上的驗證碼數(shù)字的方法,本文給大家匯總了2種方法,有需要的小伙伴可以參考下。

public void imgdo(Bitmap img)
    {
      //去色
      Bitmap btp = img;
      Color c = new Color();
      int rr, gg, bb;
      for (int i = 0; i < btp.Width; i++)
      {
        for (int j = 0; j < btp.Height; j++)
        {
          //取圖片當前的像素點
          c = btp.GetPixel(i, j);
          rr = c.R; gg = c.G; bb = c.B;
          //改變顏色
          if (rr == 102 && gg == 0 && bb == 0)
          {
            //重新設(shè)置當前的像素點
            btp.SetPixel(i, j, Color.FromArgb(255, 255, 255, 255));
          }
          if (rr == 153 && gg == 0 && bb == 0)
          {
            //重新設(shè)置當前的像素點
            btp.SetPixel(i, j, Color.FromArgb(255, 255, 255, 255));
          } if (rr == 153 && gg == 0 && bb == 51)
          {
            //重新設(shè)置當前的像素點
            btp.SetPixel(i, j, Color.FromArgb(255, 255, 255, 255));
          } if (rr == 153 && gg == 43 && bb == 51)
          {
            //重新設(shè)置當前的像素點
            btp.SetPixel(i, j, Color.FromArgb(255, 255, 255, 255));
          }
          if (rr == 255 && gg == 255 && bb == 0)
          {
            //重新設(shè)置當前的像素點
            btp.SetPixel(i, j, Color.FromArgb(255, 255, 255, 255));
          }
          if (rr == 255 && gg == 255 && bb == 51)
          {
            //重新設(shè)置當前的像素點
            btp.SetPixel(i, j, Color.FromArgb(255, 255, 255, 255));
          }
        }
      }
      btp.Save("d:\\去除相關(guān)顏色.png");
 
      pictureBox2.Image = Image.FromFile("d:\\去除相關(guān)顏色.png");
 
 
      //灰度
      Bitmap bmphd = btp;
      for (int i = 0; i < bmphd.Width; i++)
      {
        for (int j = 0; j < bmphd.Height; j++)
        {
          //取圖片當前的像素點
          var color = bmphd.GetPixel(i, j);
 
          var gray = (int)(color.R * 0.001 + color.G * 0.700 + color.B * 0.250);
 
          //重新設(shè)置當前的像素點
          bmphd.SetPixel(i, j, Color.FromArgb(gray, gray, gray));
        }
      }
      bmphd.Save("d:\\灰度.png");
      pictureBox27.Image = Image.FromFile("d:\\灰度.png");
 
 
      //二值化
      Bitmap erzhi = bmphd;
      Bitmap orcbmp;
      int nn = 3;
      int w = erzhi.Width;
      int h = erzhi.Height;
      BitmapData data = erzhi.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
      unsafe
      {
        byte* p = (byte*)data.Scan0;
        byte[,] vSource = new byte[w, h];
        int offset = data.Stride - w * nn;
 
        for (int y = 0; y < h; y++)
        {
          for (int x = 0; x < w; x++)
          {
            vSource[x, y] = (byte)(((int)p[0] + (int)p[1] + (int)p[2]) / 3);
            p += nn;
          }
          p += offset;
        }
        erzhi.UnlockBits(data);
 
        Bitmap bmpDest = new Bitmap(w, h, PixelFormat.Format24bppRgb);
        BitmapData dataDest = bmpDest.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
        p = (byte*)dataDest.Scan0;
        offset = dataDest.Stride - w * nn;
        for (int y = 0; y < h; y++)
        {
          for (int x = 0; x < w; x++)
          {
            p[0] = p[1] = p[2] = (int)vSource[x, y] > 161 ? (byte)255 : (byte)0;
            //p[0] = p[1] = p[2] = (int)GetAverageColor(vSource, x, y, w, h) > 50 ? (byte)255 : (byte)0;
            p += nn;
 
          }
          p += offset;
        }
        bmpDest.UnlockBits(dataDest);
         
        orcbmp = bmpDest;
        orcbmp.Save("d:\\二值化.png");
        pictureBox29.Image = Image.FromFile("d:\\二值化.png");
      }
 
      //OCR的值
      if (orcbmp != null)
      {
        string result = Ocr(orcbmp);
        label32.Text = result.Replace("\n", "\r\n").Replace(" ", "");
      }
 
    }

C#識別驗證碼圖片通用類

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
 
namespace BallotAiying2
{
  class UnCodebase
  {
    public Bitmap bmpobj;
    public UnCodebase(Bitmap pic)
    {
      bmpobj = new Bitmap(pic);  //轉(zhuǎn)換為Format32bppRgb
    }
 
    /// <summary>
    /// 根據(jù)RGB,計算灰度值
    /// </summary>
    /// <param name="posClr">Color值</param>
    /// <returns>灰度值,整型</returns>
    private int GetGrayNumColor(System.Drawing.Color posClr)
    {
      return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16;
    }
 
    /// <summary>
    /// 灰度轉(zhuǎn)換,逐點方式
    /// </summary>
    public 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));
        }
      }
    }
 
    /// <summary>
    /// 去圖形邊框
    /// </summary>
    /// <param name="borderWidth"></param>
    public void ClearPicBorder(int borderWidth)
    {
      for (int i = 0; i < bmpobj.Height; i++)
      {
        for (int j = 0; j < bmpobj.Width; j++)
        {
          if (i < borderWidth || j < borderWidth || j > bmpobj.Width - 1 - borderWidth || i > bmpobj.Height - 1 - borderWidth)
            bmpobj.SetPixel(j, i, Color.FromArgb(255, 255, 255));
        }
      }
    }
 
    /// <summary>
    /// 灰度轉(zhuǎn)換,逐行方式
    /// </summary>
    public void GrayByLine()
    {
      Rectangle rec = new Rectangle(0, 0, bmpobj.Width, bmpobj.Height);
      BitmapData bmpData = bmpobj.LockBits(rec, ImageLockMode.ReadWrite, bmpobj.PixelFormat);// PixelFormat.Format32bppPArgb);
      //  bmpData.PixelFormat = PixelFormat.Format24bppRgb;
      IntPtr scan0 = bmpData.Scan0;
      int len = bmpobj.Width * bmpobj.Height;
      int[] pixels = new int[len];
      Marshal.Copy(scan0, pixels, 0, len);
 
      //對圖片進行處理
      int GrayValue = 0;
      for (int i = 0; i < len; i++)
      {
        GrayValue = GetGrayNumColor(Color.FromArgb(pixels));
        pixels = (byte)(Color.FromArgb(GrayValue, GrayValue, GrayValue)).ToArgb();   //Color轉(zhuǎn)byte
      }
 
      bmpobj.UnlockBits(bmpData);
    }
 
    /// <summary>
    /// 得到有效圖形并調(diào)整為可平均分割的大小
    /// </summary>
    /// <param name="dgGrayValue">灰度背景分界值</param>
    /// <param name="CharsCount">有效字符數(shù)</param>
    /// <returns></returns>
    public void GetPicValidByValue(int dgGrayValue, int CharsCount)
    {
      int posx1 = bmpobj.Width; int posy1 = bmpobj.Height;
      int posx2 = 0; int posy2 = 0;
      for (int i = 0; i < bmpobj.Height; i++)   //找有效區(qū)
      {
        for (int j = 0; j < bmpobj.Width; j++)
        {
          int pixelValue = bmpobj.GetPixel(j, i).R;
          if (pixelValue < dgGrayValue)   //根據(jù)灰度值
          {
            if (posx1 > j) posx1 = j;
            if (posy1 > i) posy1 = i;
 
            if (posx2 < j) posx2 = j;
            if (posy2 < i) posy2 = i;
          };
        };
      };
      // 確保能整除
      int Span = CharsCount - (posx2 - posx1 + 1) % CharsCount;  //可整除的差額數(shù)
      if (Span < CharsCount)
      {
        int leftSpan = Span / 2;  //分配到左邊的空列 ,如span為單數(shù),則右邊比左邊大1
        if (posx1 > leftSpan)
          posx1 = posx1 - leftSpan;
        if (posx2 + Span - leftSpan < bmpobj.Width)
          posx2 = posx2 + Span - leftSpan;
      }
      //復制新圖
      Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
      bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat);
    }
     
    /// <summary>
    /// 得到有效圖形,圖形為類變量
    /// </summary>
    /// <param name="dgGrayValue">灰度背景分界值</param>
    /// <param name="CharsCount">有效字符數(shù)</param>
    /// <returns></returns>
    public void GetPicValidByValue(int dgGrayValue)
    {
      int posx1 = bmpobj.Width; int posy1 = bmpobj.Height;
      int posx2 = 0; int posy2 = 0;
      for (int i = 0; i < bmpobj.Height; i++)   //找有效區(qū)
      {
        for (int j = 0; j < bmpobj.Width; j++)
        {
          int pixelValue = bmpobj.GetPixel(j, i).R;
          if (pixelValue < dgGrayValue)   //根據(jù)灰度值
          {
            if (posx1 > j) posx1 = j;
            if (posy1 > i) posy1 = i;
 
            if (posx2 < j) posx2 = j;
            if (posy2 < i) posy2 = i;
          };
        };
      };
      //復制新圖
      Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
      bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat);
    }
 
    /// <summary>
    /// 得到有效圖形,圖形由外面?zhèn)魅?
    /// </summary>
    /// <param name="dgGrayValue">灰度背景分界值</param>
    /// <param name="CharsCount">有效字符數(shù)</param>
    /// <returns></returns>
    public Bitmap GetPicValidByValue(Bitmap singlepic, int dgGrayValue)
    {
      int posx1 = singlepic.Width; int posy1 = singlepic.Height;
      int posx2 = 0; int posy2 = 0;
      for (int i = 0; i < singlepic.Height; i++)   //找有效區(qū)
      {
        for (int j = 0; j < singlepic.Width; j++)
        {
          int pixelValue = singlepic.GetPixel(j, i).R;
          if (pixelValue < dgGrayValue)   //根據(jù)灰度值
          {
            if (posx1 > j) posx1 = j;
            if (posy1 > i) posy1 = i;
 
            if (posx2 < j) posx2 = j;
            if (posy2 < i) posy2 = i;
          };
        };
      };
      //復制新圖
      Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
      return singlepic.Clone(cloneRect, singlepic.PixelFormat);
    }
     
    /// <summary>
    /// 平均分割圖片
    /// </summary>
    /// <param name="RowNum">水平上分割數(shù)</param>
    /// <param name="ColNum">垂直上分割數(shù)</param>
    /// <returns>分割好的圖片數(shù)組</returns>
    public Bitmap [] GetSplitPics(int RowNum,int ColNum)
    {
      if (RowNum == 0 || ColNum == 0)
        return null;
      int singW = bmpobj.Width / RowNum;
      int singH = bmpobj.Height / ColNum;
      Bitmap [] PicArray=new Bitmap[RowNum*ColNum];
 
      Rectangle cloneRect;
      for (int i = 0; i < ColNum; i++)   //找有效區(qū)
      {
        for (int j = 0; j < RowNum; j++)
        {
          cloneRect = new Rectangle(j*singW, i*singH, singW , singH);
          PicArray[i*RowNum+j]=bmpobj.Clone(cloneRect, bmpobj.PixelFormat);//復制小塊圖
        }
      }
      return PicArray;
    }
 
    /// <summary>
    /// 返回灰度圖片的點陣描述字串,1表示灰點,0表示背景
    /// </summary>
    /// <param name="singlepic">灰度圖</param>
    /// <param name="dgGrayValue">背前景灰色界限</param>
    /// <returns></returns>
    public 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;
    }
  }
}

以上2則都是使用C#實現(xiàn)的orc識別的代碼,希望對大家學習C#有所幫助。

相關(guān)文章

  • C#實現(xiàn)給Word每一頁設(shè)置不同文字水印的方法詳解

    C#實現(xiàn)給Word每一頁設(shè)置不同文字水印的方法詳解

    Word中設(shè)置水印時,可使用預設(shè)的文字或自定義文字設(shè)置為水印效果,但通常添加水印效果時,會對所有頁面都設(shè)置成統(tǒng)一效果。本文以C#?代碼為例,對Word每一頁設(shè)置不同的文字水印效果作詳細介紹,感興趣的可以了解一下
    2022-07-07
  • 舊項目升級新版Unity2021導致Visual?Studio無法使用的問題

    舊項目升級新版Unity2021導致Visual?Studio無法使用的問題

    在項目開發(fā)過程中,不可避免的會升級開發(fā)工具。這次我在舊項目版本升級到新版Unity2021.2.x時,出現(xiàn)Visual?Studio無法定位等問題,這里我給大家分享下解決方法,舊項目升級新版Unity2021導致Visual?Studio無法使用的問題,需要的朋友可以參考下
    2021-12-12
  • C#判斷獲取的是文件夾還是文件的實例

    C#判斷獲取的是文件夾還是文件的實例

    今天小編就為大家分享一篇C#判斷獲取的是文件夾還是文件的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • c# BackgroundWorker組件的作用

    c# BackgroundWorker組件的作用

    這篇文章主要介紹了c# BackgroundWorker組件的作用,幫助大家更好的理解和使用c#編程語言,感興趣的朋友可以了解下
    2020-12-12
  • C#中調(diào)用VB中Inputbox類的實現(xiàn)方法

    C#中調(diào)用VB中Inputbox類的實現(xiàn)方法

    本文主要介紹在項目中引用Microsoft.VisualBasic,間接使用VB中的各種類庫的方法,或者自己創(chuàng)建函數(shù),調(diào)用自定義方法,以實現(xiàn)InputBox相關(guān)的功能。
    2016-05-05
  • C#鍵盤鼠標鉤子實例

    C#鍵盤鼠標鉤子實例

    這篇文章主要介紹了C#鍵盤鼠標鉤子的實現(xiàn)方法,以實例形式分析了針對鍵盤與鼠標鉤子的實現(xiàn)步驟與相關(guān)技巧,并配有相關(guān)官方文檔說明的地址,需要的朋友可以參考下
    2014-12-12
  • Winform中如何跨線程訪問UI元素

    Winform中如何跨線程訪問UI元素

    這篇文章主要介紹了Winform中如何跨線程訪問UI元素,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2020-10-10
  • 積累Visual Studio 常用快捷鍵的動畫演示

    積累Visual Studio 常用快捷鍵的動畫演示

    在代碼開發(fā)過程中,頻繁的使用鍵盤、鼠標操作非常麻煩,影響程序的開發(fā)效率。如何操作能用鍵盤來操作,那就節(jié)省時間了。下面小編把我平時積累的有關(guān)visul studio 常用快捷鍵的動畫演示分享給大家,僅供大家參考
    2015-10-10
  • c#計算某段代碼的執(zhí)行時間實例方法

    c#計算某段代碼的執(zhí)行時間實例方法

    在本篇文章里我們給大家整理了關(guān)于c#計算某段代碼的執(zhí)行時間的方法和經(jīng)驗,有興趣的朋友們學習下。
    2019-02-02
  • c#菜單動態(tài)合并的實現(xiàn)方法

    c#菜單動態(tài)合并的實現(xiàn)方法

    這篇文章主要介紹了c#菜單動態(tài)合并的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-10-10

最新評論

九江县| 盐山县| 马边| 平果县| 河源市| 新干县| 余江县| 温泉县| 东台市| 屏东县| 鹤庆县| 灵璧县| 荣昌县| 股票| 永新县| 海南省| 万年县| 伊宁市| 江山市| 永川市| 文山县| 进贤县| 丰城市| 邵武市| 秭归县| 托克逊县| 宁明县| 赤城县| 高清| 石家庄市| 无为县| 肥西县| 扶余县| 和林格尔县| 深圳市| 合江县| 金堂县| 太白县| 桓仁| 时尚| 神木县|