使用Asprise OCR扫描一个部分

时间:2015-01-15 19:09:21

标签: c# ocr picturebox

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

2 个答案:

答案 0 :(得分:0)

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

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();
}

此代码允许您在要选择的区域周围拖出一个框,然后将旧图像替换为旧图像的选定区域。然后,您所要做的就是将新图像传递到您的OCR软件上。

答案 1 :(得分:0)

出于性能考虑,您无需制作新图像。 Asprise OCR允许您将您想要的区域作为参数传递给OCR。例如:

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

上面的代码指示OCR引擎在图像的左上角执行OCR,宽度为400像素,高度为200像素。

有关详细信息,请访问C# VB.NET OCR Developer's Guide: Perform OCR On Part Of The Image