classdef CamHelper < handle
    %CAMHELPER Summary of this class goes here
    %   Detailed explanation goes here
    
    properties
        Cam
        CamName = 'USB Camera';
        cameraParams;
    end
    
    methods
        function obj = CamHelper()
            %CAMHELPER Construct an instance of this class
            %   Detailed explanation goes here
            
        end
        
        function [imOut,imIn] = correctSnapshot(obj)
            
            if ~isempty(obj.Cam)
                imIn = snapshot(obj.Cam);
                imOut = imIn;
                if  strcmp(obj.CamName,'USB Camera')
                    if isempty(obj.cameraParams)
                        obj.cameraParams = load('cameraParams.mat');
                    end
                    imOut = undistortFisheyeImage(imOut,obj.cameraParams.cameraParams.Intrinsics);
                end
            end
        end
        
        function stateChng = CamOpen(obj)
            stateChng = 0;
            if isempty(obj.Cam)
                obj.Cam = webcam(obj.CamName);
                stateChng = 1;
                pause(0.1)
            end
        end
        
        function CamClose(obj)
            if ~isempty(obj.Cam)
                obj.Cam.delete;
                obj.Cam = [];
            end
        end
        
        function delete(obj)
            delete(obj.Cam);
            obj.Cam = [];
        end
    end
end

