473,386 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Get JPG from MJPG Stream Problem

Greetings,

I am trying to get a JPG Frame from a MJPG Stream. A MJPG is basically a stream of JPGs which are splitted by a special boundary string. So I tried to get the stream, split it by the boundary strings and save the JPG binary data into a file.

The problem is, I cant save the data into a proper JPG file which I then open. This is my code:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Net;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Drawing.Imaging;
  9.  
  10. namespace JPGFroCGI
  11. {
  12.     class Program
  13.     {
  14.  
  15.         public string GetStringFromByte(byte[] dBytes)
  16.         {
  17.             System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
  18.             return enc.GetString(dBytes);
  19.  
  20.         }
  21.  
  22.         public static byte[] StrToByteArray(string str)
  23.         {
  24.             System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
  25.             return encoding.GetBytes(str);
  26.         }
  27.  
  28.         private string[] SplitByString(string testString, string split)
  29.         {
  30.             int offset = 0;
  31.             int index = 0;
  32.             int[] offsets = new int[testString.Length + 1];
  33.  
  34.             while (index < testString.Length)
  35.             {
  36.                 int indexOf = testString.IndexOf(split, index);
  37.                 if (indexOf != -1)
  38.                 {
  39.                     offsets[offset++] = indexOf;
  40.                     index = (indexOf + split.Length);
  41.                 }
  42.                 else
  43.                 {
  44.                     index = testString.Length;
  45.                 }
  46.             }
  47.  
  48.             string[] final = new string[offset + 1];
  49.             if (offset == 0)
  50.             {
  51.                 final[0] = testString;
  52.             }
  53.             else
  54.             {
  55.                 offset--;
  56.                 final[0] = testString.Substring(0, offsets[0]);
  57.                 for (int i = 0; i < offset; i++)
  58.                 {
  59.                     final[i + 1] = testString.Substring(offsets[i] + split.Length, offsets[i + 1] - offsets[i] - split.Length);
  60.                 }
  61.                 final[offset + 1] = testString.Substring(offsets[offset] + split.Length);
  62.             }
  63.             return final;
  64.         }
  65.  
  66.         public byte[] GetPictureByteArray(string url)
  67.         {
  68.             int count = 0;
  69.             byte[] buffer = new byte[1000000];
  70.             int read, total = 0;
  71.             // create StreamWriters
  72.             StreamWriter imagedata_writer = new StreamWriter("imagedata.txt");
  73.             StreamWriter streamdata_writer = new StreamWriter("streamdata.txt");
  74.  
  75.             Console.WriteLine("Creating HTTP request...");
  76.             HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  77.  
  78.             Console.WriteLine("Waiting for response...");
  79.             WebResponse resp = req.GetResponse();
  80.  
  81.             Console.WriteLine("Get response stream...");
  82.             Stream stream = resp.GetResponseStream();
  83.  
  84.             Console.WriteLine("Receiving data...");
  85.             Console.WriteLine("\nHeaders:" + resp.Headers);
  86.             Console.WriteLine("Reading data...");
  87.             while ((read = stream.Read(buffer, total, 1000)) != 0)
  88.             {
  89.                 total += read;
  90.                 // Limit frames through counter
  91.                 if (count != 200)
  92.                     count++;
  93.                 else
  94.                     break;
  95.             }
  96.  
  97.  
  98.             string sComplBuffer = GetStringFromByte(buffer);
  99.             Console.WriteLine("Writing into streamdata.txt...");
  100.             streamdata_writer.Write(sComplBuffer.Trim());
  101.  
  102.             // Split complete buffer into frames (which are separated by --myboundary)
  103.             string[] Frames = SplitByString(sComplBuffer, "--myboundary\r\n");
  104.             //Frames = SplitByString(Frames[2], "\r\n\r\n");
  105.  
  106.             // Save this one frame with imagedata_writer
  107.             imagedata_writer.Write(Frames[1]);
  108.             buffer = StrToByteArray(Frames[1].Trim());
  109.             Console.WriteLine("Writing into imagedata.txt...");
  110.             imagedata_writer.Flush();
  111.             streamdata_writer.Flush();
  112.             return buffer;
  113.  
  114.         }
  115.  
  116.         static void Main(string[] args)
  117.         {
  118.             Console.WriteLine("Program started.");
  119.             Program progrm = new Program();
  120.             byte[] bytarry = progrm.GetPictureByteArray("http://webcam.mmhk.cz/axis-cgi/mjpg/video.cgi");
  121.             Console.WriteLine("\nSaving image to 'image.jpg'...");
  122.  
  123.             FileStream fstrm = new FileStream("image.jpg", FileMode.Create);
  124.             fstrm.Write(bytarry, 0, bytarry.Length);
  125.             fstrm.Flush();
  126.  
  127.             Console.WriteLine("DONE - Press ENTER to close.");
  128.             Console.ReadLine();
  129.         }
  130.     }
  131. }
  132.  
I am using a public camera to test the MJPG stream. So now is the question: what am I doing wrong?
Jul 10 '08 #1
0 2037

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Sandeep | last post by:
Hi all, I am new to the ADODB.Stream I am using following code lRecordset.Open "Select * from <some table-name>" 'this query return more than 1000 records dim lstream as new ADODB.stream...
6
by: Yechezkal Gutfreund | last post by:
I have been using the following code (successfully) to read Xml formated text packets from a TCP stream. The output from the server stream consists of a sequence of well formed Xml documents...
0
by: mario.lat_ | last post by:
Hallo to all, I have write a little script for connecting to cisco router BUT I have a problem: I have to send to router all the commands and then I have to read the output. If I send a command1...
3
by: Sir Psycho | last post by:
Hi, For some reason, when i step over this code, it returns the full byte stream im expecting from the server, however when I let it run with no intervention, it only seems to grab a small chunk...
2
by: Mike P2 | last post by:
I made a Stream-inheriting class that just removes the tabs (actually the 4 spaces VS prefers to use) from the beginning of lines and empty lines. At first I was having trouble with it adding a...
8
by: T Driver | last post by:
Anyone have any idea how I can do the following? I have a connection to an XML file on a site I do not control, getting a string representation of the xml data that I can then feed to my...
33
by: john | last post by:
I am reading TC++PL3 and in "21.3.3 Stream State", 4 member functions returning bool are mentioned: template <class Ch, class Tr= char_traits<Ch class basic_ios: public ios_base { public: //...
3
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I'm creating an application that will read emails from GMail, using the System.Net.Sockets.TcpClient and POP protocol. However, I am having a problem with my SslStream. ...
1
by: Victor Lin | last post by:
Hi, I'm writting a application using python standard logging system. I encounter some problem with unicode message passed to logging library. I found that unicode message will be messed up by...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.