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: - 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();
-
}
-
}
-
}
-
I am using a public camera to test the MJPG stream. So now is the question: what am I doing wrong?
0 2060 Sign in to post your reply or Sign up for a free account.
Similar topics |
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
'assigning the recordset data to the stream
lrecordset.save lstream
lStream.Position = 0
|
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 written to the output stream.
We are willing to pay $ to any expert (e.g. MVP) consultant who has to time
to help us track down this problem.
(we will discuss rates if you can prove your expertise, and problem solving
approach).
|
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 and read the output for command1 the script works
well,
If I sent command1 , I send command2 and then I read output for command1
and command2 it works well.
If i send command1 and then I read the output for command1 then send
command2 I can't...
|
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 on the stream.
What am I doing wrong? There is more code than this, but this is the
problem code.
|
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 null character
in the middle of the output. I think it was a null character, Firefox
displays it as a '?' and the W3C validator says it's a non-sgml
character '0'. I changed the code around a bit and now the only
problem is that it (on some pages of...
| |
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
XmlDataSource object (CurrentXMLData):
Stream newStream =
myWebClient.OpenRead(myStringWebResource);
TextReader newReader = new StreamReader(newStream);
string newData = newReader.ReadToEnd();
CurrentXMLData.Data = newData;
|
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:
// ...
bool good() const; // next operation might succeed
bool eof() const; // end of input seen
|
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.
After I create the TcpClient, I create my SslStream...
stream = new System.Net.Security.SslStream(tcpclient.GetStream()...);
|
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 logging handler.
piese of StreamHandler:
try:
self.stream.write(fs % msg)
except UnicodeError:
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |