小隐的博客

人生在世,笑饮一生
随笔 - 304, 文章 - 0, 评论 - 349, 阅读 - 50万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

截图之后自动识别(OCR)

Posted on   隐客  阅读(942)  评论(0编辑  收藏  举报

有个鸟软件因为做的烂,连导出都没有,不得不说现在的识别还是非常好的,特别是截图的文字,识别率太高了,

然后自己一边翻页,一边截图识别,十几下搞完了,截图和识别还是比较简单的,主要代码放出来

 

复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace COCR
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            this.Hide();
            Form2 f2 = new Form2(this);
             
            f2.Show();

        }
        public void settext(string text)
        {
            this.textBox1.Text = text;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(textBox1.Text);
        }
    }
}
复制代码
复制代码
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.Windows.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace COCR
{
    public partial class Form2 : Form
    {
        public Form2(Form1 frm)
        {
            
            InitializeComponent();
            frmmain = frm;
            
        }
        public Form2()
        {
            InitializeComponent();
        }
        public Form1 frmmain = null;
        int i = 0;
        Bitmap bit;
        Bitmap bit2;
        Bitmap copy;
        int x;
        int y;
        int xx;
        int yy;
        Pen pen = new Pen(Color.Red, 2);
        Rectangle rect = new Rectangle();
        Graphics g2;
        private void Form2_Load(object sender, EventArgs e)
        {
             
            i++;
            //Opacity = 0.5;
            WindowState = FormWindowState.Minimized;
            FormBorderStyle = FormBorderStyle.None;
            this.Cursor = Cursors.Cross;
            int w = Screen.PrimaryScreen.Bounds.Width;
            int h = Screen.PrimaryScreen.Bounds.Height;
            bit = new Bitmap(w, h);//获取桌面图
            Graphics g = Graphics.FromImage(bit);//把图画出来
            g.CopyFromScreen(0, 0, 0, 0, new Size(w, h));
            pictureBox1.Image = bit;
            //bit.Save("C:/Users/Administrator/Desktop/区域截图" + i + ".jpg");
            WindowState = FormWindowState.Maximized;
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                frmmain.Show();
                frmmain.WindowState = FormWindowState.Normal;
                this.Close();
                //this.Hide();
                //Form1 f1 = new Form1();
                //f1.Show();
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            x = Math.Abs(e.X);
            y = Math.Abs(e.Y);
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                xx = Math.Abs(e.X);
                yy = Math.Abs(e.Y);
                Graphics g = pictureBox1.CreateGraphics();
                rect = new Rectangle(x, y, xx - x, yy - y);
                Refresh();
                g.DrawRectangle(pen, rect);

            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            try
            {
                copy = (Bitmap)bit.Clone();
                bit2 = new Bitmap(rect.Width, rect.Height);
                g2 = Graphics.FromImage(bit2);
                g2.DrawImage(copy, new Rectangle(0, 0, rect.Width, rect.Height), rect, GraphicsUnit.Pixel);
                pictureBox1.Image = bit2;
                //bit2.Save("C:/Users/Administrator/Desktop/区域截图" + i + ".jpg");
                //Clipboard.SetImage(bit2);
                this.Close();
                ocr(imageToByte(bit2));

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Application.Exit();
            }
        }
        private void ocr(byte[] img)
        {
        //百度的
var APP_ID = "aa"; var API_KEY = "aa"; var SECRET_KEY = "aa"; var client = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY); client.Timeout = 60000; // 修改超时时间 //var result = client.GeneralBasic(img); JObject json = client.AccurateBasic(img); Console.WriteLine(json); StringBuilder sb = new StringBuilder(); JArray record = (JArray)json["words_result"] ; for (int j = 0; j < record.Count; j++) { sb.Append(record[j]["words"].ToString() + Environment.NewLine ); } //foreach (JProperty jp in record) //{ // sb.Append(jp.Value +"\n"); //} Console.WriteLine(sb); frmmain.settext(sb.ToString ()); frmmain.Show(); frmmain.WindowState = FormWindowState.Normal; } private byte[] imageToByte(System.Drawing.Image _image) { MemoryStream ms = new MemoryStream(); _image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); return ms.ToArray(); } //将byte[]数据转换成image private Image byteToImage(byte[] myByte) { MemoryStream ms = new MemoryStream(myByte); Image _Image = Image.FromStream(ms); return _Image; } } }
复制代码

 

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
QQ交流
点击右上角即可分享
微信分享提示