我正在使用 Windows Form C# 开发一个项目,我需要知道如何验证扫描文档中的特定位置。
我目前可以使用 WIA 或 Windows Image Acquisition 扫描文档。扫描后,我想检查扫描文档的下部是否有签名或任何类型的文字。
private void scanBtn_Click(object sender, EventArgs e)
{
try
{
// Find available scanner
var deviceManager = new DeviceManager();
DeviceInfo availableScanner = null;
for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
{
if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
{
continue;
}
availableScanner = deviceManager.DeviceInfos[i];
break;
}
// Connect to scanner and acquire image
var device = availableScanner.Connect();
var scannerItem = device.Items[1];
var imgFile = (ImageFile)scannerItem.Transfer(FormatID.wiaFormatJPEG);
// Convert WIA image to System.Drawing.Image
using (MemoryStream stream = new MemoryStream((byte[])imgFile.FileData.get_BinaryData()))
{
Bitmap bitmap = new Bitmap(stream);
// Crop the excess white margins
Bitmap croppedBitmap = CropWhiteMargins(bitmap);
// Extract the region of interest (ROI)
System.Drawing.Rectangle roiRect = new System.Drawing.Rectangle(0, 1072, 150, 202);
Bitmap roiBitmap = ExtractROI(croppedBitmap, roiRect);
// Save the scanned image
string fileName = $"ScanImg_{DateTime.Now:yyyyMMddHHmmss}.jpg";
string path = Path.Combine(@"D:\Scanned Images", fileName);
if (File.Exists(path))
{
File.Delete(path);
}
croppedBitmap.Save(path);
// Add the path to the list of scanned image paths
scannedImagePaths.Add(path);
// Display the scanned image
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.ImageLocation = path;
validationBox.SizeMode = PictureBoxSizeMode.Zoom;
validationBox.Image = roiBitmap; // Display the extracted ROI in the validation PictureBox
// Update the text to display the number of scanned pages
imageNum.Text = $"Document No. {scannedImagePaths.Count}";
}
}
catch (COMException ex)
{
MessageBox.Show(ex.Message);
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
如果您的签名位于同一个位置,并且您提取的 ROI 应该仅包含该签名(在确切的位置)或不包含该签名,那么也许您可以执行诸如是否全是白色像素之类的操作。
如果您希望该签名始终位于该位置,也许您可以对 ROI 进行 OCR 并根据图像解析为的文本执行某些操作(使用 Tesseract OCR 或其他东西)
如果您不知道签名始终位于该 ROI,那么也许可以使用 SIFT 之类的方法,您可以使用特征选择来判断图像是否具有签名。