Select different cameras with a drop down menu

6 ビュー (過去 30 日間)
Alicia Wüst
Alicia Wüst 2021 年 6 月 14 日
回答済み: Vaibhav 2024 年 2 月 14 日
Hello everybody,
my plan is to create a drop down menu to select differnet webcams in my Bike-Fitting App.
E.g. I have an internal facetime webcam and any external webcam i want the drop down menu to display these two cams so the user can select the one he prefers.
Can anyone help me with the code ? I have no idea how to tell the drop down menu to display the possible webcams.
Thanks for your support!
Best regards!

回答 (1 件)

Vaibhav
Vaibhav 2024 年 2 月 14 日
Hi Alicia
I understand that you would like to create a dropdown showing all the webcams.
You can achieve this using the "uicontrol" function, and the "webcamlist" function to get the list of available webcams on your system.
Here is a code snippet for your reference:
function webcam_selector
% Create a figure for the GUI
fig = figure('Name', 'Webcam Selector', 'NumberTitle', 'off', 'MenuBar', 'none', 'ToolBar', 'none', 'Position', [500, 500, 300, 100]);
% Get the list of available webcams
cams = webcamlist();
% Check if any webcams are available
if isempty(cams)
errordlg('No webcams found!', 'Webcam Error');
return;
end
% Create a dropdown menu (popup menu) for webcam selection
cam_popup = uicontrol('Style', 'popupmenu', 'String', cams, 'Position', [50, 50, 200, 25], 'Callback', @selection_callback);
% Callback function for the dropdown menu
function selection_callback(src, event)
% Get the selected webcam index
idx = src.Value;
% Get the webcam name from the dropdown menu
selected_cam_name = cams{idx};
% Display the selected webcam name in the command window (for testing purposes)
fprintf('Selected webcam: %s\n', selected_cam_name);
% Here you can add the code to initialize the selected webcam and display its video feed
end
end
This script creates a GUI with a dropdown menu listing all the available webcams. When you select a webcam from the dropdown menu, the callback function "selection_callback" is called, which prints the name of the selected webcam to the MATLAB command window. You can modify the callback function to initialize the selected webcam and display its video feed as required for your Bike-Fitting App.
To run this script, simply save it as a ".m" file, and run it in MATLAB. Make sure you have the MATLAB Support Package for USB Webcams installed to use the "webcamlist" function.
You can refer to the MathWorks documentations below to learn more about "uicontrol" and "webcamlist" functions:
Hope it helps!

Community Treasure Hunt

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

Start Hunting!

Translated by