主要内容

Display JPEG Images Streamed from IP Camera

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

要创建一个工作示例,您需要:

  • IP address-based URL similar to:

    URL =.'http://999.99.99.99/video/mjpg.cgi';
  • 凭据类似于:

    creds = matlab.net.http.Credentials('Scheme','基本的','User','行政','经过','行政');creds.username =.'yourName'; creds.Password ='yourPassword';

CameraPlayerClass

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 aMultipartConsumerindicating that any part whose type is"image/*"should be handled byCameraPlayer。MATLAB®calls theCameraPlayer.putData接收的图像的每帧的方法,该方法调用其超类以转换数据。CameraPlayerin turn usesimshow要创建图形窗口以显示图像,并在同一窗口中显示后续图像。当用户关闭窗口时,Putdata.返回s停止=真实,导致MATLAB关闭连接。

classdefCameraPlayer < 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%是一个多级消息,这被指定为图像的处理程序% 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  - 构造函数% Image - the currently displayed Image object%下面显示接收的图像或接收的图像序列% in a multipart message and saves the last image received in the response%消息数据。直到CP为止,显示的最后一个图像仍然可见% deleted.%% req = RequestMessage;% cp = CameraPlayer;%resp = req.send(URL,[],GenericConsumer('Image / *',CP));%图像= cp.image;% ...operate on image data...%版权所有2017 MathWorks,Inc。特性% Image - the currently displayed Image object% This image is in an Axes in a Figure. It is deleted when this object is% deleted.Image%足够 - 控制显示消息的次数足够的endmethods功能obj = CameraPlayer() obj = obj@matlab.net.http.io.ImageConsumer();end功能[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);ifisempty(data)% end of image; display the result in Response.Body.Dataimdata = obj.response.body.data;ifiscell(imdata)if~isempty(imdata{2})% If it was an indexed image if we get a cell array, so convertIMDATA = IND2RGB(IMDATA {1},IMDATA {2});elseimdata = imdata{1};endendifisempty(obj.Image)% First time we are called, create a figure containing the imageobj.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;ifOBJ.ENOUGH> 100 DISP'To stop, close figure window.'obj.enough = 0;end尝试obj.Image.CData = imdata;catche% Fails if window closed or data was badifstrcmp(e.identifier,'MATLAB:class:InvalidHandle')% User must have closed the window; terminate silentlystop = true; disp'窗口关闭。'返回endendenddrawnowendend功能删除(obj)删除(obj.image);endendend

CallCameraPlayer

以下代码为检索图像提供框架。要运行代码,必须为内容提供内容的值<>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 theCameraPlayer, add it to your call to发送。For information about creating request messages, seeHTTP Interface

URL =.''; cp = CameraPlayer; consumer = matlab.net.http.io.MultipartConsumer('image/*',cp); creds = matlab.net.http.Credentials('Scheme','基本的','User','行政','经过','行政');creds.username =.''; creds.Password =''; opts = matlab.net.http.HTTPOptions('Cred',信誉,'SavePayload',true); r = matlab.net.http.RequestMessage(); resp = r.send(url, opts, consumer);