c#掃描圖片去黑邊(掃描儀去黑邊)
自動(dòng)去除圖像掃描黑邊
/// <summary>
/// 自動(dòng)去除圖像掃描黑邊
/// </summary>
/// <param name="fileName"></param>
public static void AutoCutBlackEdge(string fileName)
{
//打開圖像
Bitmap bmp = OpenImage(fileName);
RemoveBlackEdge(bmp);
//保存圖像
SaveImage(bmp, fileName);
}
private static byte[] rgbValues; // 目標(biāo)數(shù)組內(nèi)存
/// <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);
// 獲取圖像參數(shù)
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的內(nèi)存起始位置
int scanBytes = stride * h; // 用stride寬度,表示這是內(nèi)存區(qū)域的大小
// 分別設(shè)置兩個(gè)位置指針,指向源數(shù)組和目標(biāo)數(shù)組
int posScan = 0;
rgbValues = new byte[scanBytes]; // 為目標(biāo)數(shù)組分配內(nèi)存
Marshal.Copy(ptr, rgbValues, 0, scanBytes); // 將圖像數(shù)據(jù)拷貝到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); //繼續(xù)查找深度為8的倍數(shù)(可修改)
//左邊
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;
}
// 跳過圖像數(shù)據(jù)每行未用空間的字節(jié),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;
}
// 跳過圖像數(shù)據(jù)每行未用空間的字節(jié),length = stride - width * bytePerPixel
if (j != -1) posScan -= j;
}
// 內(nèi)存解鎖
Marshal.Copy(rgbValues, 0, ptr, scanBytes);
bmp.UnlockBits(bmpData); // 解鎖內(nèi)存區(qū)域
return bmp;
}
/// <summary>
/// 上下去除黑邊時(shí),臨近黑點(diǎn)去除
/// </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;
}
}
相關(guān)文章
漢字轉(zhuǎn)拼音縮寫示例代碼(Silverlight和.NET 將漢字轉(zhuǎn)換成為拼音)
本篇文章主要介紹了漢字轉(zhuǎn)拼音縮寫示例代碼(Silverlight和.NET 將漢字轉(zhuǎn)換成為拼音) 需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01C#切換鼠標(biāo)左右鍵習(xí)慣無需控制面板中修改
本人一直喜歡左手使用鼠標(biāo),偶爾同事會(huì)臨時(shí)操作一下,因?yàn)樗牧?xí)慣是右手,還得在控制面板里進(jìn)行更改,太麻煩了所以就編寫一個(gè)控制臺(tái)程序,雙擊一下即可切換左右鍵,熱愛懶人的你可不要錯(cuò)過了哈2013-02-02深入解析C#中的交錯(cuò)數(shù)組與隱式類型的數(shù)組
這篇文章主要介紹了深入解析C#中的交錯(cuò)數(shù)組與隱式類型的數(shù)組,隱式類型的數(shù)組通常與匿名類型以及對象初始值設(shè)定項(xiàng)和集合初始值設(shè)定項(xiàng)一起使用,需要的朋友可以參考下2016-01-01C#面向?qū)ο缶幊讨幸蕾嚪崔D(zhuǎn)原則的示例詳解
在面向?qū)ο缶幊讨?,SOLID?是五個(gè)設(shè)計(jì)原則的首字母縮寫,旨在使軟件設(shè)計(jì)更易于理解、靈活和可維護(hù)。本文將通過實(shí)例詳細(xì)講講C#面向?qū)ο缶幊讨幸蕾嚪崔D(zhuǎn)原則,需要的可以參考一下2022-07-07