QQueue to QByteArray? C# to C++
-
[quote author="azkay" date="1302352827"]Yeah, the only reason I was using Queue was because I used it in C#.
Though, I think ive got this working, I used std::stringstream instead of QQueue, seems to be working.
[/quote]Can you give us some more hints on what types you work on (you mentioned bytes and strings), we can point you to some convenient Qt classes then - there's almost everything here :-)
-
What ive done seems to not be working either.
Maybe itll be easier if I give you everything ive done so far;
Also, sorry for sounding too bad, been working on this for hours and its getting frustrated.
Ive coded in lots of different languages, but everytime I try to learn C++ again, I end up doing it for a couple of days, getting annoyed and moving on for a few months, always coming back, though.I thought using the stringstream would fix it, but I guess not.
What im doing is, using "Smartcam":http://sourceforge.net/projects/smartcam/files/smartcam_windows/SmartCam_v1_4/ as the server, I coded up a "client":http://talk.maemo.org/showthread.php?t=71452 for the Nokia N900 (in C#) to stream the N900s camera to it.
People suggested I learn some C++ and try to port it over, to save having to install all the mono libs on the n900 just to run it.So thats the background.
This part of the code is what im trying to port over, which is basically the "heart" of it.
It encodes the jpeg with the smartcam headers, then sends them to the smartcam server.
Biggest problem has been trying to get a BitConverter.GetBytes() equivalent, which in the c++ code is sort of working/sort of broken, most probably very wrong but its the closest I got while getting annoyed at it all.C#: http://pastebin.com/EqxKk4H0
C++: http://pastebin.com/XW8WPZH0If im making no sense, sorry.
-
Thanks for the detailed infos. What you want to achieve should be easy in Qt. I'll have a look at it later and will come back with some sample code for you. I just need to do some gardening first ;-)
I can understand that you are frustrated. C++ is a very powerful tool with the cost that you can easily shoot you in the foot. Maybe it's a bit hard to learn both C++ and the use of some 3rdparty libraray together with the more advanced parts of Qt (say networking for example) all in one step :)
-
Unfortunately, the pastebin files are no longer available, can you please paste the code here. I have some solution at hand but want to check that it actually fits the original C# algorithms.
-
Sorry, here:
@
using System;
using System.IO;
using System.Collections.Generic;namespace enqueue{
class MainClass {
public static void Main (string[] args){
Queue<byte> _buffer = new Queue<byte>();
byte[] _fileData = File.ReadAllBytes("test.jpg");
byte[] _tmpSize = BitConverter.GetBytes(_fileData.length());_buffer.Enqueue(1); _buffer.Enqueue(_tmpSize[2]); _buffer.Enqueue(_tmpSize[1]); _buffer.Enqueue(_tmpSize[0]); foreach(byte _b in _fileData){ _buffer.Enqueue(_b); } byte[] _packet = _buffer.ToArray(); //tcp sending would be here } }
}
@ -
Hi azkay,
a possible Qt code for you C# would be this:
@
#include <QtCore/QBuffer>
#include <QtCore/QFile>int foo()
{
QFile file("test.jpg");
if(file.open(QFile::ReadOnly))
{
QByteArray _fileData = file.readAll();
QByteArray _buffer;
qint32 nLength = _fileData.length();_buffer.append(1); // this doies the same a C# BitConverter.GetBytes(_fileData.length()); and then adding array[2], array[1] array[0] _buffer.append((nLength >> 16) & 0xFF); _buffer.append((nLength >> 8) & 0xFF); _buffer.append(nLength & 0xFF); _buffer.append(_fileData); //tcp sending would be here }
}
@The only tricky thing is converting the result of this:
@
BitConverter.GetBytes(_fileData.length());
@Instead of having two byte arrays, you could also work with only one:
@
#include <QtCore/QBuffer>
#include <QtCore/QFile>int foo()
{
QFile file("test.jpg");
if(file.open(QFile::ReadOnly))
{
QByteArray _buffer = file.readAll();
qint32 nLength = _buffer.length();// this does the same a C# BitConverter.GetBytes(_fileData.length()); and then adding array[2], array[1] array[0] _buffer.prepend(nLength & 0xFF); _buffer.prepend((nLength >> 8) & 0xFF); _buffer.prepend((nLength >> 16) & 0xFF); _buffer.prepend(1); //tcp sending would be here }
}
@ -
That is amazing, thank you all so much.
-
Prepending data on a QByteArray is a costly operation, as the content of the existing byte array must be copied.
As you will eventually send the data via a QTcpSocket, there is no need to buffer the data in a QByteArray. Just do some sanity checks (file.length() must not be greater than 2^24 = 256256256 = 16777216) and the write the data directly to the socket:
@
QFile file("test.jpg");
qint64 fileSize = file.size();// 3 bytes = 24 bits:
// 2^24 = 256256256 = 16777216
if(fileSize > 16777216) {
qDebug() << "file too big";
return;
}if(!file.open(QFile::ReadOnly)) {
qDebug() << "file cannot be opened";
return;
}QByteArray fileContents = file.readAll();
file.close();_tcpSocket.write(1); // the type
_tcpSocket.write((fileSize >> 16) & 0xFF);
_tcpSocket.write((fileSize >> 8) & 0xFF);
_tcpSocket.write(fileSize & 0xFF);
_tcpSocket.write(fileContents);
@ -
Ok, that's better, you are right.
I left out the socket part and thought, if you want to post process it, the it would be better top have the content in the array.you could also first put the first 4 bytes in and then append the file content , but that also copies the content around...
And perhaps you want to put the tcp stuff in a separate function....
-
Thanks again, ive successfully (after a few more issues that I sorted out) gotten it to stream from my phones camera to the smartcam server.
Next step, making the GUI. Once all the bugs are fixed, step after that will be looking at recoding my own smartcam server/webcam filter to support higher resolutions and maybe other things, which I have a feeling will be a lot more trouble than this was.