Please help. I want to make a frame with menu and a panel to display webcam's image.
I use EclipseIDE and Java and OpenCV.
I use EclipseIDE and Java and OpenCV.
here's some basic code to get you started, for anything else, please refer to java docs / tutorials (e.g., adding a menu is quite offtopic here..)
import org.opencv.core.*;
import org.opencv.videoio.*;
import java.awt.*;
import java.awt.image.*;
import java.lang.Thread.*;
import javax.swing.JFrame;
// helper class to override Panel's paint():
class ShowImage extends Panel {
BufferedImage image;
public BufferedImage mat2Buffer(Mat m) {
int type = BufferedImage.TYPE_BYTE_GRAY;
if (m.channels() > 1) {
type = BufferedImage.TYPE_3BYTE_BGR;
}
BufferedImage image = new BufferedImage(m.cols(), m.rows(), type);
// copy pixels from Mat to BufferedImage:
int bufferSize = m.channels() * m.cols() * m.rows();
byte [] b = new byte[bufferSize];
m.get(0,0,b);
final byte[] pixels = ((DataBufferByte)image.getRaster().getDataBuffer()).getData();
System.arraycopy(b, 0, pixels, 0, b.length);
return image;
}
public void show(Mat m) {
image = mat2Buffer(m);
repaint();
}
public void paint(Graphics g) {
g.drawImage(image, 0, 0, null);
}
}
public class SimpleSample {
static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }
public static void main(String[] args) {
JFrame frame = new JFrame("Display image");
frame.setSize(640, 480);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ShowImage imshow = new ShowImage();
frame.getContentPane().add(imshow);
VideoCapture cap = new VideoCapture(0);
while (cap.isOpened()) {
Mat im = new Mat();
if (! cap.read(im))
break;
imshow.show(im);
try { Thread.sleep(20); }
catch(Exception x) {}
}
}
}
Thank you for your kind reply and an attached "Example". It works, but I have put a menu "Camera" with a sub-menu "Start" in my mainframe window. Therefore, when I click "Start" the camera's video image should be displayed. In this case I have created a class MenuItemListener and a method "actionPerformed", in which I want to start the video from there. My problem is that I coud not make the video to started from within the method "actionPerformed" of the class MenuItemListener. I would appreciate very much if you could help.
Asked: 2016-04-05 01:04:26 -0600
Seen: 214 times
Last updated: Apr 05 '16
Unresolved inclusion in OpenCV+Android tutorial
How to convert Floating point image to 32-bit single-channel?
How to translate this to Java?
android: how to put a column into Mat
OpenCV Tutorial 1 - Add OpenCV on API 8
Running the OpenCV4Android application on my PC
Using OpenCV as a stress detector