How can i check if there is an active connection to webcam ?

3 views (last 30 days)
I'm making a GUI with a webcam and i'm taking a snapshot every time i click a push button, but first i have to create the webcam object in order to take the photo, so i'm wondering if there is a way to check if the GUI already make connection with the webcam and skip the creation of this object because it takes a lot of time in the creation. This is the code im trying but seems not working on the GUI:
exst = exist(vid)
if exst==0
clear('vid');
maxAttempts = 30;
attempt = 1;
hasProperty = false;
while attempt < maxAttempts
vid = webcam('USB Camera');
hasProperty = max(ismember(properties(vid), 'Exposure'));
if hasProperty
% We got all the properties so we can quit trying.
fprintf('We have all camera properties after %d attempts. Now continuing with program.\n', attempt);
break;
end
fprintf('We do not have all camera properties after %d attempts. Trying again.\n', attempt);
attempt = attempt + 1;
clear('vid');
end
%CAMERA CONFIGURATION
vid.Resolution='2048x1536';
vid.WhiteBalanceMode='manual';
vid.ExposureMode='manual';
vid.Exposure=-5.7;
vid.Gain=0;
vid.Hue=0; %-40 40
vid.Gamma=72; %72-500
vid.Contrast=64;
vid.Saturation=35;
vid.Sharpness=1;
vid.Brightness=15; %-64 64
vid.WhiteBalance=6500; %2800-6500
vid.BacklightCompensation=2; %0-2
end
img=snapshot(vid);

Answers (1)

Matthew Gingerich
Matthew Gingerich on 3 Oct 2018
This should open the camera unless it is already open:
if (exist('vid') == 0)
vid = webcam('USB Camera');
end

Categories

Find more on MATLAB Support Package for IP Cameras in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!