This is what I have so far:
QFile file(fileName);
file.open(QIODevice::WriteOnly);
QPixmap pixmap = QPixmap.loadFromData((const uchar *) imageBuffer_pointer, (sizeof(imageRows) * sizeof(imageCols));
pixmap.save(&file, "JPG");
pixmap.save(&file, "PNG");
But it only produces 0 byte image files
It is much easier to just give the save() function a file name as a first parameter:
QPixmap pixmap = QPixmap.loadFromData((const uchar *) imageBuffer_pointer,
(sizeof(imageRows) * sizeof(imageCols));
pixmap.save(fileName, "JPG");
Related
I'm creating a socket-based program to send a screenshot from one user to another user. I need to convert a screenshot to a byte array before sending. After I convert my screenshot to a QByteArray I insert 4 bytes to the beginning of the array to mark that it is a picture (it is the number 20 to tell me it is a picture and not text or something else).
After I send the byte array via a socket to other user, when it is received I read the first 4 bytes to know what it is. Since it was a picture I then convert it from a QByteArray to QPixmap to show it on a label. I use secondPixmap.loadFromData(byteArray,"JPEG") to load it but it not load any picture.
This is a sample of my code:
void MainWindow::shootScreen()
{
originalPixmap = QPixmap(); // clear image for low memory situations
// on embedded devices.
originalPixmap = QGuiApplication::primaryScreen()->grabWindow(0);
scaledPixmap = originalPixmap.scaled(500, 500);
QByteArray bArray;
QBuffer buffer(&bArray);
buffer.open(QIODevice::WriteOnly);
originalPixmap.save(&buffer,"JPEG",5);
qDebug() << bArray.size() << "diz0";
byteArray= QByteArray();
QDataStream ds(&byteArray,QIODevice::ReadWrite);
int32_t c = 20;
ds << c;
ds<<bArray;
}
void MainWindow::updateScreenshotLabel()
{
this->ui->label->setPixmap(secondPixmap.scaled(this->ui->label->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
}
void MainWindow::on_pushButton_clicked()
{
shootScreen();
}
void MainWindow::on_pushButton_2_clicked()
{
secondPixmap = QPixmap();
QDataStream ds(&byteArray,QIODevice::ReadOnly);
qint32 code;
ds>>code;
secondPixmap.loadFromData(byteArray,"JPEG");
updateScreenshotLabel();
}
Your MainWindow::on_pushButton_2_clicked implementation looks odd. You have...
QDataStream ds(&byteArray,QIODevice::ReadOnly);
which creates a read-only QDataStream that will read it's input data from byteArray. But later you have...
secondPixmap.loadFromData(byteArray,"JPEG");
which attempts to read the QPixmap directly from the same QByteArray -- bypassing the QDataStream completely.
You can also make use of the QPixmap static members that read from/write to a QDataStream. So I think you're looking for something like...
QDataStream ds(&byteArray,QIODevice::ReadOnly);
qint32 code;
ds >> code;
if (code == 20)
ds >> secondPixmap;
And likewise for your MainWindow::shootScreen implementation. You could reduce your code a fair bit by making use of QDataStream & operator<<(QDataStream &stream, const QPixmap &pixmap).
Is any ideas how i can transmit QMap<QString, QString> in Drag and Drop mode by using QMimeData?
Now i convert QMap into QString like this:
"key1:value1;key2:value2;...keyN:valueN" and assigned it to QMimeData::setText().
Then on dropEvent() i rebuild QMap from QString. Is this right way?
Convert QString to QMap
...
QStringList splittedParams = params.split(";");
QMap<QString, QString> *map = new QMap<QString, QString>();
foreach(QString param, splittedParams)
{
if(param.isEmpty()) continue;
QStringList str = param.split(":");
map->insert(str[0], str[1]);
}
...
That's going to fall apart if your strings contain the separators. For a more robust approach use something like
QByteArray ba;
QDataStream ds(&ba, QIODevice::WriteOnly);
map >> ds;
mimeData->setData(QStringLiteral("your/mime/type"), ba);
I receive a byte array which contains PNG file from network. I need to set this to a pixmap and set it as a texture to my QGlWidget. As I run the program below, pixmap is in debug mode and does not contain anything. However, bytes contains the whole byte array received from network.
void MainWindow::dataFromServer(QByteArray bytes)
{
// QByteArray bytes;
QBuffer buffer(&bytes);
QPixmap pixmap;
// pixmap = QPixmap::grabWidget(this);
buffer.open(QIODevice::WriteOnly);
pixmap.save(&buffer, "PNG"); // writes pixmap into bytes in PNG format
emit sendPixmapToWidget(pixmap);
}
and here I set the pixmap to texture:
void GlWidget::pixmapCatchFromForm(QPixmap pixmap)
{
deleteTexture(texture);
// image->loadFromData(bytes, "PNG");
texture = bindTexture(pixmap);
qDebug() << texture; // returns 1
updateGL();
}
QPixmap::save(..) saves the QPixmap's contents to the buffer, surely you want to use QPixmap::loadFromData(..) to do the opposite?
is there any way to store exact cv::Mat format data in sqlite3 using Qt.. as i will be using the same cv::Mat format in future..
i tried converting image to unsigned char* and them storing it.. but this didn't worked for me.. any other technique ??
You can serialize cv::Mat to QByteArray (see kde/libkface):
QByteArray mat2ByteArray(const cv::Mat &image)
{
QByteArray byteArray;
QDataStream stream( &byteArray, QIODevice::WriteOnly );
stream << image.type();
stream << image.rows;
stream << image.cols;
const size_t data_size = image.cols * image.rows * image.elemSize();
QByteArray data = QByteArray::fromRawData( (const char*)image.ptr(), data_size );
stream << data;
return byteArray;
}
Then store to DB.
To convert from QByteArray after reading from DB:
cv::Mat byteArray2Mat(const QByteArray & byteArray)
{
QDataStream stream(byteArray);
int matType, rows, cols;
QByteArray data;
stream >> matType;
stream >> rows;
stream >> cols;
stream >> data;
cv::Mat mat( rows, cols, matType, (void*)data.data() );
return mat.clone();
}
It works for me.
I would like to make the buffer of cv::mat point to the buffer of QImage but not copy
the data of QImage into the cv::mat.
cv::Mat const reference_qimage_to_mat(QImage const &img, int format)
{
cv::Mat mat(img.height(), img.width(), format);
for(int i = 0; i != mat.rows; ++i)
{
//pseudo code, wouldn't work
//mat.ptr(i) = img.scanLine(i);
}
return mat;
}
I try to search the answer by google but I could only find how to copy
the data of QImage into cv::mat.Thanks
the cv::Mat object is simply a header for the image data, so you can do it upon your object construction:
cv::Mat mat(img.height(), img.width(), type, img.bits());
where type depends on your data, CV_8UC1 for single channel, CV_8UC3 for RGB, etc.