简体   繁体   中英

Adding Webcam panel to existing jPanel

I'm using NetBeans and I made basic layout with jPanel in the middle of it. Now I would like to add Webcam panel in this jPanel.

Here's example how to get frames from webcam and display it in new window. It's working fine.

    Webcam webcam = Webcam.getDefault();
    webcam.setViewSize(WebcamResolution.VGA.getSize());

    WebcamPanel panel = new WebcamPanel(webcam);
    panel.setFPSDisplayed(true);
    panel.setDisplayDebugInfo(true);
    panel.setImageSizeDisplayed(true);
    panel.setMirrored(true);

    JFrame window = new JFrame("Test webcam panel");
    window.add(panel);
    window.setResizable(true);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.pack();
    window.setVisible(true);

But when I tried to display it in my existing jPanel it's not working. Webcam screen is not visible. Here's my code:

    Webcam webcam = Webcam.getDefault();
    webcam.setViewSize(WebcamResolution.VGA.getSize());

    WebcamPanel panel = new WebcamPanel(webcam);
    panel.setFPSDisplayed(true);
    panel.setDisplayDebugInfo(true);
    panel.setImageSizeDisplayed(true);
    panel.setMirrored(true);

    jPanel5.add(panel);
    jPanel5.setVisible(true);

I call all of this in my main class constructor. Just after other netbeans componenst are loaded. When I add example it's working good but then my main layout is loaded and webcam screen in another window. I would like to get it in the same window.

I have seen other topics about adding image to jPanel but it's not working with capturing movie from webcam.

Thanks for help.

But when I tried to display it in my existing jPanel it's not working.

When you add components to a visible GUI the basic code is:

panel.add(...);
panel.revalidate(); // invoke the layout manager
panel.repaint(); // paint components

All component have a default size of (0, 0) when created so there is nothing to paint. You need to invoke the layout manager so the component is given a size/location.

I changed a little my application and now in the middle of main windows is JTabbedPane and I found a solution to my problem. Instead of making Tabs using NetBeans window designer I made it with code.

I made empty JTabbedPane in Netbeans and then add this to code:

final JPanel jPanelCamera = new JPanel();

jTabbedPane1.addTab("Camera", jPanelCamera);

Webcam webcam = Webcam.getDefault();
webcam.setViewSize(WebcamResolution.VGA.getSize());

WebcamPanel webcamPanel = new WebcamPanel(webcam);
webcamPanel.setFPSDisplayed(true);
webcamPanel.setDisplayDebugInfo(true);
webcamPanel.setImageSizeDisplayed(true);
webcamPanel.setMirrored(true);

jPanelCamera.add(webcamPanel);
jPanelCamera.getParent().revalidate();

System.out.println("Camera OK");

I have no idea why earlier when I made component using NetBeans designer it was't working but now it's working good. I think if someone would add this not to JTabbedPane, but to JPanel should also make this panel with code. Not with Netbeans designer and then it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM