Advertisement
Guest User

Untitled

a guest
Mar 25th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. import static cern.jet.math.Bessel.i1;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import java.io.*;
  6. import facedetection.*;
  7. import com.googlecode.javacv.*;
  8. import com.googlecode.javacv.cpp.*;
  9. import com.googlecode.javacpp.Loader;
  10. import com.mysql.jdbc.Connection;
  11. import java.sql.Blob;
  12. import java.sql.DriverManager;
  13. import java.sql.PreparedStatement;
  14. import java.sql.ResultSet;
  15. import java.sql.SQLException;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18.  
  19.  
  20. public class FaceRecognizer extends JFrame
  21. {
  22. // GUI components
  23. private FaceRecogPanel facePanel;
  24. private JButton recogBut;
  25. private JTextField nameField; // where the name (and distance info) appears
  26. private JButton done;
  27. private JButton exit;
  28.  
  29.  
  30. public FaceRecognizer()
  31. {
  32. super("Face Recognizer");
  33. FaceRecognizer fac;
  34. Container c = getContentPane();
  35. c.setLayout( new BorderLayout() );
  36.  
  37. // Preload the opencv_objdetect module to work around a known bug.
  38. Loader.load(opencv_objdetect.class);
  39.  
  40. facePanel = new FaceRecogPanel(this); // the sequence of pictures appear here
  41. c.add( facePanel, BorderLayout.CENTER);
  42.  
  43.  
  44. // button for recognizing a highlighted face
  45. done = new JButton("Done");
  46. recogBut = new JButton("Recognize Face");
  47. recogBut.addActionListener( new ActionListener() {
  48. public void actionPerformed(ActionEvent e)
  49. { nameField.setText("");
  50. recogBut.setEnabled(false);
  51. facePanel.setRecog();
  52. }
  53. });
  54. done.addActionListener(new ActionListener() {
  55. public void actionPerformed(ActionEvent e)
  56. {
  57. WelcomeStaffs wc = new WelcomeStaffs();
  58. Connection con = null;
  59. try{
  60. String url = "jdbc:mysql://localhost:3306/facedetection";
  61. Class.forName("com.mysql.jdbc.Driver");
  62. System.out.println("Conncection Success");
  63. con = (Connection) DriverManager.getConnection(url, "root", "2204");
  64. System.out.println("Database Connected");
  65. }
  66. catch(Exception ex)
  67. {
  68. System.out.println("Exception = "+ex);
  69. }
  70. String nm = nameField.getText().toUpperCase();
  71. String name ="",course="";
  72. System.out.println("Hello im till here!!");
  73. String subject = wc.subject_txt.getText();
  74. String staff = wc.staff_txt.getText();
  75. String day = wc.day_txt.getText();
  76. String date = wc.date_txt.getText();
  77. String time = wc.time_txt.getText();
  78. int roll_no = 0;
  79. System.out.println(subject+staff);
  80. String sql = "SELECT roll_no,name,course,photo FROM students WHERE name=?";
  81. try {
  82.  
  83. PreparedStatement ps = con.prepareStatement(sql);
  84. ps.setString(1,nm);
  85. ResultSet rs = ps.executeQuery();
  86. while(rs.next()) //retrieving values from database and storing it!
  87. {
  88. roll_no = rs.getInt("roll_no");
  89. name = rs.getString("name");
  90. course = rs.getString("course");
  91. byte[]imagedata = rs.getBytes("photo");
  92. image = Toolkit.getDefaultToolkit().createImage(imagedata);
  93. format = new ImageIcon(image);
  94. System.out.println("Retrieved Data!!!!!!!");
  95. }
  96. } catch (SQLException ex) {
  97. Logger.getLogger(FaceRecognizer.class.getName()).log(Level.SEVERE, null, ex);
  98. }
  99. try{
  100. if(roll_no == 0 || name.equals("") || course.equals(""))
  101. {
  102. System.out.println("EMPTY");
  103. }
  104. else
  105. {
  106. sql = "INSERT INTO attendance(roll_no, name, course, subject, staff, day, time, date, photo) VALUES(?,?,?,?,?,?,?,?,?)";
  107. PreparedStatement ps1 = con.prepareStatement(sql);
  108. ps1.setInt(1,roll_no);
  109. ps1.setString(2,name);
  110. ps1.setString(3,course);
  111. ps1.setString(4,subject);
  112. ps1.setString(5,staff);
  113. ps1.setString(6,day);
  114. ps1.setString(7,time);
  115. ps1.setString(8,date);
  116. ps1.setBlob(9, (Blob) format);
  117. int i1 = ps1.executeUpdate();
  118. JOptionPane.showMessageDialog(null,i1+" Data Inserted");
  119. }
  120.  
  121. }
  122. catch(Exception ec)
  123. {
  124. System.out.println(""+ec);
  125. }
  126.  
  127. }
  128. });
  129.  
  130. nameField = new JTextField(20); // for the name of the recognized face
  131. nameField.setEditable(false);
  132.  
  133. JPanel p = new JPanel();
  134. p.add(recogBut);
  135. p.add( new JLabel("Name: "));
  136. p.add( nameField);
  137. p.add(done);
  138. c.add(p, BorderLayout.SOUTH);
  139.  
  140.  
  141. addWindowListener( new WindowAdapter() {
  142. public void windowClosing(WindowEvent e)
  143. { facePanel.closeDown(); // stop snapping pics
  144. /*Attendance_Chart ac = new Attendance_Chart();
  145. ac.setVisible(true);*/
  146. System.exit(0);
  147. }
  148. });
  149.  
  150. pack();
  151. setResizable(false);
  152. setVisible(true);
  153. } // end of FaceRecognizer()
  154.  
  155.  
  156.  
  157. public void setRecogName(final String faceName, final String dist)
  158. // update face name and its distance in the nameField; called from panel
  159. {
  160. SwingUtilities.invokeLater(new Runnable() {
  161. public void run()
  162. { nameField.setText( faceName);
  163. recogBut.setEnabled(true);
  164. }
  165. });
  166. } // end of setRecogName()
  167.  
  168.  
  169. // -------------------------------------------------------
  170.  
  171. public static void main( String args[] )
  172. {
  173. new FaceRecognizer();
  174.  
  175. }
  176.  
  177. public ImageIcon format = null;
  178. public Image image = null;
  179. public Blob blob = null;
  180. }
  181.  
  182. // end of FaceRecognizer class
  183.  
  184. Conncection Success for Staffs
  185. detection/recognition duration: 9ms
  186. Database Connected for Staffs
  187. Table Created
  188. Conncection Success
  189. Database Connected
  190. Hello im till here!!
  191.  
  192. Retrieved Data!!!!!!!
  193. java.lang.ClassCastException: javax.swing.ImageIcon cannot be cast to `java.sql.Blob`
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement