java手写识别源代码.docx
《java手写识别源代码.docx》由会员分享,可在线阅读,更多相关《java手写识别源代码.docx(23页珍藏版)》请在冰豆网上搜索。
![java手写识别源代码.docx](https://file1.bdocx.com/fileroot1/2022-11/23/2c1c97fe-58a0-41f3-97a2-9259ff881f63/2c1c97fe-58a0-41f3-97a2-9259ff881f631.gif)
java手写识别源代码
java手写识别源代码
1./*
2. * @(#)HandWritingRecognition.java 1.0 04/04/03
3. *
4. * You can modify the template of this file in the
5. * directory ..JCreatorTemplatesTemplate_1Project_Name.java
6. *
7. * You can also create your own project template by making a new
8. * folder in the directory ..JCreatorTemplate. Use the other
9. * templates as examples.
10. *
11. */
12.package myprojects.handwritingrecognition;
13.
14.import java.awt.*;
15.import java.awt.event.*;
16.import javax.swing.*;
17.import java.lang.*;
18.import java.io.*;
19.
20.class HandWritingRecognition extends JFrame implements ActionListener,ItemListener{
21. JButton recognize;
22. JButton train;
23. JButton clear;
24. JButton newSample;
25. JLabel result1;
26. JTextArea result2;
27. JButton choice1;
28. JButton choice2;
29. JTextArea status;
30. JComboBox num;
31. myCanvas can;
32. int model;
33. FileDialog fdsave;
34. FileDialog fdload;
35. BufferedWriter out;
36. FileWriter file_writer;
37. BufferedReader in;
38. FileReader file_reader;
39. JMenuItem save;
40. JMenuItem load;
41. JMenuItem quit;
42. JMenuItem ab;
43. JMenuItem edition;
44. JTextArea area;
45. int first,second;
46. int choice;
47. int resultLen=10;
48. int snum=10;
49.
50. int [][]sample;
51. int [][]input;
52. int []row;
53. int count;
54. int stroke;
55. int []x;
56. int []y;
57. int []dismantle;
58. int []col;
59. int [][]sample2;
60. int d1,d2;
61. boolean isSaved;
62.
63.public HandWritingRecognition() {
64. sample=new int[10][12];
65. sample2=new int[10][8];
66. input=new int[12][8];
67. row=new int[12];
68. count=0;
69. stroke=0;
70. x=new int[500];
71. y=new int[500];
72. model=0;
73. dismantle=new int[10];
74. col=new int[8];
75. d1=d2=0;
76. isSaved=true;
77.
78. fdsave=new FileDialog(this,"保存文件",FileDialog.SAVE);
79. fdload=new FileDialog(this,"打开文件",FileDialog.LOAD);
80. setSize(470,520);
81. setLocation(300,100);
82. setTitle("数字手写识别器");
83.
84. JMenuBar Bar=new JMenuBar();
85. Bar.setOpaque(true);
86. JMenu file=new JMenu("文件");
87. JMenu about=new JMenu("关于");
88. Bar.add(file);
89. Bar.add(about);
90. setJMenuBar(Bar);
91. save=new JMenuItem("保存样本");
92. load=new JMenuItem("读取样本");
93. quit=new JMenuItem("退出");
94. file.add(save);
95. file.add(load);
96. file.addSeparator();
97. file.add(quit);
98. ab=new JMenuItem("说明");
99. edition=new JMenuItem("版本");
100. about.add(ab);
101. about.addSeparator();
102. about.add(edition);
103. save.addActionListener(this);
104. load.addActionListener(this);
105. quit.addActionListener(this);
106. ab.addActionListener(this);
107. edition.addActionListener(this);
108.
109. can=new myCanvas();
110.
111. Container con=getContentPane();
112. con.setLayout(null);
113.
114. JPanel p1=new JPanel();
115. p1.setLayout(null);
116. p1.add(can);
117. can.setBounds(15,15,270,270);
118. p1.setBorder(BorderFactory.createTitledBorder("手写数字输入板"));
119. con.add(p1);
120. p1.setBounds(20,60,300,300);
121.
122. recognize=new JButton("识 别");
123. clear=new JButton("清 除");
124. newSample=new JButton("新建样本");
125. train=new JButton("训 练");
126. choice1=new JButton();
127. choice2=new JButton();
128. con.add(recognize);
129. recognize.setBounds(340,70,80,30);
130. con.add(clear);
131. clear.setBounds(340,120,80,30);
132. con.add(newSample);
133. newSample.setBounds(20,15,90,30);
134. con.add(train);
135. train.setBounds(340,170,80,30);
136. String []s={"0","1","2","3","4","5","6","7","8","9"};
137. num=new JComboBox(s);
138. JPanel p4=new JPanel();
139. con.add(p4);
140. p4.setLayout(null);
141. p4.setBounds(340,210,80,50);
142. p4.setBorder(BorderFactory.createTitledBorder("训练项"));
143. p4.add(num);
144. num.setBounds(10,15,55,25);
145.
146. JPanel p2=new JPanel();
147. con.add(p2);
148. p2.setLayout(null);
149. p2.setBounds(340,260,80,100);
150. p2.setBorder(BorderFactory.createTitledBorder("结果备选"));
151. p2.add(choice1);
152. p2.add(choice2);
153. choice1.setBounds(10,20,60,30);
154. choice2.setBounds(10,60,60,30);
155.
156. result1=new JLabel("识别结果:
");
157. result2=new JTextArea();
158. result2.setEditable(false);
159. con.add(result1);
160. con.add(result2);
161. result1.setBounds(150,15,70,30);
162. result2.setBounds(230,15,170,30);
163.
164. JPanel p3=new JPanel();
165. con.add(p3);
166. p3.setLayout(null);
167. p3.setBounds(20,370,400,70);
168. p3.setBorder(BorderFactory.createTitledBorder("STATE:
"));
169. area=new JTextArea();
170. p3.add(area);
171. area.setBounds(10,15,380,45);
172. area.setEditable(false);
173.
174. recognize.setEnabled(false);
175. choice1.setEnabled(false);
176. choice2.setEnabled(false);
177. train.setEnabled(false);
178. num.setEnabled(false);
179. clear.setEnabled(false);
180. area.setText("请新建或载入手写体样本。
");
181.
182. recognize.addActionListener(this);
183. clear.addActionListener(this);
184. train.addActionListener(this);
185. newSample.addActionListener(this);
186. choice1.addActionListener(this);
187. choice2.addActionListener(this);
188. num.addActionListener(this);
189.
190.
191.
192.addWindowListener(new WindowAdapter() {
193.public void windowClosing(WindowEvent e) {
194.
195. if(isSaved==false){
196. String s="是否要保存已修改的样本到样本文件?
";
197. String t="提示";
198. int check;
199. JFrame f=new JFrame();
200. check=JOptionPane.showConfirmDialog(f,s,t,0);
201. if(check==0){
202. fdsave.setVisible(true);
203. try{
204. if(fdsave.getFile()!
=null){
205. File file=new File(fdsave.getDirectory(),fdsave.getFile());
206. file_writer=new FileWriter(file);
207. out=new BufferedWriter(file_writer);
208. for(int i=0;i<10;i++){
209. for(int j=0;j<12;j++){
210. Integer aa=new Integer((int)sample[i][j]);
211. s=aa.toString();
212. out.write(s,0,s.length());
213. out.flush();
214. out.newLine();
215. }
216. for(int j=0;j<8;j++){
217. Integer aa=new Integer((int)sample2[i][j]);
218. s=aa.toString();
219. out.write(s,0,s.length());
220. out.flush();
221. out.newLine();
222. }
223. }
224. out.close();
225. file_writer.close();
226. }
227. }catch(FileNotFoundException e1){}
228. catch(IOException e2){}
229. }
230. isSaved=true;
231. }
232. dispose();
233.System.exit(0);
234.}
235.});
236.
237.setVisible(true);
238.show();
239.}
240.
241.public void standard(){
242. int minx,miny,maxx,maxy,xlen,ylen;
243. minx=miny=1000;
244. xlen=ylen=0;
245. maxx=maxy=0;
246. for(int i=0;i247. if(x[i]==-1){
248. continue;
249. }
250. if(x[i]251. minx=x[i];
252. }
253. if(y[i]254. miny=y[i];
255. }
256. if(x[i]>maxx){
257. maxx=x[i];
258. }
259. if(y[i]>maxy){
260. maxy=y[i];
261. }
262. }
263. System.out.println(maxx+" "+maxy+" "+minx+" "+miny );
264. xlen=maxx-minx;
265. ylen=maxy-miny;
266. int tempi,tempj;
267. for(int i=0;i268. if(x[i]==-1){
269. continue;
270. }
271. tempj=(x[i]-minx)*8/xlen;
272. tempi=(y[i]-miny)*12/ylen;
273. if(tempi>11)tempi=11;
274. if(tempj>7)tempj=7;
275. if(tempi<0)tempi=0;
276. if(tempj<0)tempj=0;
277. input[tempi][tempj]=1;
278. }
279. for(int i=0;i<12;i++){
280. int c=0;
281. for(int j=0;j<8;j++){
282. if(input[i][j]==1){
283. row[i]+=j;
284. c++;
285. }
286. }
287. row[i]*=c;
288. }
289. for(int i=0;i<8;i++){
290. int c=0;
291. for(int j=0;j<12;j++){
292. if(input[j][i]==1){
293. col[i]+=j;
294. c++;
295. }
296. }
297. col[i]*=c;
298. }
299.
300. for(int i=0;i<12;i++){
301. for(int j=0;j<8;j++){
302. System.out.print(input[i][j]+" ");
303. }
304. System.out.print(" "+row[i]+"n");
305. }
306. for(int i=0;i<8;i++){
307. System.out.print(col[i]+" ");
308. }
309. System.out.println("");
310. }
311.
312.
313.public void actionPerformed(ActionEvent e){
314. if(e.getSource()==quit){
315.
316.
317. if(isSaved==false){
318. String s="是否要保存以修改的样本到样本文件?
";
319. String t="提示";
320. int check;
321. JFrame f=new JFrame();
322. check=JOptionPane.showConfirmDialog(f,s,t,0);
323. if(check==0){
324.