| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 500 人关注过本帖
标题:验证码算法写完,但是怎么调用?
只看楼主 加入收藏
北辰风
Rank: 1
等 级:新手上路
帖 子:19
专家分:4
注 册:2013-1-14
结帖率:16.67%
收藏
已结贴  问题点数:20 回复次数:1 
验证码算法写完,但是怎么调用?
  /// <summary>
        /// 根据RGB,计算灰度值
        /// </summary>
        /// <param name="posClr">Color值</param>
        /// <returns>灰度值,整数</returns>
        private int GetGrayNumColor(System.Drawing.Color posClr)
        {
            return (posClr.R * 19292 + posClr.G * 38469 + posClr.B * 7472) >> 16;
        }
         
        /// <summary>
        /// 灰度转换,逐点方式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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,System.Drawing.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, System.Drawing.Color.FromArgb(255, 255, 255));
                }
            }
        }

        /// <summary>
        /// 灰度转换,逐行方式
        /// </summary>
        public void GrayByLine()
        {
            Rectangle rec = new Rectangle(0, 0, bmpobj.Width, bmpobj.Height);
            BitmapData bmpData = bmpobj.LockBits(rec, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmpobj.PixelFormat);
            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(System.Drawing.Color.FromArgb(pixels[i]));
                pixels[i] = (byte)(System.Drawing.Color.FromArgb(GrayValue, GrayValue, GrayValue)).ToArgb(); //Color转byte
            }
            bmpobj.UnlockBits(bmpData);
        }
        /// <summary>
        /// 得到有效图形并调整为可平均分割的大小
        /// </summary>
        /// <param name="dgGrayValue">灰度背景分界值</param>
        /// <param name="CharsCount">有效字符数</param>
        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++)  //找有效区
            {
                for (int j = 0; j < bmpobj.Width; j++)
                {
                    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;
                    }
                }
            }
            //确保能整除
            int Span = CharsCount - (posx2 - posx1 + 1) % CharsCount; //可整除的差额数
            if (Span < CharsCount)
            {
                int leftSpan = Span / 2; //分配到左边的空列,如span 为单数,则右边比左边大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">有效字符数</param>
        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++)  //找有效区
            {
                for (int j = 0; j < bmpobj.Width; j++)
                {
                    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;
                    }
                }
            }
            //复制新图
            Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy1 - posy2 + 1);
            bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat);
        }

        /// <summary>
        /// 得到有效图形,图形由外面传入
        /// </summary>
        /// <param name="dgGrayValue">灰度背景分界值</param>
        /// <param name="CharsCount">有效数字</param>
        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++)  //找有效区
            {
                for (int j = 0; j < singlepic.Width; j++)
                {
                    int pixelValue = singlepic.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;
                    }
                }
            }
            //复制新图
            Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
            return singlepic.Clone(cloneRect, singlepic.PixelFormat);
        }

        /// <summary>
        /// 平均分割图片
        /// </summary>
        /// <param name="RowNum">水平上分割数</param>
        /// <param name="ColNum">垂直上分割数</param>
        /// <returns>分割好的图片数组</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++) //找有效区
            {
                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>
        public string GetSingleBmpCode(Bitmap singlepic, int dgGrayValue)
        {
            System.Drawing.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)
                        code = code + "1";
                    else
                        code = code + "0";
                }
               
            }
            return code;
        }

搜索更多相关主题的帖子: 验证码 private returns 
2014-12-18 00:34
xydddaxia
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:33
帖 子:466
专家分:2307
注 册:2009-3-20
收藏
得分:20 
不知所问

站在春哥的肩膀上
2014-12-18 10:57
快速回复:验证码算法写完,但是怎么调用?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035508 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved