简体   繁体   中英

How to convert a cv::mat header and data part into a single qbytearray?

I want to use Qftp module of Qt 4.8.1 to send opencv images to a FTP server. I wrote the following code:

        ftp = new QFtp(this);
        //ftp initilization
          .
          .
          .
          . 
        //
        cv::Mat InputImage(100,100,CV_8UC1,cv::Scalar::all(75));
        QImage FTP_Result = QImage((const uchar *) InputImage.data, InputImage.cols, InputImage.rows, InputImage.step, QImage::Format_Indexed8);
        QByteArray byteMe((char *) FTP_Result.bits());

        qDebug()<<"Image size is: "<<FTP_Result.height()<<"*"<<FTP_Result.width(); //it returns 100*100
        ftp->put(byteMe,"Sample.jpg");

When I go into the ftp server hard disk I find Sample.jpg , and it is filled with "K" character which is the Asci equivalent of "75". It seems the header is missing so the operating system thinks it is just a simple text file. I think this is because I only copied InputImage.data into the QByteArray variable so no information about the image is available. what should I do to copy "InputImage" header information into the "byteMe" variable so I could store a healthy JPEG file ?

Try

QByteArray byteMe((char *) FTP_Result.bits(), FTP_Results.byteCount());

When you construct with a simple char * its going to look for a null terminated string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM