hCam = webcam(1);
figHandle = createWindow(hCam);
while true
pause(0.001);
if(isvalid(hPbExit) == 1 && getappdata(hPbExit, 'exitKeyPressed') == 1)
if(isvalid(hCam) == 1)
hCam.closePreview;
delete(hCam);
end
delete(figHandle);
return;
end
end
function exitPressFcn(hObject, ~)
setappdata(hObject, 'exitKeyPressed', 1);
end
function my_closereq(src,callbackdata)
global hPbExit;
setappdata(hPbExit, 'exitKeyPressed', 1);
end
function figHandle = createWindow(hCam)
global hPbExit;
figHandle = figure('CloseRequestFcn', @my_closereq, 'Name', 'Tracker', 'tag', 'mainFigure', 'OuterPosition', [100, 100, 800, 800]);
clf(figHandle);
set(figHandle, 'Name', 'カメラ映像', 'NumberTitle', 'off')
set(figHandle, 'MenuBar', 'none');
if(isempty(hCam) == 0 && isvalid(hCam) == 1)
ax = axes('parent', figHandle);
yticks(ax, []);
xticks(ax, []);
frame = snapshot(hCam);
im = image(ax, zeros(size(frame), 'uint8'));
axis(ax, 'image');
preview(hCam, im);
end
hPbExit = uicontrol(figHandle, 'Style', 'pushbutton', 'String', '終了', 'FontSize', 12,...
'Units', 'normalized', 'Position', [0.6 0.1 0.3 0.05], 'Callback', @exitPressFcn);
setappdata(hPbExit, 'exitKeyPressed', 0);
drawnow;
end