0

I am in the process of creating a program that will show a webcam on a JPanel and while the webcam is running, draw a rectangle over a part of it then store that in a Buffered Image.

Ideally my plan would be to use a mouse to draw the rectangle over it then store that part straight away once the rectangle has been drawn.

Is there a simple way of achieving this and if so how can I go about this?

Following is the code to display the webcam.

WebcamPanel panel = new WebcamPanel(webcam);
    panel.setFPSDisplayed(true);
    panel.setDisplayDebugInfo(true);
            panel.setImageSizeDisplayed(true);
    panel.setMirrored(true);
            panel.setFocusable(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);
Fdoh
  • 1
  • 3
  • You can only achieve this by writing a program. What did you try to write? Where did you get stuck? – RealSkeptic Mar 20 '17 at 19:18
  • I have the JPanel showing the webcam feed and now in the process of finding ways to draw a rectangle to the webcam feed. – Fdoh Mar 20 '17 at 19:20
  • The question would come down to, how are you displaying the webcam feed? If you're displaying it using something like a `java.awt.Canvas` or other heavy weight component, then the user is probably no, although as I understand it, VLCj has some functionality that might be able to to do it. If you're displaying the output through some kind of `JComponent` or lightweight component, then the user is a maybe, but as it stands, there are too many caveats to provide a single viable answer – MadProgrammer Mar 20 '17 at 19:35
  • @MadProgrammer I have updated the post with the code I am using for the webcam on a jpanel. – Fdoh Mar 20 '17 at 19:41
  • @Fdoh What is `WebcamPanel`? It's not part of the standard API, is it from a library or something you have written? – MadProgrammer Mar 20 '17 at 19:42
  • @MadProgrammer That is taking the webcam object and creating a panel of it, once created it is then added to the JFrame. I am making use of the https://github.com/sarxos/webcam-capture api – Fdoh Mar 20 '17 at 19:44
  • @Fdoh Okay, so `WebPanel` is a `JPanel`, which is essentially painting a `BufferedImage` (frame). You "could" override its `paintComponent` and paint your own overlay directly (after calling `super.paintComponent`) to provide the selection overlay, this is probably the best solution as it will allow you to "save" the resulting image more easily – MadProgrammer Mar 20 '17 at 19:53
  • @Fdoh Mouse selection is not difficult, it just takes a little forethought, [for example](http://stackoverflow.com/questions/32981758/easier-way-to-make-a-paint-application-in-java/32981915#32981915) – MadProgrammer Mar 20 '17 at 19:54
  • That is a brilliant example there, and thank you for the help. I should be able to solve it from here onwards. Not sure how to give you the correct answer for the problem in the comments part, as I am a bit new to Stack Overflow. If you can aid this I will give you that, this is exactly the type of thing I needed to solve this. Thanks again. – Fdoh Mar 20 '17 at 20:11

0 Answers0