Form1.cs
6.7 KB
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
188
189
190
191
192
193
194
195
196
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
using Newtonsoft;
using Newtonsoft.Json;
using System.Runtime.InteropServices;
using System.Runtime.ExceptionServices;
using System.Security.Cryptography.X509Certificates;
namespace paddleOCR
{
public partial class Paddle : Form
{
public Paddle()
{
InitializeComponent();
PaddleOCRHelper.Init();
}
private void button1_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Filter = "image files All files (*.*)|*.*";
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog.FileName;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
string path = System.Windows.Forms.Application.StartupPath + @"\paddleOCR.py";//py文件路径
if (string.IsNullOrEmpty(textBox1.Text))
{
return;
}
this.Invoke(new Action(() =>
{
startTime=DateTime.Now;
this.textBox2.Text = PaddleOCRHelper.StartPythonOcr(textBox1.Text);
this.label1.Text = $"elapsed:{(DateTime.Now - startTime).TotalSeconds.ToString("f2")}s";
try
{
Stream s = File.Open(System.Windows.Forms.Application.StartupPath + @"\ocr_result.jpg", FileMode.Open);
// pictureBox1.Image = Bitmap.FromStream(s);
s.Close();
s.Dispose();
}
catch
{
}
}));
}
catch (Exception e1)
{
MessageBox.Show(e1.Message);
}
}
DateTime startTime;
public static List<T> DeserializeJsonToList<T>(string json) where T : class
{
JsonSerializer serializer = new JsonSerializer();
StringReader sr = new StringReader(json);
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List<T>));
List<T> list = o as List<T>;
return list;
}
public void outputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
{
this.Invoke(new Action(() =>
{
this.textBox2.Text = e.Data;
this.label1.Text = $"elapsed:{(DateTime.Now - startTime).TotalSeconds.ToString("f2")}s";
try
{
Stream s = File.Open(System.Windows.Forms.Application.StartupPath + @"\ocr_result.jpg", FileMode.Open);
// pictureBox1.Image = Bitmap.FromStream(s);
s.Close();
s.Dispose();
}
catch
{
}
}));
}
}
private void button3_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.InitialDirectory = "c:\\";
openFileDialog.Filter = "python files (*.exe)|*.exe|All files (*.*)|*.*";
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog.FileName;
}
}
}
private void button4_Click(object sender, EventArgs e)
{
try
{
this.Invoke(new Action(() =>
{
startTime= DateTime.Now;
this.textBox2.Text = PaddleOCRHelper.StartCPlusOcr(textBox1.Text);
this.label1.Text = $"elapsed:{(DateTime.Now - startTime).TotalSeconds.ToString("f2")}s";
try
{
Stream s = File.Open(System.Windows.Forms.Application.StartupPath + @"\ocr_result.jpg", FileMode.Open);
// pictureBox1.Image = Bitmap.FromStream(s);
s.Close();
s.Dispose();
}
catch
{
}
}));
}
catch (Exception e1)
{
MessageBox.Show(e1.Message);
}
}
private NotifyIcon notify;
private ContextMenuStrip notifyMenu;
bool exit=false;
DeviceLibrary.Service service;
string url = ConfigHelper.Config.Get("url", "http://localhost:8090");
private void Form1_Load(object sender, EventArgs e)
{
service = new DeviceLibrary.Service();
service.Open(url);
//托盘菜单
notifyMenu = new ContextMenuStrip();
ToolStripMenuItem itemShow = new ToolStripMenuItem("显示(&S)");
itemShow.Click += ItemShow_Click;
ToolStripMenuItem itemExit = new ToolStripMenuItem("退出(&X)");
itemExit.Click += ItemExit_Click;
notifyMenu.Items.Add(itemShow);
notifyMenu.Items.Add(itemExit);
//托盘控件
notify = new NotifyIcon { Icon = Icon, Visible = true, ContextMenuStrip = notifyMenu, Text = Text };
this.WindowState= FormWindowState.Minimized;
ShowInTaskbar = false;
}
private void ItemShow_Click(object sender, EventArgs e)
{
Show();
if (WindowState == FormWindowState.Minimized)
WindowState = FormWindowState.Normal;
}
private void ItemExit_Click(object sender, EventArgs e)
{
notify.Dispose();
exit = true;
Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (!exit)
{
e.Cancel = true;
Hide();
}
}
private void Paddle_FormClosed(object sender, FormClosedEventArgs e)
{
service.Close();
}
}
}