使用IronOCR识别图片文字
程序员文章站
2022-04-15 16:29:51
...
最近实验课要做一个图片文字识别
,需求是Winform中有一个图片,进行框选后,识别框选中的内容,老师提示说去找OCR的开源库,所以我就找到了IronOCR
IronOCR:The C# OCR Library : Iron OCR
IronOcr makes it easy to read text from images in your .net apps & websites.
1.Read text and barcodes from scanned images & PDFs
2.Supports multiple international languages
3.Output as plain text or structured data
框选
进行识别前,必须框选出识别区域,所以就要先去实现框选,思路就是通过MouseUp
、MouseMove
、MouseDown
三个事件去绘制一个矩形,类似于拖选
private bool _mouseIsDown = false;
private Rectangle _selectArea = Rectangle.Empty;
private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
// 修正Width和Height可能为负数的问题
if (_selectArea.Width < 0)
{
_selectArea.Width = -_selectArea.Width;
_selectArea.X -= _selectArea.Width;
}
if (_selectArea.Height < 0)
{
_selectArea.Height = -_selectArea.Height;
_selectArea.Y -= _selectArea.Height;
}
Cursor.Clip = Rectangle.Empty;
_mouseIsDown = false;
DrawRectangle();
_selectArea = Rectangle.Empty;
}
private void pictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (_mouseIsDown)
ResizeToRectangle(e.Location);
}
/// <summary>
/// 初始化选择框
/// </summary>
/// <param name="startPoint"></param>
private void DrawStart(Point startPoint)
{
//指定工作区为整个控件
Cursor.Clip = RectangleToScreen(new Rectangle(pictureBox.Location.X, pictureBox.Location.Y,
pictureBox.Width, pictureBox.Height));
_selectArea = new Rectangle(startPoint.X + pictureBox.Location.X,
startPoint.Y + pictureBox.Location.Y,1,1);
}
/// <summary>
/// 在鼠标移动的时改变选择框的大小
/// </summary>
/// <param name="p">鼠标的位置</param>
private void ResizeToRectangle(Point p)
{
_selectArea.Width = p.X + pictureBox.Location.X - _selectArea.Left;
_selectArea.Height = p.Y + pictureBox.Location.Y - _selectArea.Top;
DrawRectangle();
}
/// <summary>
/// 绘制选择框
/// </summary>
private void DrawRectangle()
{
pictureBox.Refresh();
var rect = RectangleToScreen(_selectArea);
ControlPaint.DrawReversibleFrame(rect, Color.White, FrameStyle.Thick);
}
图片文字识别
IronOCR的使用很简单,官网给出的教程:
using IronOcr;
//..
var Ocr = new AutoOcr();
var Result = Ocr.Read(@"C:\path\to\image.png");
Console.WriteLine(Result.Text);
所以接下来只需要将框选区域进行截图
,然后交给OCR识别即可
private readonly AutoOcr _ocr;
public MainForm()
{
InitializeComponent();
_ocr = new AutoOcr();
// 强制初始化OCR
var img = new Bitmap(1,1);
_ocr.Read(img);
}
// 框选的代码.......
/// <summary>
/// 识别框选的单词/句子
/// </summary>
/// <returns>识别的结果</returns>
private string SelectText()
{
var rect = RectangleToScreen(_selectArea);
var img = new Bitmap(rect.Width, rect.Height);
var graphics = Graphics.FromImage(img);
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.CopyFromScreen(rect.Left, rect.Top, 0, 0,
new Size(rect.Width, rect.Height));
var result = _ocr.Read(img);
Debug.WriteLine(result.Text);
return result.Text;
}
最后在MouseUp
事件的时候调用SelectText
即可,然后拿着识别出来的文字去干啥都行
项目源码:OCRDemo