使用 Asprise OCR 扫描一个部分

Scan a Section using Asprise OCR

我下载了Asprise制作的OCR样本。我喜欢它,因为它非常快。示例中只有我一个小问题:导入图片时,无法select 将图片中要转换为TXT 的部分。如何在 pictureBox 中查看图像和 select(我认为使用两点)要扫描的部分?

您需要做的是创建一个裁剪工具。假设您的图像位于图片框控件中,它将如下所示:

private bool _isSelecting;
private Rectangle _selectionRectangle;

private void Form1_Load(object sender, EventArgs e)
{
    _isSelecting = false;
}

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        _selectionRectangle = new Rectangle(e.X, e.Y, 0, 0);
        pictureBox1.Invalidate();
        _isSelecting = true;
    }
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        _selectionRectangle = new Rectangle(_selectionRectangle.Left, _selectionRectangle.Top,
            e.X - _selectionRectangle.Left, e.Y - _selectionRectangle.Top);
        pictureBox1.Invalidate();
    }
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    if (!_isSelecting)
        return;
    using (var pen = new Pen(Color.Red, 2))
    {
        e.Graphics.DrawRectangle(pen, _selectionRectangle);
    }
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    using (var bmp = new Bitmap(pictureBox1.Image))
    {
        if (_selectionRectangle.Width + _selectionRectangle.X > pictureBox1.Image.Width)
            _selectionRectangle.Width = pictureBox1.Image.Width - _selectionRectangle.X;
        if (_selectionRectangle.Height + _selectionRectangle.Y > pictureBox1.Image.Height)
            _selectionRectangle.Height = pictureBox1.Image.Height - _selectionRectangle.Y;

        var selectedBmp = bmp.Clone(_selectionRectangle, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        pictureBox1.Image = selectedBmp;
    }
    _isSelecting = false;
    pictureBox1.Invalidate();
}

此代码将允许您在想要 select 的区域周围拖出一个框,然后它将用旧图像的 selected 区域替换旧图像。然后您所要做的就是将新图像传递到您的 OCR 软件中。

您不必为了性能而制作新图像。 Asprise OCR 允许您将想要的区域作为参数传递给 OCR。例如:

string s = ocr.Recognize("img.jpg", -1, 0, 0, 400, 200,
   AspriseOCR.RECOGNIZE_TYPE_ALL, AspriseOCR.OUTPUT_FORMAT_PLAINTEXT);

以上代码指示OCR引擎对宽400像素、高200像素的图片左上角部分进行OCR

更多详情,请访问C# VB.NET OCR Developer's Guide: Perform OCR On Part Of The Image