数字图像识别JAVA代码
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
", JOptionPane.ERROR_MESSAGE);
} });
btFresh.addActionListener(new ActionListener() { // 刷新按钮监听 public void actionPerformed(ActionEvent e) { p2.removeAll(); // 先移除,否则出现重叠 p3.removeAll();
c.setBackground(new Color(204, 245, 255)); p1.setBackground(new Color(204, 245, 255)); p2.setBackground(new Color(204, 245, 255)); p3.setBackground(new Color(204, 245, 255));
package pkg;
import javax.swing.*;
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
/** * 图形化界面 */
public class ImageApp extends JApplet { private static final long serialVersionUID = 1L; static int i, j, k, r; Container c; JPanel p1, p2, p3; JButton btReco, btCent, btFresh; JLabel label1, label2, label3, label4; JTextField write;
p3.add(write); p3.add(btFresh); write.updateUI(); btFresh.updateUI(); p3.updateUI();
c.add(p2, BorderLayout.CENTER); } }); }
private int random() { // 返回随机数 0-9 return (int) (Math.random() * 10);
new ImageRecognitor(k);
label4 = new JLabel("验证码", new ImageIcon("../img/00" + r + ".png"), SwingConstants.CENTER);
new ImageRecognitor(r);
System.out.println(ImageRecognitor.numString); //输出识别出的数字串 write = new JTextField(8); write.setOpaque(false);
new ImageRecognitor(i);
label2 = new JLabel(new ImageIcon("../img/00" + j + ".png"), SwingConstants.CENTER);
new ImageRecognitor(j);
label3 = new JLabel(new ImageIcon("../img/00" + k + ".png"), SwingConstants.CENTER);
p1.add(btReco); p1.add(btCent); p2.add(label1); p2.add(label2); p2.add(label3); p2.add(label4); p3.add(write); p3.add(btFresh); c.add(p1, BorderLayout.SOUTH); c.add(p2, BorderLayout.CENTER); c.add(p3, BorderLayout.NORTH);
c.setBackground(new Color(204, 245, 255)); p2.setBackground(new Color(204, 245, 255));
/Baidu Nhomakorabea*
* 重绘面板 */ p2.add(label1); p2.add(label2); p2.add(label3); p2.add(label4); label1.updateUI(); label2.updateUI(); label3.updateUI(); label4.updateUI(); p2.updateUI();
i = random(); j = random(); k = random(); r = random(); ImageRecognitor.numString = null;
label1 = new JLabel(new ImageIcon("../img/00" + i + ".png"), SwingConstants.CENTER); // 单张图片识别
new ImageRecognitor(k);
label4 = new JLabel("验证码", new ImageIcon("../img/00" + r + ".png"), SwingConstants.CENTER);
new ImageRecognitor(r); write = new JTextField(8); write.setOpaque(false);
btReco = new JButton("识别"); btCent = new JButton("确定"); btFresh = new JButton("刷新");
btReco.setContentAreaFilled(false); btCent.setContentAreaFilled(false); btFresh.setContentAreaFilled(false);
if (write.getText().equals(ImageRecognitor.numString)) { JOptionPane.showMessageDialog(null, "输入正确!", "辨识正确", JOptionPane.INFORMATION_MESSAGE);
} else JOptionPane.showMessageDialog(null, "验证码输入错误!", "辨识错误
}
/** * 得到二值化矩阵 */
public static int[][] getSymbolMatrix(int[] pixels, int sparseFactor) { final int width = ImageModel.width; final int height = ImageModel.height; int[][] ret = new int[width][height];
} }
package pkg;
/** * Created on 2014/4/25. */
import java.awt.Image; import java.awt.image.PixelGrabber; import java.util.logging.Level; import java.util.logging.Logger;
if (matrix[j][i] != 0) { if (i < 6) { // 纵向 if (j < 6) { // 横向 top_l++; } else { top_r++; } } else if (i < 12) { cet++; } else { if (j < 6) { bot_l++; } else { bot_r++; } }
for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (pixels[i * width + j] == -1) { ret[j][i] = 0; } else { ret[j][i] = 1; } }
} return ret; }
/** * 输出字符矩阵 */
public static void displayMatrix(int[][] matrix) { System.out.println(" "); int top_l = 0; int top_r = 0; int cet = 0; int bot_l = 0; int bot_r = 0; int k = 0;
public void init() { new ImageModel(); c = getContentPane(); p1 = new JPanel(new FlowLayout()); p2 = new JPanel(new FlowLayout(1, 0, 0)); p3 = new JPanel(new FlowLayout());
new ImageRecognitor(i);
label2 = new JLabel(new ImageIcon("../img/00" + j + ".png"), SwingConstants.CENTER);
new ImageRecognitor(j);
label3 = new JLabel(new ImageIcon("../img/00" + k + ".png"), SwingConstants.CENTER);
i = random(); j = random(); k = random(); r = random(); ImageRecognitor.numString = null;
label1 = new JLabel(new ImageIcon("../img/00" + i + ".png"), SwingConstants.CENTER); // 单张图片识别
double v_t_l = 0; double v_t_r = 0; double v_c = 0; double v_b_l = 0; double v_b_r = 0;
for (int i = 0; i < matrix[0].length; i++) { for (int j = 0; j < matrix.length; j++) { if (matrix[j][i] != 0) { System.out.print("*"); } else { System.out.print(" "); }
btReco.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { write.setText(ImageRecognitor.numString); }
});
btCent.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println(write.getText()); System.out.println(ImageRecognitor.numString);
public class ImageMatrix { static int k = 0; static double[] total = new double[10]; static double totalPic;
public static int[] getPixels(Image image, int width, int height) { // 得到像素大小 int[] pixels = new int[width * height]; try { new PixelGrabber(image, 0, 0, width, height, pixels, 0, width) .grabPixels(); } catch (InterruptedException ex) { Logger.getLogger(ImageMatrix.class.getName()).log(Level.SEVERE, null, ex); } return pixels;
} });
btFresh.addActionListener(new ActionListener() { // 刷新按钮监听 public void actionPerformed(ActionEvent e) { p2.removeAll(); // 先移除,否则出现重叠 p3.removeAll();
c.setBackground(new Color(204, 245, 255)); p1.setBackground(new Color(204, 245, 255)); p2.setBackground(new Color(204, 245, 255)); p3.setBackground(new Color(204, 245, 255));
package pkg;
import javax.swing.*;
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
/** * 图形化界面 */
public class ImageApp extends JApplet { private static final long serialVersionUID = 1L; static int i, j, k, r; Container c; JPanel p1, p2, p3; JButton btReco, btCent, btFresh; JLabel label1, label2, label3, label4; JTextField write;
p3.add(write); p3.add(btFresh); write.updateUI(); btFresh.updateUI(); p3.updateUI();
c.add(p2, BorderLayout.CENTER); } }); }
private int random() { // 返回随机数 0-9 return (int) (Math.random() * 10);
new ImageRecognitor(k);
label4 = new JLabel("验证码", new ImageIcon("../img/00" + r + ".png"), SwingConstants.CENTER);
new ImageRecognitor(r);
System.out.println(ImageRecognitor.numString); //输出识别出的数字串 write = new JTextField(8); write.setOpaque(false);
new ImageRecognitor(i);
label2 = new JLabel(new ImageIcon("../img/00" + j + ".png"), SwingConstants.CENTER);
new ImageRecognitor(j);
label3 = new JLabel(new ImageIcon("../img/00" + k + ".png"), SwingConstants.CENTER);
p1.add(btReco); p1.add(btCent); p2.add(label1); p2.add(label2); p2.add(label3); p2.add(label4); p3.add(write); p3.add(btFresh); c.add(p1, BorderLayout.SOUTH); c.add(p2, BorderLayout.CENTER); c.add(p3, BorderLayout.NORTH);
c.setBackground(new Color(204, 245, 255)); p2.setBackground(new Color(204, 245, 255));
/Baidu Nhomakorabea*
* 重绘面板 */ p2.add(label1); p2.add(label2); p2.add(label3); p2.add(label4); label1.updateUI(); label2.updateUI(); label3.updateUI(); label4.updateUI(); p2.updateUI();
i = random(); j = random(); k = random(); r = random(); ImageRecognitor.numString = null;
label1 = new JLabel(new ImageIcon("../img/00" + i + ".png"), SwingConstants.CENTER); // 单张图片识别
new ImageRecognitor(k);
label4 = new JLabel("验证码", new ImageIcon("../img/00" + r + ".png"), SwingConstants.CENTER);
new ImageRecognitor(r); write = new JTextField(8); write.setOpaque(false);
btReco = new JButton("识别"); btCent = new JButton("确定"); btFresh = new JButton("刷新");
btReco.setContentAreaFilled(false); btCent.setContentAreaFilled(false); btFresh.setContentAreaFilled(false);
if (write.getText().equals(ImageRecognitor.numString)) { JOptionPane.showMessageDialog(null, "输入正确!", "辨识正确", JOptionPane.INFORMATION_MESSAGE);
} else JOptionPane.showMessageDialog(null, "验证码输入错误!", "辨识错误
}
/** * 得到二值化矩阵 */
public static int[][] getSymbolMatrix(int[] pixels, int sparseFactor) { final int width = ImageModel.width; final int height = ImageModel.height; int[][] ret = new int[width][height];
} }
package pkg;
/** * Created on 2014/4/25. */
import java.awt.Image; import java.awt.image.PixelGrabber; import java.util.logging.Level; import java.util.logging.Logger;
if (matrix[j][i] != 0) { if (i < 6) { // 纵向 if (j < 6) { // 横向 top_l++; } else { top_r++; } } else if (i < 12) { cet++; } else { if (j < 6) { bot_l++; } else { bot_r++; } }
for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (pixels[i * width + j] == -1) { ret[j][i] = 0; } else { ret[j][i] = 1; } }
} return ret; }
/** * 输出字符矩阵 */
public static void displayMatrix(int[][] matrix) { System.out.println(" "); int top_l = 0; int top_r = 0; int cet = 0; int bot_l = 0; int bot_r = 0; int k = 0;
public void init() { new ImageModel(); c = getContentPane(); p1 = new JPanel(new FlowLayout()); p2 = new JPanel(new FlowLayout(1, 0, 0)); p3 = new JPanel(new FlowLayout());
new ImageRecognitor(i);
label2 = new JLabel(new ImageIcon("../img/00" + j + ".png"), SwingConstants.CENTER);
new ImageRecognitor(j);
label3 = new JLabel(new ImageIcon("../img/00" + k + ".png"), SwingConstants.CENTER);
i = random(); j = random(); k = random(); r = random(); ImageRecognitor.numString = null;
label1 = new JLabel(new ImageIcon("../img/00" + i + ".png"), SwingConstants.CENTER); // 单张图片识别
double v_t_l = 0; double v_t_r = 0; double v_c = 0; double v_b_l = 0; double v_b_r = 0;
for (int i = 0; i < matrix[0].length; i++) { for (int j = 0; j < matrix.length; j++) { if (matrix[j][i] != 0) { System.out.print("*"); } else { System.out.print(" "); }
btReco.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { write.setText(ImageRecognitor.numString); }
});
btCent.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println(write.getText()); System.out.println(ImageRecognitor.numString);
public class ImageMatrix { static int k = 0; static double[] total = new double[10]; static double totalPic;
public static int[] getPixels(Image image, int width, int height) { // 得到像素大小 int[] pixels = new int[width * height]; try { new PixelGrabber(image, 0, 0, width, height, pixels, 0, width) .grabPixels(); } catch (InterruptedException ex) { Logger.getLogger(ImageMatrix.class.getName()).log(Level.SEVERE, null, ex); } return pixels;