Reputation: 305
I have created a Java Application using JavaCV and it is running successfully. Now I need to create an Applet to access the Web Cam. I Know taht the applet signing is needed to access the Web Cam. My problem is that I have tried to create the applet with the following code. But it is not working. ie the GUI is not creating and no error message is there in the 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
}
please help me to find a solution. Or suggest me another way to create the Applet. I 've created applet using V4L4J library. But it work only in Linux. I need to work both in Windows and Linux
Upvotes: 1
Views: 2170
Reputation: 305
You have to add the buffered image to the JPanel od any other SWING Component from the start() method of the Applet in a Multi-threaded manner
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
}
Upvotes: 2