2015-01-15 92 views
1

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

回答

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软件即可。

0

您不需要为了性能而制作新的图像。 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