【发布时间】:2016-09-18 01:28:22
【问题描述】:
我已经安装了 OpenCV-3.1.0,在 3.0.0 或更高版本中,Java 中没有 HighGUI 模块。该功能分为两个附加模块(videoio、imgcodecs)。
我正在尝试使用 Java 和 OpenCv 从网络摄像头捕获视频。这是我发现的一个可以完成这项工作的课程。但是由于我的版本没有 HighGUI 模块,我可以使用什么方式(或代码)来获得相同的功能,而不是“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
}
【问题讨论】:
-
我知道 HighGUI 在 3.0.0 或更高版本中缺失,现在它的功能分为两个附加模块。但我需要确切的方法来获得“Highgui.imencode()”的功能。
-
Imgcodecs.imencode()
-
非常感谢。我会继续