Reputation: 303
I have installed OpenCV-3.1.0 and in 3.0.0 or later versions, there's no HighGUI module in Java. That functionality is split in to two additional modules (videoio,imgcodecs).
I'm trying to capture a video from web cam using Java with OpenCv. Here's a class I found that does the Job. But since my version is not having HighGUI module, what is the way (or the code) that I can use to get the same functionality, instead of "Highgui.imencode()"?
package gui;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import javax.imageio.ImageIO;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.videoio.VideoCapture;
/**
* @author erandi
*/
public class WebCamTesting extends javax.swing.JFrame {
/**
* Creates new form WebCamTesting
*/
//definitions
private DaemonThread myThread = null;
int count = 0;
VideoCapture webSource = null;
Mat frame = new Mat();
MatOfByte mem = new MatOfByte();
//class of thread
class DaemonThread implements Runnable {
protected volatile boolean runnable = false;
@Override
public void run() {
synchronized (this) {
while (runnable) {
if (webSource.grab()) {
try {
webSource.retrieve(frame);
//Error - can't find the class Highgui
Highgui.imencode(".bmp", frame, mem);
Image im = ImageIO.read(new ByteArrayInputStream(mem.toArray()));
BufferedImage buff = (BufferedImage) im;
Graphics g = jPanelVideo.getGraphics();
if (g.drawImage(buff, 0, 0, getWidth(), getHeight() - 150, 0, 0, buff.getWidth(), buff.getHeight(), null))
if (runnable == false) {
System.out.println("Going to wait()");
this.wait();
}
} catch (Exception ex) {
System.out.println("Error");
}
}
}
}
}
}
public WebCamTesting() {
initComponents();
}
/**
* 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">
private void initComponents() {
jPanelVideo = new javax.swing.JPanel();
jButtonStart = new javax.swing.JButton();
jButtonPause = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout jPanelVideoLayout = new javax.swing.GroupLayout(jPanelVideo);
jPanelVideo.setLayout(jPanelVideoLayout);
jPanelVideoLayout.setHorizontalGroup(
jPanelVideoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 454, Short.MAX_VALUE)
);
jPanelVideoLayout.setVerticalGroup(
jPanelVideoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 335, Short.MAX_VALUE)
);
jButtonStart.setText("Start");
jButtonStart.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonStartActionPerformed(evt);
}
});
jButtonPause.setText("Pause");
jButtonPause.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonPauseActionPerformed(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()
.addGap(121, 121, 121)
.addComponent(jButtonStart)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButtonPause)
.addGap(117, 117, 117))
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addComponent(jPanelVideo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(40, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanelVideo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButtonPause)
.addComponent(jButtonStart))
.addGap(55, 55, 55))
);
pack();
}// </editor-fold>
private void jButtonStartActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
webSource = new VideoCapture(0); //video capture from default cam
myThread = new DaemonThread(); //create object from thread class
Thread t = new Thread(myThread);
t.setDaemon(true);
myThread.runnable = true;
t.start(); //start thread
jButtonStart.setEnabled(false); //deactivate start button
jButtonPause.setEnabled(true); // activate pause button
}
private void jButtonPauseActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
myThread.runnable = false; //stop thread
jButtonPause.setEnabled(false); //activate start
jButtonStart.setEnabled(true); //deactivate pause
webSource.release();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // load native library of opencv
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new WebCamTesting().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButtonPause;
private javax.swing.JButton jButtonStart;
private javax.swing.JPanel jPanelVideo;
// End of variables declaration
}
Upvotes: 1
Views: 1908