App Designer: How to use a camera in different Callbacks

3 次查看(过去 30 天)
Hello everybody,
I am using App Designer and try to implement a camera into my application.
I have one Callback in which I open a live stream from a camera, when a Button is pushed. To realise this, I use the following Code:
app.vid = videoinput('gentl', 1,'Mono8');
hImage = image(app.UIAxes,zeros(1024,1280,3));
app.UIAxes.XLim = [0,1280];
app.UIAxes.YLim = [0,1024];
app.UIAxes.XTick = [];
app.UIAxes.YTick = [];
pbaspect(app.UIAxes,[1280,1024,1]);
preview(app.vid,hImage);
Now, in a different Callback, I want to gather an image from the actual live picture. Right now i am using the following Code:
asd= getdata(app.vid);
save('path','asd');
Unfortunately get the following Error when calling:
Error using imaqdevice/getdata (line 154) OBJ is not running and no frames are available.
Can somebody help me to solve my problem?

采纳的回答

Prabhan Purwar
Prabhan Purwar 2020-1-23
Hi,
Folllowing is the code and app design
design.PNG
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
UIAxes matlab.ui.control.UIAxes
UIAxes2 matlab.ui.control.UIAxes
Button matlab.ui.control.Button
end
properties (Access = public)
cam;
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.UIAxes2.Visible = 'off';
evalin("base", 'clear cam');
app.cam = webcam;
frame = snapshot(app.cam);
im = image(app.UIAxes, zeros(size(frame), 'uint8'));
axis(app.UIAxes, 'image');
preview(app.cam, im);
end
% Button pushed function: Button
function ButtonPushed(app, event)
img = snapshot(app.cam);
image(app.UIAxes2, img);
app.UIAxes2.Visible = 'off';
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'UI Figure';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Title')
xlabel(app.UIAxes, 'X')
ylabel(app.UIAxes, 'Y')
app.UIAxes.Position = [26 270 300 185];
% Create UIAxes2
app.UIAxes2 = uiaxes(app.UIFigure);
title(app.UIAxes2, 'Title')
xlabel(app.UIAxes2, 'X')
ylabel(app.UIAxes2, 'Y')
app.UIAxes2.Position = [25 43 300 185];
% Create Button
app.Button = uibutton(app.UIFigure, 'push');
app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
app.Button.Position = [447 249 100 22];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = app1
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
Error you are facing instantiates that the code is not able to access the camera object at that instant.
Hope it helps!!

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by