saki4510t / UVCCamera

library and sample to access to UVC web camera on non-rooted Android device
2.96k stars 1.19k forks source link

Problem taking photos with multiple cameras #556

Open lgstud-io opened 4 years ago

lgstud-io commented 4 years ago

I have an issue to take pictures simultaneously with multiple cameras. Most of the cases (99% of time) only a single picture is taken

I have a class that wraps every connected camera to the device. The cameraNumber is a unique number of the camera (1 to n) I call this method for every single camera wrapper object

It does not matter if I call them all at once (in a loop), or all one by one waiting for the previous camera to finish and call onPhotoSaved in listener. Only a single photo is taken by camera 1. And the result of the .stillCapture callback returns the path to the image "..._1.jpg" for every other still capture call also.

So: I call takePicture for camera No1. It retuns me picPath "...._1.jpg" Then I call takePicture for camera No2. It retuns me picPath" ..._1.jpg" also. and the picture named "...._2.jpg" is never taken.

What might be the problem?

my photo taking code is the following:

void takePicture(String folderPath, String photoTime) { if (cameraHandler != null && cameraHandler.isOpened()) { String newImagePath = folderPath + "/" + photoTime+ "_" + cameraNumber + ".jpg"; cameraHandler.captureStill(newImagePath, picPath -> { listener.onPhotoSaved(new CapturedPhoto(cameraNumber, picPath)); } }); } else { listener.onPhotoSaved(new CapturedPhoto(cameraNumber, null)); } } }

quocnhat7 commented 4 years ago

Did you increase "cameraNumber" after capture picture?

quocnhat7 commented 4 years ago

I call takePicture for camera No1. It retuns me picPath "...._1.jpg" Then I call takePicture for camera No2. It retuns me picPath" ..._1.jpg" also. => did you check the picture content, are you sure picture #1 is different with picture #2? (if different, for sure it is because the second camera saved the picture #2 into the path _1)

lgstud-io commented 4 years ago

there are separate wrapper objects existing for each camera, so camera number is all right. I have downloaded the repository, and did a huge refactoring. (I also removed the video parts as I am not going to use it, and it made only complications with the refactoring.

The main problem is that your solution is not thread safe therefore the threads of the cameras access the same variable and therefore there is a problem I have mentioned.

The solution I have created is a nutshell is:


so basically that's it. not I can take pictures with as many cameras as I possibly can connect to the device, and more importantly, all at the same time.
heaveninfosoft commented 4 years ago

@lgstud-io can you send me code for multiple camera taking picture as well as recording video??