2013-08-31 1 views
1

JavaCV를 사용하여 Java 응용 프로그램을 만들었으며 성공적으로 실행 중입니다. 이제 웹캠에 액세스하려면 애플릿을 만들어야합니다. 애플릿 서명이 웹 캠에 액세스하는 데 필요하다는 것을 알고 있습니다. 내 문제는 다음 코드로 애플릿을 만들려고했다는 것입니다. 하지만 작동하지 않습니다. 즉 GUI가 생성되지 않고 IDE에 오류 메시지가 없습니다.JAVACV를 사용하여 웹캠에 액세스하는 Java 애플릿을 만드는 방법

import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_8U; 
import static com.googlecode.javacv.cpp.opencv_core.cvAbsDiff; 
import static com.googlecode.javacv.cpp.opencv_core.cvFlip; 
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_CHAIN_APPROX_SIMPLE; 
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_GAUSSIAN; 
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_RETR_LIST; 
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_RGB2GRAY; 
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_THRESH_BINARY; 
import static com.googlecode.javacv.cpp.opencv_imgproc.cvCvtColor; 
import static com.googlecode.javacv.cpp.opencv_imgproc.cvFindContours; 
import static com.googlecode.javacv.cpp.opencv_imgproc.cvSmooth; 
import static com.googlecode.javacv.cpp.opencv_imgproc.cvThreshold; 

import java.awt.Color; 
import java.awt.event.WindowEvent; 
import java.io.DataOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.lang.reflect.InvocationTargetException; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.SQLException; 
import java.util.ArrayList; 
import java.util.List; 

import javax.imageio.ImageIO; 


import com.googlecode.javacpp.Loader; 
import com.googlecode.javacv.FrameGrabber; 
import com.googlecode.javacv.OpenCVFrameGrabber; 
import com.googlecode.javacv.FrameGrabber.Exception; 
import com.googlecode.javacv.cpp.opencv_core.CvContour; 
import com.googlecode.javacv.cpp.opencv_core.CvMemStorage; 
import com.googlecode.javacv.cpp.opencv_core.CvSeq; 
import com.googlecode.javacv.cpp.opencv_core.IplImage; 


/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/** 
* 
* @author samp 
*/ 
public class VismanWeb extends javax.swing.JApplet 
{ 

    /** 
    * Initializes the applet NewJApplet 
    */ 

    private String server=""; 
    private String secur=""; 
    private String ipaddr=""; 
    IplImage frame; 

    private boolean captFlag=false; 
    public void start() 
    { 


     FrameGrabber grabber = new OpenCVFrameGrabber(""); 
     int height=240,width=320; 
     grabber.setImageHeight(height); 
     grabber.setImageWidth(width); 

     try {  

      //Start grabber to capture video 
      grabber.start();  

      //Declare img as IplImage 
      IplImage img; 

      while (true) { 

      //inser grabed video fram to IplImage img 
      img = grabber.grab(); 



      if (img != null) {  
      //Flip image horizontally 
      cvFlip(img, img, 1); 
      //Show video frame in canvas 
      capturePnl.getGraphics().drawImage(img.getBufferedImage(), 0, 0, 320, 240, null); 
      } 
      } 
      } 
     catch (Exception e) {  
     } 


    } 
    @Override 
    public void init() { 
     /* 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(VismanWeb.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(VismanWeb.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(VismanWeb.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(VismanWeb.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     try { 
      java.awt.EventQueue.invokeAndWait(new Runnable() { 
       public void run() { 
        initComponents(); 
       } 
      }); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (InvocationTargetException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * This method is called from within the init() method 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">       
    private void initComponents() { 

     capturePnl = new javax.swing.JPanel(); 
     imagePnl = new javax.swing.JPanel(); 
     captureBtn = new javax.swing.JButton(); 
     getContentPane().setBackground(Color.WHITE); 
     setMaximumSize(new java.awt.Dimension(360, 560)); 
     setMinimumSize(new java.awt.Dimension(360, 560)); 
     setPreferredSize(new java.awt.Dimension(360, 560)); 

     capturePnl.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); 
     capturePnl.setMaximumSize(new java.awt.Dimension(320, 240)); 
     capturePnl.setMinimumSize(new java.awt.Dimension(320, 240)); 
     capturePnl.setPreferredSize(new java.awt.Dimension(320, 240)); 

     javax.swing.GroupLayout capturePnlLayout = new javax.swing.GroupLayout(capturePnl); 
     capturePnl.setLayout(capturePnlLayout); 
     capturePnlLayout.setHorizontalGroup(
      capturePnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 318, Short.MAX_VALUE) 
     ); 
     capturePnlLayout.setVerticalGroup(
      capturePnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 238, Short.MAX_VALUE) 
     ); 

     imagePnl.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(38, 21, 248))); 
     imagePnl.setMaximumSize(new java.awt.Dimension(320, 240)); 
     imagePnl.setMinimumSize(new java.awt.Dimension(320, 240)); 

     javax.swing.GroupLayout imagePnlLayout = new javax.swing.GroupLayout(imagePnl); 
     imagePnl.setLayout(imagePnlLayout); 
     imagePnlLayout.setHorizontalGroup(
      imagePnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 320, Short.MAX_VALUE) 
     ); 
     imagePnlLayout.setVerticalGroup(
      imagePnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 240, Short.MAX_VALUE) 
     ); 

     captureBtn.setText("Capture"); 
     captureBtn.setMaximumSize(new java.awt.Dimension(320, 30)); 
     captureBtn.setMinimumSize(new java.awt.Dimension(320, 30)); 
     captureBtn.setPreferredSize(new java.awt.Dimension(320, 30)); 
     captureBtn.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       imagePnl.getGraphics().drawImage(frame.getBufferedImage(), 0, 0,imagePnl.getWidth(), imagePnl.getHeight(), null); 
      } 
     }); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(20, 20, 20) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addComponent(imagePnl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
         .addComponent(captureBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 320, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addComponent(capturePnl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) 
       .addContainerGap(20, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(capturePnl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addComponent(captureBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addComponent(imagePnl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap()) 
     ); 
    }//       



    // Variables declaration - do not modify      
    private javax.swing.JButton captureBtn; 
    private javax.swing.JPanel capturePnl; 
    private javax.swing.JPanel imagePnl; 
    // End of variables declaration     

} 

해결 방법을 찾도록 도와주십시오. 또는 Applet을 만드는 또 다른 방법을 제안하십시오. 나는 V4L4J 라이브러리를 사용하여 애플릿을 만들었습니다. 그러나 그것은 리눅스에서만 작동합니다. 나는 JPanel의이 다중 스레드 방식으로 애플릿의 start() 메서드에서 다른 스윙 구성 요소 중독에 당신은 버퍼 이미지를 추가 할 필요가 윈도우와 리눅스

답변

2

모두를 작업해야

public class VismanWeb extends javax.swing.JApplet 
{ 

    /** 
    * Initializes the applet NewJApplet 
    */ 

    private String server=""; 
    private String secur=""; 
    private String ipaddr=""; 
    IplImage frame; 
    OpenCVFrameGrabber grabber; 


    private boolean captFlag=false; 
    public void start() 
    { 

      Runnable run=new Runnable(){ 

       @Override 
       public void run() 
       { 
        // TODO Auto-generated method stub 
        grabber = new OpenCVFrameGrabber(0); 
        try { 
         grabber.start(); 
        } catch (Exception e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        //IplImage grabbedImage = null; 
        try { 
         frame = grabber.grab(); 
        } catch (Exception e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 

        //CanvasFrame canvasFrame = new CanvasFrame("Cam"); 
        // canvasFrame.setCanvasSize(grabbedImage.width(), grabbedImage.height()); 
        grabber.setFrameRate(10); 
        System.out.println("fraamerate = " + grabber.getFrameRate()); 
         try { 
         while (capturePnl.isVisible() && (frame = grabber.grab()) != null) { 
          capturePnl.getGraphics().drawImage(frame.getBufferedImage(), 0, 0, 320, 240, null); 
          //System.out.print("inside\n"); 
         } 
        } catch (Exception e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 

         try { 
         grabber.stop(); 
        } catch (Exception e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 

      }; 
      Thread capture=new Thread(run); 
      capture.start(); 



    } 






    @Override 
    public void init() { 
     /* 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(VismanWeb.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(VismanWeb.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(VismanWeb.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(VismanWeb.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the applet */ 
     try { 
      java.awt.EventQueue.invokeAndWait(new Runnable() { 
       public void run() { 
        initComponents(); 
       } 
      }); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (InvocationTargetException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * This method is called from within the init() method 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">       
    private void initComponents() { 

     capturePnl = new javax.swing.JPanel(); 
     imagePnl = new javax.swing.JPanel(); 
     captureBtn = new javax.swing.JButton(); 
     getContentPane().setBackground(Color.WHITE); 
     setMaximumSize(new java.awt.Dimension(360, 560)); 
     setMinimumSize(new java.awt.Dimension(360, 560)); 
     setPreferredSize(new java.awt.Dimension(360, 560)); 

     capturePnl.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); 
     capturePnl.setMaximumSize(new java.awt.Dimension(320, 240)); 
     capturePnl.setMinimumSize(new java.awt.Dimension(320, 240)); 
     capturePnl.setPreferredSize(new java.awt.Dimension(320, 240)); 

     javax.swing.GroupLayout capturePnlLayout = new javax.swing.GroupLayout(capturePnl); 
     capturePnl.setLayout(capturePnlLayout); 
     capturePnlLayout.setHorizontalGroup(
      capturePnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 318, Short.MAX_VALUE) 
     ); 
     capturePnlLayout.setVerticalGroup(
      capturePnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 238, Short.MAX_VALUE) 
     ); 

     imagePnl.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(38, 21, 248))); 
     imagePnl.setMaximumSize(new java.awt.Dimension(320, 240)); 
     imagePnl.setMinimumSize(new java.awt.Dimension(320, 240)); 

     javax.swing.GroupLayout imagePnlLayout = new javax.swing.GroupLayout(imagePnl); 
     imagePnl.setLayout(imagePnlLayout); 
     imagePnlLayout.setHorizontalGroup(
      imagePnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 320, Short.MAX_VALUE) 
     ); 
     imagePnlLayout.setVerticalGroup(
      imagePnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 240, Short.MAX_VALUE) 
     ); 

     captureBtn.setText("Capture"); 
     captureBtn.setMaximumSize(new java.awt.Dimension(320, 30)); 
     captureBtn.setMinimumSize(new java.awt.Dimension(320, 30)); 
     captureBtn.setPreferredSize(new java.awt.Dimension(320, 30)); 
     captureBtn.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       imagePnl.getGraphics().drawImage(frame.getBufferedImage(), 0, 0,imagePnl.getWidth(), imagePnl.getHeight(), null); 
      } 
     }); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(20, 20, 20) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addComponent(imagePnl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
         .addComponent(captureBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 320, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addComponent(capturePnl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) 
       .addContainerGap(20, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(capturePnl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addComponent(captureBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addComponent(imagePnl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap()) 
     ); 
     // capt(); 
    }//       




//----------------------------------------------------------------- 



//----------------------------------------------------------------- 




    // Variables declaration - do not modify      
    private javax.swing.JButton captureBtn; 
    private javax.swing.JPanel capturePnl; 
    private javax.swing.JPanel imagePnl; 
    // End of variables declaration     

} 
관련 문제