- Home /
PhotoCapture switch device multiple USB webcam
Hi, I am using windows and unity, and 2 USB webcams.
I want to take a series of photos with the two webcams ( for a stereo 3D modeling application)
and from what I can tell there are 2 ways of taking a photo with unity
1st using the Photocapture thing https://docs.unity3d.com/ScriptReference/Windows.WebCam.PhotoCapture.html
which works great, because it has these different functions for asynchronously capturing high quality photos and having functions that let you know when it has been written to memory. The problem is that I cannot specify which camera to use for the Photocapture. In the example it just chooses a random camera you have connected (probably device 0).
2nd using the WebcamTexture thing https://docs.unity3d.com/ScriptReference/WebCamTexture.html
Which lets you connect and stream from at least 2 webcams and you can specify which cameras each is by their Device ID and you can "capture" a photo just by grabbing pixels from the buffer e.g.
photoBTargetTexture.SetPixels(camBwebcam.GetPixels());
and then save those images to the disk via
byte[] bytes = theTex.EncodeToPNG();
string filePath = "Graycode/" + nameofimage + ".png";
File.WriteAllBytes(Application.absoluteURL + filePath, bytes);
(even though this way of saving photos seems to take MUCH longer than if using the photocapture, like 5 mins instead of 1 min)
but the problems with the WebcamTexture approach are that
1) The images don't seem that well synced between camera captures or to what is displayed on the screen when being told to capture a frame (i can use if(camAwebcam.didUpdateThisFrame ){...} but it still tends to lag behind the other code)
2) The resulting images somehow seem to take a really long time to save to the disk, and to open vs the PhotoCapture technique, and they seem to be a lower quality (despite all photos being captured at the same resolution).
So ideally, i would use the PhotoCapture, but try to set 2 photocaptures with different cameras, but that doesn't seem possible.
I also tried the plugin NatDevice, but that doesn't seem to be helpful either as you can only access advanced controls if using android or ios, not a PC.
Any thoughts?
Answer by Hikinghack · Aug 31, 2020 at 03:32 PM
I noticed that in new versions of unity there are some additions to the Webcam APIs such as Webcam mode.
https://docs.unity3d.com/2020.2/Documentation/ScriptReference/Windows.WebCam.WebCam.html
anybody know if this would somehow solve my problem? Or how to use this feature?