Code covered by the BSD License  

Highlights from
Video Recording

image thumbnail

Video Recording

by

 

Recording video & snapshots using a web cam or other.

CreateImAqObject(varargin)
function [A FullFilePath logfile TF memLim] = CreateImAqObject(varargin)

%varargin{1} is the handles structure passed to the function.
if isempty(varargin{1}.FullFilePath)
    [fileName,filepath,~] = uiputfile;
    ext = '.avi';
    fileName = [fileName ext];
    FullFilePath = [filepath fileName];
    if exist(FullFilePath, 'file')
        comm = menu('Select Option:', 'Continue and Acquire', 'Exit');
        switch comm
            case 1
                %do nothing.
                disp('over writting files...')
            case 2
                FullFilePath = '';
        end
    end
else
    FullFilePath = varargin{1}.FullFilePath;
end
% if video recording is selected.
if get(varargin{1}.popupmenu1, 'value')==1
    try
        logfile = avifile(FullFilePath,'Colormap',gray(256),...
            'compression','None', 'FPS', 25);
    catch e
        if length(varargin)==1
            set(varargin{1}.text_filepath, 'string', e.message),
        end
        errordlg(e.message),
        error(e.message),
    end
    if(exist(FullFilePath)==2)
        disp('AVI file created.'),
        TF = 1;
    else
        disp('Could not create the AVI LOG FILE.'),
        TF = 0;
        return,
    end
else
    logfile = '';TF = 1;
end

%use a user selected cam recorder if multiple is found.
%http://www.mathkb.com/Uwe/Forum.aspx/matlab/117290/real-time-image-process
%ing
cam_info = imaqhwinfo;
select_string = '';
cam_id = 1;
res = '';
available_cam = imaqhwinfo(char(cam_info.InstalledAdaptors(1)));
if (length(available_cam.DeviceIDs) > 1)
    cam_names = char(available_cam.DeviceInfo.DeviceName);
    for num_cams = 1:length(available_cam.DeviceIDs)
        select_string = strcat(select_string, [', ''' cam_names(num_cams,:) '''']);
    end    
    k = eval(['menu(''Select a Camera:''' select_string ')']);
    cam_id = k;
end
%select a resolution.
%if none selected then default res = 'YUY2_320x240' is used.
try
    ImageResolution = available_cam.DeviceInfo(cam_id).SupportedFormats;
    for ii = 1:length(ImageResolution)
        res = strcat(res, [', ''' ImageResolution{ii} '''']);
    end
    k = eval(['menu(''Select a Resolution:''' res ')']);
    res = ImageResolution{k};
catch    
    res = 'YUY2_320x240';
end
if isempty(res)
    res = 'YUY2_320x240';
end
result = 'Try Again';
%check the if device is available.
while strcmp(result,'Try Again')
    try
        try
            A = videoinput(char(cam_info.InstalledAdaptors(1)), cam_id, res);
        catch e
            disp(e.message),
        end
        preview(A);
        if get(varargin{1}.popupmenu1, 'value')==1
            result = 'Success';pause(0.05),
            stoppreview(A),closepreview,
            %,'ReturnedColorSpace','grayscale'
            set(A,'ReturnedColorSpace', 'grayscale', 'LoggingMode', 'disk&memory');
            set(A,'TriggerRepeat', Inf, 'TriggerFrameDelay', 0);
            %disp('Information about the video Input Object:'),
            disp(['Preview is set to: ' get(A,'Previewing') '.']),
            %allocate 3 GByte of memory to logg image data into memory buffer.
        elseif get(varargin{1}.popupmenu1, 'value')==2
            %do nothing and carry on.
            result = 'Success';
        end
        try
            MemLimit = imaqmem(varargin{1}.MemThreshold);memLim = varargin{1}.MemThreshold;
            if MemLimit.FrameMemoryLimit >= (MemLimit.AvailPhys-0.60e9)
                MemLimit = imaqmem(MemLimit.AvailPhys-0.30e9);
                memLim = MemLimit.AvailPhys-0.30e9;
                disp('')
            end                   
            disp(['Allocating ' num2str(MemLimit.FrameMemoryLimit) ' Bytes, of Memory for Image Aquisition.']),
        catch e
            disp(e.message),
        end        
    catch
        delete(A)
        result = questdlg(['Image acquisition device is not available. '...
            'It may be in use by another application.'],...
            'Error Accessing Hardware','Try Again','Abort','Try Again');
    end
    try
        %preview(A);pause(1)
    catch
    end
end

Contact us