当前位置: 代码迷 >> QT开发 >> qt传输图片没法显示
  详细解决方案

qt传输图片没法显示

热度:33   发布时间:2016-04-25 03:40:20.0
qt传输图片无法显示
使用UDP传输图片,但是接收之后无法正常显示,只显示一块黑色的图片
发送图片的代码:
  QByteArray buffer( 6+3*image->width(), 0 );
  QDataStream stream( &buffer, QIODevice::WriteOnly );
  stream.setVersion( QDataStream::Qt_4_6 );
  stream << (quint16)image->width() << (quint16)image->height();
  quint16 y = qrand() % image->height();
  stream << y;
  for( int x=0; x<image->width(); ++x )
  {
    QRgb rgb = image->pixel( x, y );
    stream << (quint8)qRed( rgb ) << (quint8)qGreen( rgb ) << (quint8)qBlue( rgb );
  }
  socket->writeDatagram( buffer.data(),buffer.size(), QHostAddress::Broadcast, 8888 );

接收图片的代码:
 while( socket->hasPendingDatagrams() )
  {
    QByteArray buffer( socket->pendingDatagramSize(), 0 );
    socket->readDatagram( buffer.data(), buffer.size() );
    QDataStream stream( buffer );
    stream.setVersion( QDataStream::Qt_4_6 );
    quint16 width, height, y;
    stream >> width >> height >> y;
    if( !image )
      image = new QImage( width, height, QImage::Format_RGB32 );
    else if( image->width() != width || image->height() != height )
    {
      delete image;
      image = new QImage( width, height, QImage::Format_RGB32 );
    }
    for( int x=0; x<width; ++x )
    {
      quint8 red, green, blue;
      stream >> red >> green >> blue;
     ui->textEdit->append("ddd");
      image->setPixel( x, y, qRgb( red, green, blue ) );
    }
  }

接收之后显示



找了好久没找出哪错了,请各位帮个忙呀
  相关解决方案