1 | initial version |
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.imgcodecs.Imgcodecs;
import org.opencv.videoio.*;
import org.opencv.imgproc.*;
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 Mat2BufferedImage(Mat m) {
int type = BufferedImage.TYPE_BYTE_GRAY;
if (m.channels() > 1) {
type = BufferedImage.TYPE_3BYTE_BGR;
}
// copy pixels from Mat to BufferedImage:
int bufferSize = m.channels()*m.cols()*m.rows();
byte [] b = new byte[bufferSize];
m.get(0,0,b);
BufferedImage image = new BufferedImage(m.cols(),m.rows(), type);
final byte[] targetPixels = ((DataBufferByte)image.getRaster().getDataBuffer()).getData();
System.arraycopy(b, 0, targetPixels, 0, b.length);
return image;
}
public void show(Mat m) {
image = Mat2BufferedImage(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) {}
}
}
}
2 | No.2 Revision |
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.imgcodecs.Imgcodecs;
import org.opencv.videoio.*;
import org.opencv.imgproc.*;
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 Mat2BufferedImage(Mat 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();
m.channels() * m.cols() * m.rows();
byte [] b = new byte[bufferSize];
m.get(0,0,b);
BufferedImage image = new BufferedImage(m.cols(),m.rows(), type);
final byte[] targetPixels pixels = ((DataBufferByte)image.getRaster().getDataBuffer()).getData();
System.arraycopy(b, 0, targetPixels, pixels, 0, b.length);
return image;
}
public void show(Mat m) {
image = Mat2BufferedImage(m);
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) {}
}
}
}
3 | No.3 Revision |
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.imgcodecs.Imgcodecs;
import org.opencv.videoio.*;
import org.opencv.imgproc.*;
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) {}
}
}
}