开发者

need to compress QImage to send it via IP

i'm programming an application (in C++/Qt Designer 4.6 and using some librairies like ffmpeg and v4l2) which capture from webcam and i want to send the captured QImage via IP, so i send it into a QTcpSoc开发者_如何学JAVAket and i succed in receiving it in my server application but the problem it's too slow, crearly because the QImage isn't compressed so i'm not getting the wanted result which is live video streaming via IP, my question is how can i compress the QImage? I think in converting it to the YUV format but i can't realize it, this is some lines from my code to send the available QImage:

QImage image;
QByteArray ba;
QBuffer buffer(&ba);
image.save(&buffer, "PNG");
imsocket->write(ba);


You would be better off sending an MPEG compressed video stream instead of compressing each frame as QImage and sending it over network.

MPEG compressed video streams use a technique which uses a full scale image for first frame and then only records changed pixels for each proceeding frame thus providing maximum compression as well as smooth video playback over the network.

On the client end, you will playback by getting frames generated by the library from the compressed MPEG stream.


I had a similar problem as the above, my code looked like this:

QBuffer buffer;
buffer.open(QIOdevice::WriteOnly);
QDataStream stream(&buffer);
stream << image;

Using QElapsedTimer I found that encoding the image took more than 500 msec for a 1024x768 sized image. Using valgrind I found that the time was spend in png_write_image which was called from QImageWriter::write(QImage)

Changing the code to encode as a JPG instead, made the code execute in only 5ms for the same image. The resulting code looked like this:

QBuffer buffer;
buffer.open(QIODevice::WriteOnly);
image.save(&buffer,"JPEG");

Unfortunately I found no way to tell QDataStream to encode images in JPG rather than PNG, so the decoding side obviously needed to be adapted to decode the image like:

image.load(&buffer, "JPEG");


Before you send off the image, you can compress the byte array and then de-compress it on the server. This should provide a nice boost for you.

QByteArray arr, cArr;
cArr = qCompress(arr);


You should adhere to the first rule of optimization: Don't optimize when you have not measured where your application spends its time. I asume you are just guessing which part pf your application is slow.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜