1

我已经使用 JavaCV 创建了一个 Java 应用程序,它运行成功。现在我需要创建一个小程序来访问网络摄像头。我知道访问网络摄像头需要小程序签名。我的问题是我尝试使用以下代码创建小程序。但它不起作用。即 GUI 没有创建,IDE 中没有错误消息。

    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                   

}

请帮我找到解决方案。或者建议我另一种创建小程序的方法。我已经使用 V4L4J 库创建了小程序。但它只适用于 Linux。我需要在 Windows 和 Linux 中工作

4

1 回答 1

2

您必须以多线程方式从 Applet 的 start() 方法将缓冲图像添加到 JPanel 或任何其他 SWING 组件

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                   

}
于 2013-09-10T10:52:02.287 回答