繁体   English   中英

如何使用JAVACV创建访问Webcam的Java Applet

[英]How to create a Java Applet accessing webcam using JAVACV

我已经使用JavaCV创建了Java应用程序,并且该应用程序已成功运行。 现在,我需要创建一个Applet来访问网络摄像头。 我知道访问Web Cam需要小程序签名。 我的问题是我尝试使用以下代码创建小程序。 但这是行不通的。 即,未创建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                   

}

请帮助我找到解决方案。 或建议我另一种创建Applet的方法。 我已经使用V4L4J库创建了applet。 但是它仅在Linux中有效。 我需要同时在Windows和Linux中工作

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

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                   

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM