Data Extraction jpeg/mjpeg

Hello
I'm looking to extract jpeg image data from a mjpeg stream, but I'm not sure how to do this in c++ .

The code snippet below does the job of connecting to the remote ip camera server & receives the mjpeg stream via INCOMING_STREAM variable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
		// Loop and grab mjpeg stream
		while(true)
		{
			int retval;
			retval = recv(hSocket, tempBuffer, sizeof(tempBuffer)-1, 0);
			if (retval==0)
			{
				break; // Connection has been closed
			}
			else if (retval==SOCKET_ERROR)
			{
				throw HRException("socket error while receiving.");
			}
			else
			{
				//***************************************************
				//
				// This is where we receive the incoming data stream
				//
				//***************************************************
				// retval is number of bytes read
				// Terminate buffer with zero				
				tempBuffer[retval] = 0;
				
				INCOMING_STREAM=tempBuffer;

                
			}
		}

The ip camera server returns the headers followed by jpeg data between --myboundary tags like so

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
HTTP/1.0 200 OK 
Cache-Control: no-cache 
Pragma: no-cache 
Expires: Thu, 01 Dec 1994 16:00:00 GMT 
Connection: close 
Content-Type: multipart/x-mixed-replace; boundary=--myboundary  

--myboundary 
Content-Type: image/jpeg Content-Length: 19529 

<<jpeg image data>
--myboundary
Content-Type: image/jpeg Content-Length: 19529 

<<jpeg image data>
--myboundary

Continues...

My question is what would be the best solution to achieve this data extraction process in c++, string function? Any coding examples help/advice would be greatly appreciated.

Best Regards
Steve
My question is what would be the best solution to achieve this data extraction process in c++, string function? Any coding examples help/advice would be greatly appreciated.


First, I think you may want to consider a Regular Expression libraries (someone told me Boost::RegEx) to help you extract those <<jpeg image data> using --myboundary as the boundary marker to indicate each jpeg image data.

Second, it seems to be using HTTP so for binary stream data, they maybe using Base64 encoding so you may need to find a Base64 library for decoding to get the plain raw bytes.

Third, after getting the plain raw bytes representing the jpeg image data, you may want to use a jpeg library to read/write or analyze that jpeg image. http://en.wikipedia.org/wiki/Libjpeg is one jpeg library.

Thankyou for they reply.
three interesting points I,ll look into.

Regards
Steve
Topic archived. No new replies allowed.