Main Content

Display JPEG Images Streamed from IP Camera

This example shows how to use an HTTP MultipartConsumer to stream video from a website. It customizes an ImageConsumer class-CameraPlayer-to display JPEG images from a hypothetical website derived from an IP address.

To create a working example, you need:

  • IP address-based URL similar to:

    url = 'http://999.99.99.99/video/mjpg.cgi';
  • Credentials similar to:

    creds = matlab.net.http.Credentials('Scheme','Basic','User','admin','Pass','admin');
    creds.Username = 'yourName';
    creds.Password = 'yourPassword';
    

CameraPlayer Class

An IP camera sends JPEG images as parts of a never-ending multipart message. Each part has a type of"image/jpeg". To process this message, create a MultipartConsumer indicating that any part whose type is "image/*" should be handled by CameraPlayer. MATLAB® calls the CameraPlayer.putData method for each frame of the image received, which calls its superclass to convert the data. CameraPlayer in turn uses imshow to create a figure window to display the image, and displays subsequent images in the same window. When the user closes the window, putData returns stop=true, which causes MATLAB to close the connection.

classdef CameraPlayer < matlab.net.http.io.ImageConsumer
% CameraPlayer Player for IP camera
%   A ContentConsumer that displays image content types in a figure window.  If
%   specified directly in the RequestMessage.send() operation, it assumes the
%   entire contents of the ResponseMessage is a single image.  If the response
%   is a multipart message, and this is specified as a handler for image
%   types to a GenericConsumer or MultipartConsumer, then this assumes each
%   part is a frame of a video stream and displays them as they are received.
%
%   CameraPlayer properties:
%     CameraPlayer - constructor
%     Image        - the currently displayed Image object
%  
%   The following displays a received image, or a sequence of images received
%   in a multipart message and saves the last image received in the response
%   message Data.  The last image displayed remains visible until cp is
%   deleted.
% 
%     req = RequestMessage;
%     cp = CameraPlayer;
%     resp = req.send(url, [], GenericConsumer('image/*', cp));
%     image = cp.Image;
%     ...operate on image data...

% Copyright 2017 The MathWorks, Inc.

    properties
        % Image - the currently displayed Image object
        %   This image is in an Axes in a Figure.  It is deleted when this object is
        %   deleted.
        Image
        % Enough - control number of times to display message
        Enough
    end
    
    methods
        function obj = CameraPlayer()
            obj = obj@matlab.net.http.io.ImageConsumer();
        end
        
        function [len, stop] = putData(obj, data)
        % putData - called by MATLAB or parent Consumer to process image data
        
            % Pass the data to ImageConsumer, which buffers it until the end of data
            % and then converts it to a MATLAB image depending on the type of image.
            [len, stop] = obj.putData@matlab.net.http.io.ImageConsumer(data);
            if isempty(data)
                % end of image; display the result in Response.Body.Data
                imdata = obj.Response.Body.Data;
                if iscell(imdata) 
                    if ~isempty(imdata{2})
                        % If it was an indexed image if we get a cell array, so convert
                        imdata = ind2rgb(imdata{1},imdata{2});
                    else
                        imdata = imdata{1};
                    end
                end
                if isempty(obj.Image)
                    % First time we are called, create a figure containing the image
                    obj.Image = imshow(imdata);
                    obj.Enough = 0;
                else
                    % Subsequent times we are called, just change CData in an already-displayed
                    % image.

                    obj.Enough = obj.Enough+1;
                    if obj.Enough > 100 
                        disp 'To stop, close figure window.'
                        obj.Enough = 0;
                    end
                    try
                        obj.Image.CData = imdata;
                    catch e
                        % Fails if window closed or data was bad
                        if strcmp(e.identifier, 'MATLAB:class:InvalidHandle')
                            % User must have closed the window; terminate silently
                            stop = true;
                            disp 'Figure window closed.'
                            return
                        end
                    end
                end
                drawnow
            end
        end
        
        function delete(obj)
            delete(obj.Image);
        end
    end
end

Call CameraPlayer

The following code provides a framework for retrieving images. To run the code, you must provide values for content within <> characters. The URL for your web service might include additional parameters, such as login information and other information specified as name, value pair arguments. To utilize the CameraPlayer, add it to your call to send. For information about creating request messages, see Call Web Services from MATLAB Using HTTP.

url = '<YOUR_URL_CONTAINING_IP_ADDRESS>';
cp = CameraPlayer;
consumer = matlab.net.http.io.MultipartConsumer('image/*',cp);
creds = matlab.net.http.Credentials('Scheme','Basic','User','admin','Pass','admin');
creds.Username = '<YOURNAME>';
creds.Password = '<YOURPASSWORD>';
opts = matlab.net.http.HTTPOptions('Cred',creds,'SavePayload',true);
r = matlab.net.http.RequestMessage();
resp = r.send(url, opts, consumer);