I'm reimplemented the present method from a QAbstractVideo Surface in order to capture frames from an IP camera.
This is my reimplemented methods (the required ones):
QList<QVideoFrame::PixelFormat> CameraFrameGrabber::supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType) const
{
Q_UNUSED(handleType);
return QList<QVideoFrame::PixelFormat>()
<< QVideoFrame::Format_ARGB32
<< QVideoFrame::Format_ARGB32_Premultiplied
<< QVideoFrame::Format_RGB32
<< QVideoFrame::Format_RGB24
<< QVideoFrame::Format_RGB565
<< QVideoFrame::Format_RGB555
<< QVideoFrame::Format_ARGB8565_Premultiplied
<< QVideoFrame::Format_BGRA32
<< QVideoFrame::Format_BGRA32_Premultiplied
<< QVideoFrame::Format_BGR32
<< QVideoFrame::Format_BGR24
<< QVideoFrame::Format_BGR565
<< QVideoFrame::Format_BGR555
<< QVideoFrame::Format_BGRA5658_Premultiplied
<< QVideoFrame::Format_AYUV444
<< QVideoFrame::Format_AYUV444_Premultiplied
<< QVideoFrame::Format_YUV444
<< QVideoFrame::Format_YUV420P
<< QVideoFrame::Format_YV12
<< QVideoFrame::Format_UYVY
<< QVideoFrame::Format_YUYV
<< QVideoFrame::Format_NV12
<< QVideoFrame::Format_NV21
<< QVideoFrame::Format_IMC1
<< QVideoFrame::Format_IMC2
<< QVideoFrame::Format_IMC3
<< QVideoFrame::Format_IMC4
<< QVideoFrame::Format_Y8
<< QVideoFrame::Format_Y16
<< QVideoFrame::Format_Jpeg
<< QVideoFrame::Format_CameraRaw
<< QVideoFrame::Format_AdobeDng;
}
bool CameraFrameGrabber::present(const QVideoFrame &frame)
{
//qWarning() << "A frame";
if (frame.isValid()) {
//qWarning() << "Valid Frame";
QVideoFrame cloneFrame(frame);
cloneFrame.map(QAbstractVideoBuffer::ReadOnly);
const QImage image(cloneFrame.bits(),
cloneFrame.width(),
cloneFrame.height(),
QVideoFrame::imageFormatFromPixelFormat(cloneFrame .pixelFormat()));
qWarning() << "Is created image NULL?" << image.isNull();
if (!image.isNull())
emit nextFrameAsImage(image);
cloneFrame.unmap();
return true;
}
return false;
}
And this is is how I used it:
grabber = new CameraFrameGrabber(this);
connect(grabber,&CameraFrameGrabber::nextFrameAsImage,this,&QCmaraTest::on_newFrame);
QMediaPlayer *a = new QMediaPlayer(this);
QString url = "http://Admin:1234#10.255.255.67:8008";
a->setMedia(QUrl(url));
a->setVideoOutput(grabber);
a->play();
The problem is that the image that is created is null. As far as I can tell, this can only be because the frame is valid but does not contain data.
Any ideas what the problem could be?
Important Detail: If I set the stream to a QVideoWidget and simply show that, it works just fine.
So I found out what the problem was.
This:
QVideoFrame::imageFormatFromPixelFormat(cloneFrame .pixelFormat())
Was returning invalid format because the IP cam gave the format as a YUV format which QImage can't handle. The solution was to force the format and the only one I found that did not make the program crash was: QImage::Format_Grayscale8.
With that, it worked.
Related
I got this code from a book. When I ran on Visual Studio, it said to switch strcpy() to strcpy_s(), and after I did that, it seems the program terminated at the delete pointer. I tried to run on Dev-C++, and it works fine. Anyone knows why? Thank you.
#include "pch.h"
#include <iostream>
#include <cstring>
int main()
{
cout << "Enter a kind of animal: ";
cin >> animal; // ok if input < 20 chars
ps = animal; // set ps to point to string
cout << ps << "!\n"; // ok, same as using animal
cout << "Before using strcpy():\n";
cout << animal << " at " << (int *)animal << endl;
cout << ps << " at " << (int *)ps << endl;
ps = new char[strlen(animal) + 1]; // get new storage
strcpy_s(ps, sizeof(animal), animal); // copy string to new storage
cout << "After using strcpy():\n";
cout << animal << " at " << (int *)animal << endl;
cout << ps << " at " << (int *)ps << endl;
delete[] ps;
return 0;
}
I'm using opencv in a Qt app. I've seen some generic c++ ways of printing out the values of a Mat and have done so with
cout << "myMat = "<< endl << " " << myMat << endl << endl;
Ideally I could have a QString with the contents of this Mat. Is there a neat way to do this?
You can use ostringstream and its method str() to get string which you can pass as parameter to QString constructor.
cv::Mat M(2,2, CV_8UC3, cv::Scalar(0,0,255));
ostringstream oss;
oss << "M = " << endl << " " << M << endl;
QString matContent(oss.str()); // QT3
QString matContent2(oss.str().c_str()); // QT4/5 (const char*) constructor
I have this function :
bool TCPClient::sendPacket(Protocol::Packet &packet)
{
if(this->tcpSocket->state() == QAbstractSocket::ConnectedState)
{
std::cout << "writing ..." << std::endl;
std::cout << sizeof(BabelPacket) + packet.dataLength << std::endl;
this->tcpSocket->write((const char *)&packet, sizeof(Protocol::BabelPacket) + packet.dataLength);
return this->tcpSocket->waitForBytesWritten(-1);
}
else
{
std::cout << "socket close" << std::endl;
return false;
}
}
But when I called it, it blocks my GUI.
Have you got an idea how I can make this call non blocking, and still have the readyRead signal triggered ?
Thanks
I want to use QSettings to save my window's dimensions so I came up with these two functions to save & load the settings:
void MainWindow::loadSettings()
{
settings = new QSettings("Nothing","KTerminal");
int MainWidth = settings->value("MainWidth").toInt();
int MainHeight = settings->value("MainHeight").toInt();
std::cout << "loadSettings " << MainWidth << "x" << MainHeight << std::endl;
std::cout << "file: " << settings->fileName().toLatin1().data() << std::endl;
if (MainWidth && MainHeight)
this->resize(MainWidth,MainHeight);
else
this->resize(1300, 840);
}
void MainWindow::saveSettings()
{
int MainHeight = this->size().height();
int MainWidth = this->size().width();
std::cout << "file: " << settings->fileName().toLatin1().data() << std::endl;
std::cout << "saveSettings " << MainWidth << "x" << MainHeight << std::endl;
settings->setValue("MainHeight",MainHeight);
settings->setValue("MainWidth",MainWidth);
}
Now, I can see the demensions being extracted in saveSettings as expected but no file gets created and hence loadSettings will always load 0 only. Why is this?
QSettings isn't normally instantiated on the heap. To achieve the desired effect that you are looking for, follow the Application Example and how it is shown in the QSettings documentation.
void MainWindow::readSettings()
{
QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
const QByteArray geometry = settings.value("geometry", QByteArray()).toByteArray();
if (geometry.isEmpty()) {
const QRect availableGeometry = QApplication::desktop()->availableGeometry(this);
resize(availableGeometry.width() / 3, availableGeometry.height() / 2);
move((availableGeometry.width() - width()) / 2,
(availableGeometry.height() - height()) / 2);
} else {
restoreGeometry(geometry);
}
}
void MainWindow::writeSettings()
{
QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
settings.setValue("geometry", saveGeometry());
}
Also note the use of saveGeometry() and restoreGeometry(). Other similarly useful functions for QWidget based GUIs are saveState() and restoreState() (not shown in the above example).
I strongly recommend the zero parameter constructor of QSettings, and to setup the defaults in your main.cpp, like so:
QSettings::setDefaultFormat(QSettings::IniFormat); // personal preference
qApp->setOrganizationName("Moose Soft");
qApp->setApplicationName("Facturo-Pro");
Then when you want to use QSettings in any part of your application, you simply do:
QSettings settings;
settings.setValue("Category/name", value);
// or
QString name_str = settings.value("Category/name", default_value).toString();
QSettings in general is highly optimized, and works really well.
Hope that helps.
Some other places where I've talked up usage of QSettings:
Using QSettings in a global static class
https://stackoverflow.com/a/14365937/999943
I have two list of images to be downloaded from network. I use qnetworkaccessmanager to get image from url. But on the reply slot there is no image data with the reply. :( I am not able to figure out where am I going wrong.. If someone is able to figure out, it would be helpful :)
void SyncDialog::getImages()
{
qDebug() << Q_FUNC_INFO << "Invoked";
int groupMasterCount = mSyncMasterData.groupMasterList.count();
qDebug() << Q_FUNC_INFO << "groupmastercout" << groupMasterCount;
for (int i = 0 ; i < groupMasterCount; ++i)
{
GroupMaster groupItem = mSyncMasterData.groupMasterList.at(i);
QNetworkReply *reply =
mImageGetNwMgr.get(QNetworkRequest(QUrl(groupItem.image)));
reply->setProperty("name", QVariant("G_" + groupItem.groupCode));
connect(reply, SIGNAL(readyRead()), this, SLOT(saveImage()));
qDebug() << Q_FUNC_INFO << "get call reply" << reply->readAll();
qDebug() << Q_FUNC_INFO << "get url" << groupItem.image;
mSyncMasterData.groupMasterList[i].image.clear();
#ifdef Q_OS_WIN
mSyncMasterData.groupMasterList[i].image = "C:/POS/Images/G_"
+groupItem.groupCode;
#else
mSyncMasterData.groupMasterList[i].image = "/mnt/sdcard/POS/Images/G_"
+groupItem.groupCode;
#endif
}
int itemMasterCount = mSyncMasterData.itemMasterList.count();
qDebug() << Q_FUNC_INFO << "itemmastercout" << itemMasterCount;
for (int i = 0 ; i < itemMasterCount; ++i)
{
ItemMaster item = mSyncMasterData.itemMasterList.at(i);
QNetworkReply *reply =
mImageGetNwMgr.get(QNetworkRequest(QUrl(item.imagePath)));
reply->setProperty("name", QVariant("I_" + item.itemCode));
connect(reply, SIGNAL(readyRead()), this, SLOT(saveImage()));
qDebug() << Q_FUNC_INFO << "get call reply" << reply->readAll();
qDebug() << Q_FUNC_INFO << "get url" << item.imagePath;
mSyncMasterData.itemMasterList[i].imagePath.clear();
#ifdef Q_OS_WIN
mSyncMasterData.itemMasterList[i].imagePath = "C:/POS/Images/I_"
+item.itemCode;
#else
mSyncMasterData.itemMasterList[i].imagePath = "/mnt/sdcard/POS/Images/G_"
+item.itemCode;
#endif
}
qDebug() << Q_FUNC_INFO << "Exits";
}
In my slot I save images , but in reply->readAll gives me ""
void SyncDialog::saveImage()
{
qDebug() << Q_FUNC_INFO << "Invoked";
QObject *senderObj = sender();
QNetworkReply *reply = qobject_cast<QNetworkReply*>(senderObj);
QImage* img2 = new QImage();
img2->loadFromData(reply->readAll());
qDebug() << Q_FUNC_INFO << "image nw reply" << reply->readAll();
QString imageName = reply->property("name").toString();
qDebug() << Q_FUNC_INFO << "imageName" << imageName;
if(img2->isNull())
{
qDebug() << Q_FUNC_INFO << "image is null";
return;
}
#ifdef Q_OS_WIN
img2->save("C:/POS/Images/" + imageName);
#else
img2->save("/mnt/sdcard/POS/Images/" + imageName);
#endif
qDebug() << Q_FUNC_INFO << "Exits";
}
Also I see in logs "libpng error: Read Error" and the slot is invoked multiple times...
Thanks all,
I resolved the issue with few changes :)
connect(reply, SIGNAL(finished()), this, SLOT(saveImage()));
finished() makes sure complete image data is received from n/w.
QByteArray imageData = reply->readAll();
QImage *image = new QImage();
image->loadFromData(imageData);
reply->readAll() should be saved as first call clears the data after returning it.
image->save("C:/POS/Images/" + imageName + ".png"
don't forget to specify image format (either in filename or as parameter)