最近項目遇到一個問題,需要對掃描出來的圖片進行去除黑邊。百度下沒有找到處理黑邊的源碼,無奈自己嘗試寫了這個方法。

1 /// <summary> 2 /// 自動去除圖像掃描黑邊 3 /// </summary> 4 /// <param name="fileName"></param> 5 public static void AutoCutBlackEdge(string fileName) 6 { 7 //打開圖像 8 Bitmap bmp = OpenImage(fileName); 9 10 RemoveBlackEdge(bmp); 11 //保存圖像 12 SaveImage(bmp, fileName); 13 } 14 15 private static byte[] rgbValues; // 目標數組內存 16 17 /// <summary> 18 /// 圖像去黑邊 19 /// </summary> 20 /// <param name="bmp"></param> 21 /// <returns></returns> 22 private static Bitmap RemoveBlackEdge(Bitmap bmp) 23 { 24 Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); 25 BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat); 26 27 // 獲取圖像參數 28 int w = bmpData.Width; 29 int h = bmpData.Height; 30 int stride = bmpData.Stride; // 掃描線的寬度 31 double picByteSize = GetPicByteSize(bmp.PixelFormat); 32 int bWidth = (int)Math.Ceiling(picByteSize * w); //顯示寬度 33 int offset = stride - bWidth; // 顯示寬度與掃描線寬度的間隙 34 IntPtr ptr = bmpData.Scan0; // 獲取bmpData的內存起始位置 35 int scanBytes = stride * h; // 用stride寬度,表示這是內存區域的大小 36 37 // 分別設置兩個位置指針,指向源數組和目標數組 38 int posScan = 0; 39 rgbValues = new byte[scanBytes]; // 為目標數組分配內存 40 Marshal.Copy(ptr, rgbValues, 0, scanBytes); // 將圖像數據拷貝到rgbValues中 41 42 bool isPass = true; 43 int i = 0, j = 0; 44 int cutW = (int)(bWidth * 0.02); //2%寬度(可修改) 45 int cutH = (int)(h * 0.02); //2%高度(可修改) 46 int posLen = (int)(picByteSize * 8); //繼續查找深度為8的倍數(可修改) 47 //左邊 48 for (i = 0; i < h; i++) 49 { 50 for (j = 0; j < bWidth; j++) 51 { 52 isPass = true; 53 if (rgbValues[posScan] < 255) rgbValues[posScan] = 255; 54 55 if (rgbValues[posScan + 1] == 255) 56 { 57 for (int m = 1; m <= posLen; m++) 58 { 59 if (rgbValues[posScan + m] < 255) isPass = false; 60 } 61 } 62 if (rgbValues[posScan + 1] < 255 || bWidth / 2 < j) isPass = false; 63 recCheck(ref rgbValues, posScan, h, stride, true); 64 65 posScan++; 66 if (j >= cutW && isPass) break; 67 } 68 // 跳過圖像數據每行未用空間的字節,length = stride - width * bytePerPixel 69 if (j == bWidth) posScan += offset; 70 else posScan += (offset + bWidth - j - 1); 71 } 72 //右邊 73 posScan = scanBytes - 1; 74 for (i = h - 1; i >= 0; i--) 75 { 76 posScan -= offset; 77 for (j = bWidth - 1; j >= 0; j--) 78 { 79 isPass = true; 80 if (rgbValues[posScan] < 255) rgbValues[posScan] = 255; 81 82 if (rgbValues[posScan - 1] == 255) 83 { 84 for (int m = 1; m <= posLen; m++) 85 { 86 if (rgbValues[posScan - m] < 255) isPass = false; 87 } 88 } 89 if (rgbValues[posScan - 1] < 255 || bWidth / 2 > j) isPass = false; 90 recCheck(ref rgbValues, posScan, h, stride, false); 91 92 posScan--; 93 if (cutH < (h - i)) 94 if (j < (bWidth - cutW) && isPass) break; 95 } 96 // 跳過圖像數據每行未用空間的字節,length = stride - width * bytePerPixel 97 if (j != -1) posScan -= j; 98 } 99 100 // 內存解鎖 101 Marshal.Copy(rgbValues, 0, ptr, scanBytes); 102 bmp.UnlockBits(bmpData); // 解鎖內存區域 103 104 return bmp; 105 } 106 107 /// <summary> 108 /// 上下去除黑邊時,臨近黑點去除 109 /// </summary> 110 /// <param name="rgbValues"></param> 111 /// <param name="posScan"></param> 112 /// <param name="h"></param> 113 /// <param name="stride"></param> 114 /// <param name="islLeft"></param> 115 private static void recCheck(ref byte[] rgbValues, int posScan, int h, int stride, bool islLeft) 116 { 117 int scanBytes = h * stride; 118 int cutH = (int)(h * 0.01); //臨近最大1%高度(可修改) 119 for (int i = 1; i <= cutH; i++) 120 { 121 int befRow = 0; 122 if (islLeft && (posScan - stride * i) > 0) 123 { 124 befRow = posScan - stride * i; 125 } 126 else if (!islLeft && (posScan + stride * i) < scanBytes) 127 { 128 befRow = posScan + stride * i; 129 } 130 if (rgbValues[befRow] < 255) rgbValues[befRow] = 255; 131 else break; 132 } 133 }
該方法沒有涉及到什么算法,都是按自己的思路寫的。雖然還是存在缺陷,但對於一般的文檔黑邊,還是夠用的。歡迎大家進行擴展或修改,並多多指點。
/// <summary>
/// 根據圖片像素數據格式獲得占的字節大小
/// </summary>
/// <param name="bmpPixelFormat"></param>
/// <returns></returns>
private static double GetPicByteSize(PixelFormat bmpPixelFormat)
{
double picByteSize;
if (bmpPixelFormat == PixelFormat.Format24bppRgb) picByteSize = 3;
else if (bmpPixelFormat == PixelFormat.Format32bppArgb) picByteSize = 4;
else if (bmpPixelFormat == PixelFormat.Format8bppIndexed) picByteSize = (double)3 / 24 * 8;
else if (bmpPixelFormat == PixelFormat.Format1bppIndexed) picByteSize = (double)3 / 24;
else if (bmpPixelFormat == PixelFormat.Format4bppIndexed) picByteSize = (double)3 / 24 * 4;
else picByteSize = 3;
return picByteSize;
}