Results 1 to 19 of 19

Thread: Receiving raw image data through TCP for display using Qt

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Receiving raw image data through TCP for display using Qt

    Quote Originally Posted by benz6699 View Post
    However the image I get is not totally black (It should be all black as I am not providing any light to the sensor). There are always some pixels with different color at the right bottom of the image (Refer attachment "temp1.png"). Can it be the noise?
    Have you checked the return value of readRawData()? Is it the number of bytes you expect?

    Cheers,
    _

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Receiving raw image data through TCP for display using Qt

    You cannnot expect to read all the image data from the socket in one go. As already said in this thread, you must keep track of how much data you have received. My previous post outlines a solution to your problem that does not assume any single piece of data (including the 32-bit integers) to be read in a single execution of the slot connected to readyRead().

  3. #3
    Join Date
    Jun 2014
    Posts
    16
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Receiving raw image data through TCP for display using Qt

    Quote Originally Posted by anda_skoa View Post
    Have you checked the return value of readRawData()? Is it the number of bytes you expect?

    Cheers,
    _
    Yes, I have checked the return value of readRawData(). It is the number of bytes I expect. For this testing I have set the camera so that it deliver only 8 x 10 pixel image. And all the values in the buffer are zero from buffer[0] to buffer[79]. So I should get a total black image right? However I always get an image with pixels with other colour at the bottom right corner. I cannot identify where is my mistake here. Please advice.

  4. #4
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Receiving raw image data through TCP for display using Qt

    What value does QImage::byteCount() return? Scanlines must be 32-bit aligned, which generally introduces some padding; though there shouldn't be any if your image is 8 pixels wide and 8 bits deep.

    By the way, you do not need to go through a temporary buffer. Just read into QImage::bits() directly.

  5. #5
    Join Date
    Jun 2014
    Posts
    16
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Receiving raw image data through TCP for display using Qt

    Quote Originally Posted by yeye_olive View Post
    What value does QImage::byteCount() return? Scanlines must be 32-bit aligned, which generally introduces some padding; though there shouldn't be any if your image is 8 pixels wide and 8 bits deep.

    By the way, you do not need to go through a temporary buffer. Just read into QImage::bits() directly.
    QImage::byteCount() return the value I expect which is 80 (I "ask" the sensor to send me 8 x 10 x1 byte image). So what else can I check more?

    Can you show me the code of how to read data directly into QImage::bits()? Thanks.

  6. #6
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Receiving raw image data through TCP for display using Qt

    Quote Originally Posted by benz6699 View Post
    QImage::byteCount() return the value I expect which is 80 (I "ask" the sensor to send me 8 x 10 x1 byte image). So what else can I check more?
    Could you post a minimal complete example reproducing the problem?
    Quote Originally Posted by benz6699 View Post
    Can you show me the code of how to read data directly into QImage::bits()? Thanks.
    Sure, something like: result = my_qiodevice_ptr->read(my_qimage.bits() + number_of_bytes_already_read, my_qimage.byteCount() - number_of_bytes_already_read);

  7. #7
    Join Date
    Jun 2014
    Posts
    16
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Receiving raw image data through TCP for display using Qt

    Quote Originally Posted by yeye_olive View Post
    Could you post a minimal complete example reproducing the problem?
    Qt Code:
    1. #include "client.h"
    2. #include <QHostAddress>
    3. #include "mainwindow.h"
    4. #include <QtGui>
    5. #include <QAbstractSocket>
    6. #include <QImage>
    7.  
    8. Client::Client(QWidget *parent) :
    9. QWidget(parent)
    10. {
    11. socket = new QTcpSocket(this);
    12. connect(socket, SIGNAL(readyRead()),this, SLOT(tcpReady()) );
    13. }
    14.  
    15. int Client::capture(int mode, int NBRLine)
    16. {
    17. if (socket->state() != QTcpSocket::ConnectedState)
    18. {
    19. socket->connectToHost("192.168.0.65", 1096);
    20. }
    21. /* send command to retrieve raw image data from sensor */
    22. if(socket->waitForConnected(5000))
    23. {
    24. QByteArray block;
    25. QDataStream out(&block, QIODevice::WriteOnly);
    26. out.setVersion(QDataStream::Qt_4_0);
    27. out.setByteOrder(QDataStream::LittleEndian);
    28. //out << qint32(0) << qint32(0) << qint32(0) << qint32(1);
    29. out << qint32(9) << qint32(1) << qint32(0) << mode << qint32(10) << qint32(2) << qint32(0) << NBRLine ;
    30. socket->write(block);
    31. socket->flush();
    32. }
    33. else
    34. {
    35. return false;
    36. }
    37. }
    38.  
    39.  
    40. int Client::tcpReady()
    41. {
    42. QDataStream input(socket);
    43. input.setVersion(QDataStream::Qt_4_0);
    44. input.setByteOrder(QDataStream::LittleEndian);
    45. qint32 cmdID, counter, metaSize, CmdNbr, min, max, value, error, width, height;
    46. QByteArray buffer;
    47.  
    48. int sizereceived = 0;
    49. int sizeAvailable = 0;
    50.  
    51. forever{
    52. if (socket->bytesAvailable() < sizeof(qint32))
    53. {
    54. return 0;
    55. }
    56. input >> cmdID;
    57. // We get the data response with cmdID = 100 when we send a command to get parameter value
    58. if (cmdID == 100)
    59. {
    60. input >> counter >> metaSize >> CmdNbr >> min >> max >> value;
    61. }
    62. // We get the data response with cmdID = 101 when we send a command to set parameter value
    63. else if (cmdID == 101)
    64. {
    65. input >> counter >> metaSize >> CmdNbr >> error;
    66. }
    67. // Here we get a streaming raw image data
    68. else if (cmdID == 1)
    69. {
    70. break;
    71. }
    72. else
    73. {
    74. return 0;
    75. }
    76. }
    77.  
    78. // Here we read the data and construct image
    79. input >> counter >> metaSize >> width >> height;
    80. int pixel = width * height;
    81. char *temp = new char[pixel];
    82. //char *temp = 0;
    83. int read = input.readRawData(temp, pixel);
    84. buffer.append(temp, pixel);
    85. QImage image = QImage(width, height, QImage::Format_Indexed8);
    86. delete [] temp;
    87.  
    88. int m_index = 0;
    89. for (int i = 0; i < (pixel-1); ++i, ++m_index)
    90. {
    91. image.bits()[m_index] = buffer[i];
    92. }
    93. int cnt = image.byteCount();
    94. bool gray = image.isGrayscale();
    95.  
    96. if( !image.isNull() )
    97. {
    98. int result = image.save("E:/temp1.png", "PNG",1);
    99. }
    100. return 1;
    101. }
    To copy to clipboard, switch view to plain text mode 


    Sure, something like: result = my_qiodevice_ptr->read(my_qimage.bits() + number_of_bytes_already_read, my_qimage.byteCount() - number_of_bytes_already_read);
    I tried the following code, but it give me the error message" :-1: error: C2664: 'qint64 QIODevice::read(char *,qint64)' : cannot convert parameter 1 from 'uchar *' to 'char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast."

    Qt Code:
    1. input >> counter >> metaSize >> width >> height;
    2. int pixel = width * height;
    3.  
    4. QImage image = QImage(width, height, QImage::Format_Indexed8);
    5. socket->read(image.bits(), pixel);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Receiving Image through serial port in QT
    By jakr13 in forum Qt Programming
    Replies: 3
    Last Post: 4th February 2013, 05:14
  2. Display my data array as an image
    By OzQTNoob in forum Newbie
    Replies: 5
    Last Post: 4th December 2012, 09:17
  3. Replies: 0
    Last Post: 26th July 2012, 09:32
  4. How to Display/Paint raw image data.
    By vinaykusta in forum Qt Programming
    Replies: 5
    Last Post: 12th October 2011, 14:01
  5. Replies: 2
    Last Post: 29th September 2008, 00:08

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
  •  
Qt is a trademark of The Qt Company.