c scan image to remove black edge of scanner to remove black edge
- 2020-06-15 10:08:59
- OfStack
Automatically remove image scan black edge
/// <summary>
/// Automatically remove image scan black edge
/// </summary>
/// <param name="fileName"></param>
public static void AutoCutBlackEdge(string fileName)
{
// Open the image
Bitmap bmp = OpenImage(fileName);
RemoveBlackEdge(bmp);
// Save the image
SaveImage(bmp, fileName);
}
private static byte[] rgbValues; // Target array memory
/// <summary>
/// The image goes black
/// </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);
// Get image parameters
int w = bmpData.Width;
int h = bmpData.Height;
int stride = bmpData.Stride; // The width of the scan line
double picByteSize = GetPicByteSize(bmp.PixelFormat);
int bWidth = (int)Math.Ceiling(picByteSize * w); // According to the width of the
int offset = stride - bWidth; // Shows the gap between width and scanline width
IntPtr ptr = bmpData.Scan0; // To obtain bmpData Is the starting location of memory
int scanBytes = stride * h; // with stride Width, which means this is the size of the memory area
// Set two position Pointers, respectively, to the source and target arrays
int posScan = 0;
rgbValues = new byte[scanBytes]; // Allocates memory for the target array
Marshal.Copy(ptr, rgbValues, 0, scanBytes); // Copy image data to rgbValues In the
bool isPass = true;
int i = 0, j = 0;
int cutW = (int)(bWidth * 0.02); //2% Width (modifiable)
int cutH = (int)(h * 0.02); //2% Height (modifiable)
int posLen = (int)(picByteSize * 8); // Keep looking for depth zero 8 Multiple of (modifiable)
// On the left
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;
}
// Skipping bytes of unused space per line of image data, length = stride - width * bytePerPixel
if (j == bWidth) posScan += offset;
else posScan += (offset + bWidth - j - 1);
}
// On the right
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;
}
// Skipping bytes of unused space per line of image data, length = stride - width * bytePerPixel
if (j != -1) posScan -= j;
}
// Memory to unlock
Marshal.Copy(rgbValues, 0, ptr, scanBytes);
bmp.UnlockBits(bmpData); // Unlock memory area
return bmp;
}
/// <summary>
/// When the black edges are removed, the adjacent black spots are removed
/// </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); // Close to the biggest 1% Height (modifiable)
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;
}
}