Page 3 of 3 FirstFirst 123
Results 41 to 55 of 55

Thread: Wireless Video

  1. #41
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Wireless Video

    Hey I m able to sendwhole image over socket to client but only half of bytes are received at client side

    Now my server side code is
    Qt Code:
    1. QBuffer buffer;
    2. QImageWriter writer(&buffer, "png");
    3. writer.write(pic);
    4. QByteArray data;
    5. QDataStream stream(&data,QIODevice::WriteOnly);
    6. stream.setVersion(QDataStream::Qt_4_6);
    7. stream << (quint32)buffer.data().size();
    8. data.append(buffer.data());
    9. nWrite = gtcpSocket->write(data);
    10.  
    11. QMessageBox::critical(this, QString::number(data.length()),QString::number((int)nWrite));
    To copy to clipboard, switch view to plain text mode 
    MessageBox shows image size and bytes transferred to be 9441 but only 4380 bytes are received at client side


    My client side code is
    Qt Code:
    1. QDataStream in(tcpSocket);
    2. in.setVersion(QDataStream::Qt_4_6);
    3. QByteArray* array = new QByteArray();
    4. *array = tcpSocket->readAll();
    5. QMessageBox::critical(this,tr("Bytes rcvd %1."),QString::number(array->length()));
    6. QImage image;
    7. image = QImage::fromData(array->data(),"png");
    8. image.save("rcvdimage.png","png");
    To copy to clipboard, switch view to plain text mode 
    where the problem lies?

  2. #42
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Wireless Video

    Quote Originally Posted by Qt Coder View Post
    The messageBox shows ba.length() to be 9437 and nWrite to be 8 only.
    Close the buffer before writing its contents to the socket.
    where as the file size is 7045 bytes and size on disk 8192 bytes ,I dont know from where it has added extra bytes.
    From the cluster size on your filesystem, most probably.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #43
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Wireless Video

    readAll reads all available data, but in your case the date may not have all arrived when you call it. Following is a little (untested) code showing you how to send and receive a length. You should be able to extend it from here.
    Qt Code:
    1. //server
    2. /* Assumes data is a QByteArray containing your image. */
    3.  
    4. /* First get our number into a QByteArray */
    5. QByteArray len = QByteArray::number(data.length(), 10);
    6.  
    7. /* Pad it with spaces, so that it has a constant width. */
    8. len = len.leftJustified(10, ' ', false);
    9.  
    10. /* Put the number at the start of our data array. */
    11. data.prepend(len);
    12.  
    13. /* Now send our data to the socket, */
    14. g_tcpsocket->write(data);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //client
    2. while (data.length() < 10)
    3. {
    4. /* Read all available data. */
    5. QByteArray received = tcpsocket->readAll();
    6.  
    7. /* Add received data to the end of our byte array. */
    8. data += received;
    9.  
    10. /* Wait until the next packet of data arrives. */
    11. tcpsocket->waitForReadyRead();
    12. }
    13.  
    14. /* Get the first ten bytes, making up our number. */
    15. QByteArray len = data.left(10);
    16.  
    17. /* Convert to an integer. */
    18. int length = len.toInt();
    19.  
    20. /* Remove the first ten bytes from our data array. */
    21. data.remove(0, 10);
    22.  
    23. /* Now repeat the loop above until you have length bytes in data. */
    To copy to clipboard, switch view to plain text mode 

  4. #44
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Wireless Video

    A strange behavior at client side,If I put a messagebox just before reading data from socket then it reads all 9441 bytes.

    Before reading any data from socket I m checking how many bytes the server has send
    Qt Code:
    1. QMessageBox::critical(this,tr("Bytes available"),QString::number((int)tcpSocket->bytesAvailable()));
    To copy to clipboard, switch view to plain text mode 

    it shows 4380 bytes available but reads 9441 bytes if I put msgbox before start reading .

    If no msgbox is there it reads only 4380 bytes why so.

    Also I m not able to reconstruct the image from this 9441 bytes of data

    My client side code is
    Qt Code:
    1. QDataStream in(tcpSocket);
    2. in.setVersion(QDataStream::Qt_4_6);
    3.  
    4. QMessageBox::critical(this,tr("Bytes available %1."),QString::number((int)tcpSocket->bytesAvailable()));
    5. ba = tcpSocket->readAll();
    6. QMessageBox::critical(this,tr("Bytes rcvd %1."),QString::number(ba.length()));
    7.  
    8.  
    9. QImage icon(128, 128, QImage::Format_RGB32);
    10. QImageReader reader(ba,"png");
    11. if(reader.canRead())
    12. QMessageBox::critical(this,tr("Image can be read"),"");
    13. else
    14. QMessageBox::critical(this,tr("Error"),reader.errorString());
    15. if (reader.read(&icon)) {
    16.  
    17. //pixmap.loadFromData(QImage());
    18. QMessageBox::critical(this,tr("Time to set icon"),"");
    19.  
    20.  
    21. ui.label->setPixmap(QPixmap::fromImage(icon));
    22. }
    To copy to clipboard, switch view to plain text mode 
    Errorstring returns "File not found" and My debugging output is
    Qt Code:
    1. QFSFileEngine::open: No file name specified
    2. QFSFileEngine::open: No file name specified
    To copy to clipboard, switch view to plain text mode 

  5. #45
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Wireless Video

    @numbat

    Hey I added the code u suggested
    but at client side

    Qt Code:
    1. while (data.length() < 10)
    2. {
    3. /* Read all available data. */
    4. QByteArray received = tcpSocket->readAll();
    5.  
    6. /* Add received data to the end of our byte array. */
    7. data += received;
    8.  
    9. /* Wait until the next packet of data arrives. */
    10. //tcpSocket->waitForReadyRead();
    11. }
    To copy to clipboard, switch view to plain text mode 

    if I uncomment that tcpSocket->waitForReadyRead(); my program hangs. Now my another question is how to construct image from data I received?

  6. #46
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Wireless Video

    Since you are using a char * and not providing a length, how does the method know how many bytes to send? Its logical to assume that it'll stop at the first null byte, which could be 8 bytes in.

    Now, if only there was a method that could use your QByteArray directly.... ;-)

  7. #47
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Wireless Video

    I have wriiten a test application where I m converting the Qimage to QByteArray and converting this QByteArray to QImage again and setting as pixmap on label.

    Here is the code

    Qt Code:
    1. //Image to be converted to QByteArray , stored in debug folder
    2. QImage pic("monkey_off_128x128.png","PNG");
    3.  
    4. //Writing to QByteArray
    5.  
    6. QImageWriter writer(&b, "png");
    7. writer.write(pic);
    8. QDataStream stream(&ba,QIODevice::WriteOnly);
    9. stream.setVersion(QDataStream::Qt_4_6);
    10. b.open(QBuffer::ReadWrite);
    11. stream << (quint32)b.data().size();
    12. ba.append(b.data());
    13. b.close();
    14.  
    15.  
    16.  
    17. //Reading from QByteArray
    18.  
    19. QPixmap pixmap;
    20. QImage ImagePNG;
    21.  
    22. QBuffer buf(&ba);
    23.  
    24. buf.open(QIODevice::ReadWrite);
    25. ImagePNG.loadFromData(ba,"png");
    26.  
    27. ViewObj->ui.label->setPixmap(QPixmap::fromImage(ImagePNG));
    28.  
    29. if(ImagePNG.isNull())
    30. QMessageBox::critical(this,"NULL IMAGE",QString::number(ba.length()));
    To copy to clipboard, switch view to plain text mode 
    my image size is 8192 bytes
    but when I test this code but it shows 9441 bytes in QByteArray also ImagePNG.isNull returns true and no image is displayed.

    Where I m going wrong?

  8. #48
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Wireless Video

    Please define "image size"

  9. #49
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Wireless Video

    I m not getting you

    The image size is 7045 Bytes and size on disk is 8192 Bytes (I got this information from file properties right clicking the image file) but my test program shows 9441 bytes in QByteArray,I dont know from where it has got these extra bytes ,may be bcoz of this image is not constructed properly....

  10. #50
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Wireless Video

    What you are doing here doesn't make much sense. Either use QDataStream for everything or don't use it at all.

    Qt Code:
    1. QImage img(...);
    2. QDataStream stream(&ba, QIODevice::WriteOnly);
    3. stream << img;
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. QByteArray ba = ... ;
    2. QDataStream stream(ba, QIODevice::ReadOnly);
    3. QImage img;
    4. stream >> img;
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #51
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Wireless Video

    Quote Originally Posted by Qt Coder View Post
    I m not getting you

    The image size is 7045 Bytes and size on disk is 8192 Bytes (I got this information from file properties right clicking the image file) but my test program shows 9441 bytes in QByteArray,I dont know from where it has got these extra bytes ,may be bcoz of this image is not constructed properly....
    PNG is a compressed file, so Image size could be the size of the file, the size of the raw data, or the size of the uncompressed data. Thats why I asked you to define it.

    So if the compressed file size is 7045, then it is logical that the uncompressed file size (ie, when the file is loaded) will be larger.

  12. #52
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Wireless Video

    I m able to send and receive as well as display image .

    My latest server side code is

    Qt Code:
    1. IplImage *image=cvQueryFrame(camera);
    2. CapturedImage = cvwidget->putImage(image);
    3.  
    4. cvwidget->imagelabel->setPixmap(QPixmap::fromImage(CapturedImage)); //For viewing captured image
    5.  
    6. QByteArray data;
    7. QBuffer buffer(&data);
    8. buffer.open(QIODevice::WriteOnly);
    9. CapturedImage.save(&buffer, "PNG");
    10. buffer.close();
    11.  
    12. QByteArray len = QByteArray::number(data.length(), 10);
    13.  
    14. /* Pad it with spaces, so that it has a constant width. */
    15. len = len.leftJustified(10, ' ', false);
    16.  
    17. /* Put the number at the start of our data array. */
    18. data.prepend(len);
    19.  
    20. /*send QbyteArray over socket */
    21. nWrite = gtcpSocket->write(data);
    To copy to clipboard, switch view to plain text mode 

    My client side code for receiving and displaying image is

    Qt Code:
    1. void MainWindow::readDatafromServer() //Slot conneted to readyRead() signal
    2. {
    3.  
    4. qint64 nRead;
    5. QDataStream in(tcpSocket);
    6. in.setVersion(QDataStream::Qt_4_6);
    7. QByteArray data;
    8. while (data.length() < 10)
    9. {
    10. /* Read all available data. */
    11. QByteArray received = tcpSocket->readAll();
    12.  
    13. // QMessageBox::critical(this,tr("first"),QString::number(received.length()));
    14. /* Add received data to the end of our byte array. */
    15. data += received;
    16.  
    17. /* Wait until the next packet of data arrives. */
    18. // tcpSocket->waitForReadyRead();
    19. }
    20.  
    21. /* Get the first ten bytes, making up our number. */
    22. QByteArray len = data.left(10);
    23.  
    24. /* Remove the first ten bytes from our data array. */
    25. data.remove(0, 10);
    26.  
    27. QString str( len);
    28. bool ok;
    29. int length = str.toInt(&ok,10);
    30. // QMessageBox::critical(this,str,QString::number(length));
    31. // QMessageBox::critical(this,"ABytes",QString::number((int)tcpSocket->bytesAvailable()));
    32.  
    33.  
    34. while (data.length() < length)
    35. {
    36. /* Read all available data. */
    37. QByteArray receivedB = tcpSocket->readAll();
    38. // QMessageBox::critical(this,tr("second"),QString::number(receivedB.length()));
    39.  
    40.  
    41. /* Add received data to the end of our byte array. */
    42. data += receivedB;
    43.  
    44. /* Wait until the next packet of data arrives. */
    45. //tcpSocket->waitForReadyRead();
    46. }
    47. QBuffer buf;
    48. buf.setBuffer(&data);
    49. buf.open(QIODevice::ReadOnly);
    50. qir.setDevice(&buf);
    51. QImage ImagePNG;
    52. qir.read(&ImagePNG);
    53.  
    54. ViewObj->ui.label->setPixmap(QPixmap::fromImage(ImagePNG));
    55. }
    To copy to clipboard, switch view to plain text mode 
    At server captures image from webcam and writes over socket every o.1 sec,Now how to use waitForReadyRead() at client side to read this continuous data from server .I tried but it freezes the GUI,If not used client receives image only one time.

  13. #53
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Wireless Video

    I m able to get all images sent by server

    I added

    Qt Code:
    1. tcpSocket->waitForReadyRead(100) ;
    To copy to clipboard, switch view to plain text mode 
    in client's mainwindow constructer now the constructor looks like
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. tcpSocket = new QTcpSocket(this);
    7.  
    8. ViewObj = new view(this);
    9. ViewObj->show();
    10.  
    11. tcpSocket->waitForReadyRead(100) ;
    12.  
    13. connect(ui->BtnConnect, SIGNAL(clicked()), this, SLOT(Get_ServerPortIP()));
    14. connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));
    15. connect(tcpSocket, SIGNAL(readyRead()),SLOT(readDatafromServer()));
    16.  
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

    But now problem is

    server sends captured image after every 0.1 sec,but at my client app if I dont put msgbox before accepting any new data my GUI freezes ,I dont know why it needs the msgbox to be clicked/some time to rest before accepting new image?


    what could be alternative to this ?
    should I put sleep() function before accepting new image?

  14. #54
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Wireless Video

    You are blocking the event loop because you are using blocking calls. You should use asynchronous calls and signals/slots.

  15. #55
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Wireless Video

    Data is coming continously even if I dont use/comment tcpSocket->waitForReadyRead(100) ;
    But problem is I get all images continously if I put MsgBox before accepting new data

    i.e.
    Qt Code:
    1. void MainWindow::readDatafromServer()
    2. {
    3. qint64 nRead;
    4. QDataStream in(tcpSocket);
    5. in.setVersion(QDataStream::Qt_4_6);
    6. QByteArray data;
    7.  
    8. QMessageBox::critical(this,"WebClient","Received Data");
    9.  
    10. while (data.length() < 10)
    11. {
    12. .....
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    if I remove this QMessageBox ,nothing is displayed and my GUI is freezed,

    why it needs that msgbox /some time before accepting new data?

    what is asynchronous call and how to use it?

Similar Threads

  1. Playing video using Phonon video widget
    By Leolander in forum Newbie
    Replies: 0
    Last Post: 26th February 2010, 07:15
  2. Replies: 3
    Last Post: 5th July 2009, 18:22
  3. Video freezes during mpeg video playback using Phonon
    By davejames in forum Qt Programming
    Replies: 2
    Last Post: 12th January 2009, 09:45
  4. Video
    By mahiapkum in forum Qt Programming
    Replies: 3
    Last Post: 22nd April 2008, 12:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.