基本信息
源码名称:OCR 实现 截图 并提取图片中的文字实例 附完整源码
源码大小:0.20M
文件格式:.rar
开发语言:C#
更新时间:2013-04-17
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
该实例实现了 截图并识别截图中的文字功能
该项目需要在安装 Microsoft Office Document Imaging
下载地址 如下: http://www.xdowns.com/soft/4/25/2009/Soft_50716.html
点击Capture之后 开始截图,截图的结果显示在如下窗体中
与此同时 将截图中的文字提取到下面的文本框中
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;
namespace DevOCR
{
public partial class ShowBitmap : Form
{
//这个Bitmap对象存储
//选择位图。
public Bitmap m_objBitmap;
public ShowBitmap()
{
InitializeComponent();
}
// This just draws the bitmap in the window.这只是绘制位图的窗口。
private void OnPaint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(m_objBitmap, 0, 0);
}
// We come here when the dialog has first appeared.我们来到这里时,对话框已首次出现。
private void timer1_Tick(object sender, EventArgs e)
{
// Turn the timer off so we don't
// do this multiple times.
//打开计时器关闭,因此我们不
//这样做多次。
timer1.Enabled = false;
// Ask user if this is what they want to do.问问用户如果这是他们想做的事情。
if (MessageBox.Show("Convert this to text?",
"Convert?", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
// Create a temp file name.创建一个临时文件的名称。
string strFileName = "temp" DateTime.Now.Millisecond ".tif";
try
{
// Save the bitmap object to a tiff file.位图对象保存到一个TIFF文件。
Console.WriteLine(m_objBitmap.PixelFormat.ToString());
m_objBitmap.Save(strFileName, ImageFormat.Tiff);
// Instantiate the MODI.Document object实例MODI.Document对象
MODI.Document md = new MODI.Document();
// The Create method grabs the picture from
// disk snd prepares for OCR.Create
//方法从抓起图片
//磁盘新区准备的光学字符识别。
md.Create(strFileName);
// Do the OCR.运行OCR
md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
// This string will contain the text.这个字符串将包含文本。
string strText = String.Empty;
// Get the first (and only image)获得第一个(也是唯一图片)
MODI.Image image = (MODI.Image)md.Images[0];
// Get the layout.获取布局。
MODI.Layout layout = image.Layout;
// Loop through the words.遍历文字
for (int j = 0; j < layout.Words.Count; j )
{
// Get this word.得到得到文字
MODI.Word word = (MODI.Word)layout.Words[j];
// Add a blank space to separate words.添加一个空格来分隔文字。
if (strText.Length > 0)
{
strText = " ";
}
// Add the word.添加文字
strText = word.Text;
}
// Close the MODI.Document object.关闭MODI.Document对象。
md.Close(false);
// Create the dialog that displays创建对话框显示
// the OCRed text.文本的文本识别。
ShowText st = new ShowText();
// The the dialog's text.该对话框的文本。
st.m_strOCRText = strText;
// Show the dialog.显示的对话框。
st.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
try
{
// Delete the temp file.删除临时文件。
File.Delete(strFileName);
}
catch { }
}
// Close this dialog.关闭此对话框。
Close();
}
}
}