Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package houghtransform; import java.awt.Toolkit; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.TimerTask; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.ImageIcon; import java.util.Timer; import org.opencv.core.Core; import org.opencv.videoio.VideoCapture; import static org.opencv.videoio.Videoio.*; /** * * @author Maksim */ public class CaptureJFrame extends javax.swing.JFrame { int choiceT; final int thrN = 4; Capture[] thread = new Capture[thrN]; int fps = 0; VideoCapture webCam; Timer frameRate = new Timer(); TimerTask task = new TimerTask() { @Override public void run() { jTextField1.setText(String.valueOf(fps)); fps = 0; } }; /** * Creates new form CaptureJFrame * @param choiceT * @throws java.io.IOException */ public CaptureJFrame(int choiceT) throws IOException { initComponents(); this.choiceT = choiceT; this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent we) { for (int i = 0; i < thrN; i++) { thread[i].stop(); } for (int i = 0; i < thrN; i++) { try { thread[i].t.join(); } catch (InterruptedException ex) { System.out.println("Parent thread is die!!!"); } } frameRate.cancel(); task.cancel(); webCam.release(); dispose(); } }); System.loadLibrary(Core.NATIVE_LIBRARY_NAME); webCam = new VideoCapture(); webCam.open(0); if (webCam.isOpened() == false) throw new IOException("No access to camera!!!"); int width = (int) webCam.get(CV_CAP_PROP_FRAME_WIDTH); int height = (int) webCam.get(CV_CAP_PROP_FRAME_HEIGHT); this.setSize(width * 2 + 25, height + 80); for (int i = 0; i < thrN; i++) { thread[i] = new Capture(this, webCam, i, choiceT); thread[i].start(); } frameRate.scheduleAtFixedRate(task, 1000, 1000); } public synchronized void showVideo(BufferedImage cannyImage, BufferedImage houghImage) { try { wait(100); } catch (InterruptedException ex) { Logger.getLogger(CaptureJFrame.class.getName()).log(Level.SEVERE, null, ex); } jLabel2.setIcon(new ImageIcon(cannyImage)); jLabel1.setIcon(new ImageIcon(houghImage)); fps++; } public void close() { WindowEvent winClosingEvent = new WindowEvent(this, WindowEvent.WINDOW_CLOSING); Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosingEvent); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); jTextField1.setEditable(false); jTextField1.setText("0"); jButton1.setText("jButton1"); jButton1.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent evt) { jButton1MousePressed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton1).addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE) .addGap(72, 72, 72))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton1)) .addGap(257, 257, 257))); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MousePressed close(); }//GEN-LAST:event_jButton1MousePressed /** * @param i */ public static void main(final int i) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(CaptureJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(CaptureJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(CaptureJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(CaptureJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { try { new CaptureJFrame(i).setVisible(true); } catch (IOException ex) { System.out.println("No access to camera!!!"); } } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JTextField jTextField1; // End of variables declaration//GEN-END:variables }