Ask Your Question
0

Please help. I want to make a frame with menu and a panel to display webcam's image.

asked 2016-04-05 01:04:26 -0600

updated 2016-04-05 01:28:05 -0600

berak gravatar image

I use EclipseIDE and Java and OpenCV.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-04-05 02:34:35 -0600

berak gravatar image

updated 2016-04-05 02:45:08 -0600

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) {}
        }
    }
}
edit flag offensive delete link more

Comments

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.

PaiWann gravatar imagePaiWann ( 2016-04-05 04:12:13 -0600 )edit
2

again, all of this is beyond the scope of this site, you're pretty much on your own, now.

berak gravatar imageberak ( 2016-04-05 04:34:46 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-04-05 01:04:26 -0600

Seen: 214 times

Last updated: Apr 05 '16