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
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.IO;
- using System.Net;
- using System.Data;
- using System.Drawing;
- using System.Drawing.Imaging;
- namespace JPGFroCGI
- {
- class Program
- {
- public string GetStringFromByte(byte[] dBytes)
- {
- System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
- return enc.GetString(dBytes);
- }
- public static byte[] StrToByteArray(string str)
- {
- System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
- return encoding.GetBytes(str);
- }
- private string[] SplitByString(string testString, string split)
- {
- int offset = 0;
- int index = 0;
- int[] offsets = new int[testString.Length + 1];
- while (index < testString.Length)
- {
- int indexOf = testString.IndexOf(split, index);
- if (indexOf != -1)
- {
- offsets[offset++] = indexOf;
- index = (indexOf + split.Length);
- }
- else
- {
- index = testString.Length;
- }
- }
- string[] final = new string[offset + 1];
- if (offset == 0)
- {
- final[0] = testString;
- }
- else
- {
- offset--;
- final[0] = testString.Substring(0, offsets[0]);
- for (int i = 0; i < offset; i++)
- {
- final[i + 1] = testString.Substring(offsets[i] + split.Length, offsets[i + 1] - offsets[i] - split.Length);
- }
- final[offset + 1] = testString.Substring(offsets[offset] + split.Length);
- }
- return final;
- }
- public byte[] GetPictureByteArray(string url)
- {
- int count = 0;
- byte[] buffer = new byte[1000000];
- int read, total = 0;
- // create StreamWriters
- StreamWriter imagedata_writer = new StreamWriter("imagedata.txt");
- StreamWriter streamdata_writer = new StreamWriter("streamdata.txt");
- Console.WriteLine("Creating HTTP request...");
- HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
- Console.WriteLine("Waiting for response...");
- WebResponse resp = req.GetResponse();
- Console.WriteLine("Get response stream...");
- Stream stream = resp.GetResponseStream();
- Console.WriteLine("Receiving data...");
- Console.WriteLine("\nHeaders:" + resp.Headers);
- Console.WriteLine("Reading data...");
- while ((read = stream.Read(buffer, total, 1000)) != 0)
- {
- total += read;
- // Limit frames through counter
- if (count != 200)
- count++;
- else
- break;
- }
- string sComplBuffer = GetStringFromByte(buffer);
- Console.WriteLine("Writing into streamdata.txt...");
- streamdata_writer.Write(sComplBuffer.Trim());
- // Split complete buffer into frames (which are separated by --myboundary)
- string[] Frames = SplitByString(sComplBuffer, "--myboundary\r\n");
- //Frames = SplitByString(Frames[2], "\r\n\r\n");
- // Save this one frame with imagedata_writer
- imagedata_writer.Write(Frames[1]);
- buffer = StrToByteArray(Frames[1].Trim());
- Console.WriteLine("Writing into imagedata.txt...");
- imagedata_writer.Flush();
- streamdata_writer.Flush();
- return buffer;
- }
- static void Main(string[] args)
- {
- Console.WriteLine("Program started.");
- Program progrm = new Program();
- byte[] bytarry = progrm.GetPictureByteArray("http://webcam.mmhk.cz/axis-cgi/mjpg/video.cgi");
- Console.WriteLine("\nSaving image to 'image.jpg'...");
- FileStream fstrm = new FileStream("image.jpg", FileMode.Create);
- fstrm.Write(bytarry, 0, bytarry.Length);
- fstrm.Flush();
- Console.WriteLine("DONE - Press ENTER to close.");
- Console.ReadLine();
- }
- }
- }