程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#掃描圖片去黑邊(掃描儀去黑邊)

c#掃描圖片去黑邊(掃描儀去黑邊)

編輯:C#入門知識

c#掃描圖片去黑邊(掃描儀去黑邊)。本站提示廣大學習愛好者:(c#掃描圖片去黑邊(掃描儀去黑邊))文章只能為提供參考,不一定能成為您想要的結果。以下是c#掃描圖片去黑邊(掃描儀去黑邊)正文


主動去除圖象掃描黑邊


/// <summary>
        /// 主動去除圖象掃描黑邊
        /// </summary>
        /// <param name="fileName"></param>
        public static void AutoCutBlackEdge(string fileName)
        {
            //翻開圖象
            Bitmap bmp = OpenImage(fileName);

            RemoveBlackEdge(bmp);
            //保留圖象
            SaveImage(bmp, fileName);
        }

        private static byte[] rgbValues; // 目的數組內存

        /// <summary>
        /// 圖象去黑邊
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
        private static Bitmap RemoveBlackEdge(Bitmap bmp)
        {
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);

            // 獲得圖象參數 
            int w = bmpData.Width;
            int h = bmpData.Height;
            int stride = bmpData.Stride;  // 掃描線的寬度
            double picByteSize = GetPicByteSize(bmp.PixelFormat);
            int bWidth = (int)Math.Ceiling(picByteSize * w); //顯示寬度
            int offset = stride - bWidth;  // 顯示寬度與掃描線寬度的間隙 
            IntPtr ptr = bmpData.Scan0;   // 獲得bmpData的內存肇端地位 
            int scanBytes = stride * h;  // 用stride寬度,表現這是內存區域的年夜小

            // 分離設置兩個地位指針,指向源數組和目的數組 
            int posScan = 0;
            rgbValues = new byte[scanBytes];  // 為目的數組分派內存 
            Marshal.Copy(ptr, rgbValues, 0, scanBytes);  // 將圖象數據拷貝到rgbValues中 

            bool isPass = true;
            int i = 0, j = 0;
            int cutW = (int)(bWidth * 0.02); //2%寬度(可修正)
            int cutH = (int)(h * 0.02);      //2%高度(可修正)
            int posLen = (int)(picByteSize * 8); //持續查找深度為8的倍數(可修正)
            //右邊
            for (i = 0; i < h; i++)
            {
                for (j = 0; j < bWidth; j++)
                {
                    isPass = true;
                    if (rgbValues[posScan] < 255) rgbValues[posScan] = 255;

                    if (rgbValues[posScan + 1] == 255)
                    {
                        for (int m = 1; m <= posLen; m++)
                        {
                            if (rgbValues[posScan + m] < 255) isPass = false;
                        }
                    }
                    if (rgbValues[posScan + 1] < 255 || bWidth / 2 < j) isPass = false;
                    recCheck(ref rgbValues, posScan, h, stride, true);

                    posScan++;
                    if (j >= cutW && isPass) break;
                }
                // 跳過圖象數據每行未用空間的字節,length = stride - width * bytePerPixel 
                if (j == bWidth) posScan += offset;
                else posScan += (offset + bWidth - j - 1);
            }
            //左邊
            posScan = scanBytes - 1;
            for (i = h - 1; i >= 0; i--)
            {
                posScan -= offset;
                for (j = bWidth - 1; j >= 0; j--)
                {
                    isPass = true;
                    if (rgbValues[posScan] < 255) rgbValues[posScan] = 255;

                    if (rgbValues[posScan - 1] == 255)
                    {
                        for (int m = 1; m <= posLen; m++)
                        {
                            if (rgbValues[posScan - m] < 255) isPass = false;
                        }
                    }
                    if (rgbValues[posScan - 1] < 255 || bWidth / 2 > j) isPass = false;
                    recCheck(ref rgbValues, posScan, h, stride, false);

                    posScan--;
                    if (cutH < (h - i))
                        if (j < (bWidth - cutW) && isPass) break;
                }
                // 跳過圖象數據每行未用空間的字節,length = stride - width * bytePerPixel
                if (j != -1) posScan -= j;
            }

            // 內存解鎖 
            Marshal.Copy(rgbValues, 0, ptr, scanBytes);
            bmp.UnlockBits(bmpData);  // 解鎖內存區域 

            return bmp;
        }

        /// <summary>
        /// 高低去除黑邊時,鄰近斑點去除
        /// </summary>
        /// <param name="rgbValues"></param>
        /// <param name="posScan"></param>
        /// <param name="h"></param>
        /// <param name="stride"></param>
        /// <param name="islLeft"></param>
        private static void recCheck(ref byte[] rgbValues, int posScan, int h, int stride, bool islLeft)
        {
            int scanBytes = h * stride;
            int cutH = (int)(h * 0.01); //鄰近最年夜1%高度(可修正)
            for (int i = 1; i <= cutH; i++)
            {
                int befRow = 0;
                if (islLeft && (posScan - stride * i) > 0)
                {
                    befRow = posScan - stride * i;
                }
                else if (!islLeft && (posScan + stride * i) < scanBytes)
                {
                    befRow = posScan + stride * i;
                }
                if (rgbValues[befRow] < 255) rgbValues[befRow] = 255;
                else break;
            }
        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved