当前位置:   article > 正文

使用C#的Winform实现图文识别OCR及截图功能_c#屏幕截图并识别文字

c#屏幕截图并识别文字

前言

       现在网上查资料的时候,有很多自己想复制下来的文章,但是却有限制,它会引导你去充值消费它们的VIP,从而得到权限复制。当然要想不充值就复制,办法不是没有,而我最常用的就是打开QQ软件,把想要复制的文章进行截图,然后提取图中文字。

QQ提取文字功能

在这里插入图片描述
       当然每次见到有意思的功能,自己都会去研究研究是怎么实现的。今天刚好就有时间,顺便复习一下Winform。刚做的时候我有在网上查相关资料。大部分都是介绍两种方式:

方式一、Asprise-OCR实现。

方式二、Microsoft Office Document Imaging(Office 2007) 组件实现。

我也有试了一试这两种方式。但是 (方式一)对中文不支持,而(方式二)却需要用户下载 组件 从而实现。当然如果开发一款 APP且需要用户下载其他插件才可以使用的话,那可大大降低了用户体验感了。所以只好抛弃这两种方式,采用百度AI的OCR图文识别接口

实现思路

【1】建立两个窗体,分别为主窗体和截图窗体。
【2】主窗体拥有 PictureBox 控件用于装载,截出来的图,或者预览本地图 片。
【3】也就是说,实现图文识别目标,分2种;1是从本地找出带文字的图片。2是截图。
【4】点击截图按钮的时候,立即调用截图窗体,而当前窗体Hide()隐藏掉。截图窗体本身为一个半透明的窗体。而截图的过程中最重要的是分为3步骤;
(1)鼠标按下时 : MouseDown 代表截图的开始,记录好鼠标按下时的坐标。
(2)鼠标移动时 : MouseMove 鼠标移动的同时,记录偏移坐标,同时 使用 Graphics 的 FillRectangle() 方法,根据鼠标移动的坐标填充一个矩形,且这个范围清除背景实现完全透明。代表截图的区域。
(3)鼠标释放时:MouseUp 记录当前坐标,关闭截图窗体,调用主窗体内生成截图的 Graphics 工具CopyFromScreen() 方法。并显示主窗体。代表截图结束。
【5】那么在生成截图完毕之后,给PictureBox的BackgroundImage设置背景图片。
【6】在PictureBox 中添加 contextMenuStrip控件,可以鼠标右键,复制图片。使用的是 Clipboard.SetImage()方法。复制到剪贴板。这样就可以任意将截好的图复制在任何可以粘帖的地方。
【7】点击识别文字按钮。首先你得需要去百度AI开放平台去申请图文识别应用获得相关 API_KEY 和 SECRET_KEY 。经过 client.GeneralBasic(); 读取图片路径二进制文件。也就是 用 IO文件的 ReadAllBytes()方法就好了 File.ReadAllBytes(imagePath);。最后百度OCR图文识别会返回相关JSON格式数据回来。要想赋值在 TextBox 中还需要进行解析。那在返回格式中有 “words_result_num”:字段那就好说了,代表结果数据的行数。之后进行循环就好了。那么就不多说了,直接上代码。

数据返回格式

{
  "log_id": xxxxxxxxxxxxxxxxxxxxx,
  "words_result_num": 11,
  "words_result": [
    {
      "words": "“OCR是英文 Optical Character Recognition的缩写,意思是光学字符识别"
    },
    {
      "words": "也可简单地称为文字识别,是文字自动输入的种方法。它通过扫描和摄像等"
    },
    {
      "words": "光学输入方式获取纸张上的文字图像信息,利用各种模式识别算法分析文字形"
    },
    {
      "words": "态特征可以将票据、报刊、书籍、文稿及其它印刷品转化为图像信息,再利用文"
    },
    {
      "words": "字识别技术将图像信息转化为可以使用的计算机翰入技术。可应用于银行票据、"
    },
    {
      "words": "大量文字资料、档案卷宗、文案的录入和处理领域。适合于银行、税务等行业大"
    },
    {
      "words": "量票据表格的自动扫描识别及长期存储。相对-般文本,通常以最终识别率、识"
    },
    {
      "words": "别速度、版面理解正确率及版面还原满意度4个方面作为OCR技术的评测依据」"
    },
    {
      "words": "而相对于表格及票据,通常以识别率或整张通过率及识别速度为测定OCR技术"
    },
    {
      "words": "的实用标准,随着人工智能的兴起,人们在追求让工作更简单化,OC识别技"
    },
    {
      "words": "术"
    }
  ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

实现步骤

1:主窗体界面

mainform

主界面代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Drawing.Imaging;
using System.Threading;


namespace ImageRecognition
{
    public partial class Mainform : Form
    {
        /// <summary>
        /// 声明当前窗体,用于截图完了之后调用
        /// </summary>
        public static Mainform currentForm = null;
        public string imagePath = "";
        public Mainform()
        {
            InitializeComponent();
            currentForm = this;
        }

    

        /// <summary>
        /// 点击浏览图片按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_imgpath_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            txt_imgpath.Text = openFileDialog1.FileName;
            pictureBox.BackgroundImage = Image.FromFile(txt_imgpath.Text);
            pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
        }
        
        /// <summary>
        /// 加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
        

        }
        
        /// <summary>
        /// 生成截图,截图完成并鼠标释放时调用
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public void GeneratingScreenshots(int x, int y, int width, int height)
        {
            try
            {
                pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
                Bitmap image; image = new Bitmap(width, height);
                Graphics g = Graphics.FromImage(image);
                //'截屏 CopyFromScreen,前两个参数表示截屏起点,第三和第四为绘制图像的起点。
                g.CopyFromScreen(x, y, 0, 0, new System.Drawing.Size(width, height));
                //当图片小于  pictureBox 控件的时候给他居中
                if (width < pictureBox.Width && height < pictureBox.Height)
                    pictureBox.BackgroundImageLayout = ImageLayout.Center;
                pictureBox.BackgroundImage = image;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 右击鼠标 点击复制图片时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 复制图片在剪贴板ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //判断 pictureBox 是否有图片 即判断是否已经截图
            if (pictureBox.BackgroundImage != null)      
            {
                //将图片已复制到剪贴板
                Clipboard.SetImage(pictureBox.BackgroundImage);
                MessageBox.Show("图片已复制到剪贴板", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// <summary>
        /// 点击截图按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Screenshot_Click(object sender, EventArgs e)
        {
            this.Hide();                                  //当前窗体隐藏
            Screenshot screenshot = new Screenshot();     //实例化截图窗体
            screenshot.ShowDialog();                      //显示截图窗体
        }

        /// <summary>
        /// 点击图文识别按钮的点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_BaiduOCR_Click(object sender, EventArgs e)
        {
            //百度图文识别应用的 API_KEY,以及 SECRET_KEY  ,可以登录去复制
            var API_KEY = "你的API_KEY";
            var SECRET_KEY = "你的SECRET_KEY";
            var client = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY);
            client.Timeout = 60000;  // 设置超时时间

            if (imagePath.IndexOf('.') == -1)
            {
                imagePath = txt_imgpath.Text;
            }
            else 
            {
                MessageBox.Show("您还未截图,或选择图片识别");
                return;
            }
            var image = File.ReadAllBytes(imagePath);
            try
            {
                //发送需要识别的图片
                var result = client.GeneralBasic(image);
                txt_result.Text = JsonGetStr(result.ToString());
            }
            catch (Exception ex)
            {
                txt_result.Text = ex.Message;
            }
        }

        /// <summary>
        /// 解析百度API返回的json格式扫描内容
        /// </summary>
        /// <param name="ReText"></param>
        /// <returns></returns>
        public string JsonGetStr(string ReText)
        {
            JObject obj = Newtonsoft.Json.Linq.JObject.Parse(ReText);
            string resMag = "";
            JToken record = obj["words_result"];
            JToken[] TrainInfoArr = record.ToArray();
            int arrLength = TrainInfoArr.Length;
            for (int i = 0; i < arrLength; i++)
            {
                resMag += TrainInfoArr[i]["words"].ToString();
            }
            return resMag;
        }

        private void 保存截图CtrlsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (pictureBox.BackgroundImage != null)
            {
                SaveFileDialog saveFile = new SaveFileDialog();
                saveFile.DefaultExt = "png";
                saveFile.Filter = "Png Files|*.png";
                Random ranNum = new Random();
                saveFile.FileName = "XiaoGang" + ranNum.Next(1000, 9999);
                imagePath = saveFile.FileName;
                DialogResult dialogResult = saveFile.ShowDialog();
                if (dialogResult == System.Windows.Forms.DialogResult.OK)
                {
                    pictureBox.BackgroundImage.Save(saveFile.FileName, ImageFormat.Png);
                }
            }
        }

    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187

截图窗体 :不需要拖取控件,只需要设置以下属性即可;

(1) Cursor属性为:Cross;代表鼠标滑过时,为一个十字架形状。
(2) FormorderStyle属性为:None;不需要任何外观形式,不显示最小化、最大化、和关闭按钮。
(3)WindowState属性为:Maximized;窗体为最大化;
(4)Optity属性为:50%;设置窗体透明度于截图完全透明区域形成对比。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace ImageRecognition
{
    public partial class Screenshot : Form
    {
        
        int x, y, nowX, nowY, width, height;
        bool isMouthDown = false;
        Graphics g;

        /// <summary>
        /// 鼠标释放
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form2_MouseUp(object sender, MouseEventArgs e)
        {
            nowX = MousePosition.X + 1;
            nowY = MousePosition.Y + 1;
            this.Close();
            Mainform.currentForm.GeneratingScreenshots(x < nowX ? x : nowX, y < nowY ? y : nowY, width, height);
            Mainform.currentForm.Show();
        }

     
        /// <summary>
        /// 鼠标移动的时候
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form2_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouthDown)
            {
                width = Math.Abs(MousePosition.X - x);         //获取宽
                height = Math.Abs(MousePosition.Y - y);        //获取高
                g = CreateGraphics();                          //开始创建一个Graphics
                g.Clear(this.BackColor);                       //清空背景颜色
                //根据鼠标移动的坐标填充一个矩形
                g.FillRectangle(Brushes.CornflowerBlue, x < MousePosition.X ? x : MousePosition.X, y < MousePosition.Y ? y : MousePosition.Y, width + 1, height + 1);
            }
        }

        /// <summary>
        /// 鼠标按下的时候
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form2_MouseDown(object sender, MouseEventArgs e)
        {
            x = MousePosition.X;
            y = MousePosition.Y;
            isMouthDown = true;
        }

      
        public Screenshot()
        {
            InitializeComponent();
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73

注意:忘记提到了,一定要添加 using Baidu.Aip.Ocr; 百度的图文识别OCR的引用。可在Visual studio中右击项目 管理Nuget程序包中去下载

GU
xz

项目整体运行视频

Winform仿qq截图,OCR图文识别

最后

好了今天的分享到此结束!!欢迎指出不足,诚恳接受批评。当然有问题的小伙伴可以下方评论。如果连OCR百度图文识别的API也懒得去申请,也可以私聊我。借给你们玩玩。

好朋友博客地址https://blog.csdn.net/weixin_43851854

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/368761?site
推荐阅读
相关标签
  

闽ICP备14008679号