首页 > 编程语言 >c# paddleorcsharp批量图片文字识别,拖拽实现,正则表达式提取

c# paddleorcsharp批量图片文字识别,拖拽实现,正则表达式提取

时间:2022-11-30 00:45:15浏览次数:45  
标签:c# Text object System paddleorcsharp listBox1 using Data 拖拽

using PaddleOCRSharp;
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 PaddleOCRSharp_识别
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        string[] imgpaths;
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
            ofd.Multiselect = true;
            ofd.ShowDialog();
            imgpaths = ofd.FileNames;

            listBox1.Items.AddRange(imgpaths);
        }

        OCRModelConfig config = null;
        //使用默认参数
        OCRParameter oCRParameter = new OCRParameter();

        private void button2_Click(object sender, EventArgs e)
        {
            //识别结果对象
            OCRResult ocrResult = new OCRResult();
            //建议程序全局初始化一次即可,不必每次识别都初始化,容易报错。     
            PaddleOCREngine engine = new PaddleOCREngine(config, oCRParameter);

            //不能用foreach循环,因为foreach操作items改变了listbox,导致不能索引选中
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                ocrResult = engine.DetectText(listBox1.Items[i].ToString());

                if (ocrResult != null)
                {

                    listBox1.SetSelected(i, true);

                    textBox1.Text += ocrResult.Text + "\r\n" + "\t";
                }
            }
        }



        private void button3_Click(object sender, EventArgs e)
        {
            this.textBox1.SelectAll();
            Clipboard.SetDataObject(this.textBox1.Text);
            MessageBox.Show("复制成功!");
        }



        //拖拽实现:拖入enter+拖入drop ,2个不能少
        private void listBox1_DragEnter(object sender, DragEventArgs e)
        {
            //拖放文件,判断是取文本内容还是文件的link链接
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                //如果是文件取链接
                e.Effect = DragDropEffects.Link;
            }
            else
            {
                //是文本取内容
                if (e.Data.GetDataPresent(DataFormats.Text))
                {
                    e.Effect = DragDropEffects.Copy; //有copy move link等
                }
            }


        }
        string[] a;
        private void listBox1_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                //将获得的链接object强转为string[],加入listbox
                a = ((string[])(e.Data.GetData(DataFormats.FileDrop)));
                listBox1.Items.AddRange(a);
            }
            else
            {
                //是文本取内容
                if (e.Data.GetDataPresent(DataFormats.Text))
                {
                    textBox1.Text = e.Data.GetData(DataFormats.Text).ToString();
                }
            }

        }

        private void button4_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
        }
    }
}

  

标签:c#,Text,object,System,paddleorcsharp,listBox1,using,Data,拖拽
From: https://www.cnblogs.com/yaoyue68/p/16937194.html

相关文章