How to redefine the Capture object?

edited May 2017 in Library Questions

Good day.

I want to make it possible to reconnect the camera which is bound to the Capture object during the program works. So, it's just a button on the screen and when you click it the camera should be changed. Actually, now there is no matter if it connects to a new or the previous one.

That's the truncated part of the code:

import processing.video.Capture;

Capture wirelessCam;

void setup() {
  fullScreen();
  smooth();
  frameRate(60);

  connect();
}

void draw() {
  background(#000000);
  if (wirelessCam.available()) wirelessCam.read();
  image(wirelessCam, 0, 0);
}

void connect() {
  wirelessCam = new Capture(this, Capture.list()[0]);
  wirelessCam.start();
}

void mouseClicked() {
  connect();
}

Seems to be an easy block of code working correctly, but, in fact, it isn't. The first time when the program is launched it really shows the webcam video stream. But as only you click the mouse the whole program breaks down - instead of the expected video image you see a lonely useless rectangle the same color as the background.

I've done my best to solve the proplem, combining the functions sequences and adding/removing some parts of code. Unfortunately, I managed to do nothing. Literally, NOTHING.

I will be very grateful to your ideas and any kind of help. Thanks.

Answers

  • I have tried this before and when I tried to re-define my capture object, I could get even the blue death screen that nobody wants to ever see.

    How many capture devices do you have? Or are you just switching resolution settings? One possible idea is to have an array of capture devices and stop and play accordingly.

    Kf

  • There are 22 of them and they're all the same webcam device.

    But, you see, that's not the sense. It doesn't change its behaviour while trying to re-connect a different device. You may even add a function like connect(), but make it bing the wirelessCam to a Capture object with another characteristics, thoung it still won't work.

    Speaking about creating an array of Capture objects - yeah, I had such an idea. The only thing is that this will occupy to much memory, and I'm not sure such an array allows the program work as fast as it is currently.

  • Answer ✓

    If you are using the same device, I would suggest instantiating one object at high resolution and then display at lower resolution. This idea might not not suit your needs as I don't know all the details of your application. Can you ellaborate why you want to do this by the way?

    Not sure if it is a good idea to have an array of objects manipulating the same device but at different resolutions. You could manipulate multiple cameras. You can instantiate a capture device to each of them and then only process the one you have the focus on.

    Kf

  • edited May 2017

    Oh god... I've finally found the problem. Still cannot believe that the solution is so easy and, moreover, so evident. Just have a look at this and nothing more should be explained:

    ...
    void mouseClicked() {
      wirelessCam.stop();
      connect();
    }
    ...
    

    Kfrajer, thank you for you ideas.

    By the way, the principal reason to have an ability to change the camera connected to a Capture object is that the program should connect to a certain camera with known name and resolution, though it isn't known if it's connected to PC (the camera supposed to be a video stream from video capture card). If it is not at the moment, the program chooses a default one, which stands for the number 0 in Capture.list().

    P.S. I tried to make an array of Capture objects, but it will give the same result if not to use the cameraName.stop() function. Now I'm sure it has some special sense.

  • @daniil

    Great you found a solution. Thxs for sharing your answer.

    Kf

  • edited May 2017

    If you're instantiating a new Capture after stop(), you're probably generating memory leakage! :-SS

    In order to make sure all resources are freed up, use dispose() instead. L-)

Sign In or Register to comment.