QImageReader::text() does not work properly - qt

I need to read image description (QImage::text()) without reading whole image. I used QImageReader, but its behaviour is very strange. It does not return text() or textKeys() but QImage does.
Example code:
QImageReader imageReader(filename,0);
QImage image;
bool ok=imageReader.read(&image);
qDebug() << "KEYS_READER: " << imageReader.textKeys() << "\n";
qDebug() << "KEYS_IMAGE: " << image.textKeys() << "\n";
When I try to load data from image which has one text key it prints in the debug window:
KEYS: ()
KEYS0: ("UVFI")
But I need to retrieve keys without reading the whole image data.

Related

QSharedMemory not the same size

QSharedMemory is changing sizes on me.
create:
...
int size = buffer->size();
qDebug() << "buffer->size()" << size << "points" << points->size() << "share name" << sharedMemoryName;
if (!m_sharedMemory->create(size)) {
qCritical() << tr("Unable to create shared memory segment.");
return;
}
m_sharedMemory->lock();
char *to = (char*)m_sharedMemory->data();
memcpy(to, buffer->data(), qMin(m_sharedMemory->size(), size));
m_sharedMemory->unlock();
read:
QSharedMemory sharedMemory(sharedMemoryName);
if (!sharedMemory.attach(QSharedMemory::AccessMode::ReadOnly)) {
qCritical() << "Unable to attach to shared memory segment.";
return nullptr;
}
qDebug() << sharedMemoryName << sharedMemory.size();
Not the same size.
when create the size is 658824
when read the size is 659456
ok - sounds crazy now, but I run the read multiple times and all the sudden the size was correct. Then I restarted everything (same size on create) and the error came back.
edit:
I just realized that the size of the QSharedMemory is not necessarily then same as the QBuffer
memcpy(to, buffer->data(), qMin(m_sharedMemory->size(), size));
why is that and how can I know "on the other side" the correct size (without making an ugly workaround)
edit 2:
I may found it. looks like QSharedMemory reserves the memory in 4096 blocks.
solution for me is to check the QByteArray for empty
if(in.at(i) == QChar(0))
break;

pleora sdk convert PvBuffer or PvRawData to QByteArray

I'm using the pleora sdk to capture images from an external camera and I am able to successfully write the data to tiff image files on disk. My next step is to change the data storage to SQLite instead of disk files.
I have PvBuffer *lBuffer pointer working fine. Now I need to convert that data to a format I can use to write to SQLite. I'm using Qt on linux so the QByteArray would be very convenient.
This is kind of a specific question for the pleora sdk and Qt. I'm hoping someone has experience with this.
PvRawData *rawData = lBuffer->GetRawData();
QByteArray ba;
//Need to copy the data from rawData to ba.
Thank you in advance.
I found an answer and wanted to post in case anybody else has something similar. I uses the reintepret_cast method.
data = lBuffer->GetDataPointer()
imgSize = lBuffer->GetPayloadSize();
const char *d = reinterpret_cast<char *>(data);
QByteArray ba(d, imgSize);
QSqlQuery q = QSqlQuery( db );
q.prepare("INSERT INTO imgData (image) values (:imageData)");
q.bindValue(":imageData", ba);
if ( !q.exec() )
qDebug() << "Error inserting image into table: " << q.lastError() << endl;
else
qDebug() << "Query executed properly" << endl;

Calling a method from a different .cpp file

I want to save some data as a text file, the first txt file will contain header information, the other text file will save data streamed from sensors, so with the help from the internet I created the following "datalogger.cpp" file
#include "datalogger.h"
#include <QDebug>
#include <iostream>
#include <QFile>
DataLogger::DataLogger(QObject *parent) : QObject(parent)
{
}
DataLogger::~DataLogger(){
}
void DataLogger::save(DataStream &input){
saveAsText(input);
}
void DataLogger::saveAsText(DataStream &input){
QTextStream outHeader(&outFileHeader);
outHeader << "[CAPTURE SETTINGS]\n"
<< "Filename: " << SettingsSingleton::instance().getFileName() << ".txt \n"
<< "Samples: " << QString::number(input.size()) << "\n"
<< "Duration: " << QString::number(input.back().time) << "ms \n"
<< "Sample rate: " << QString::number(SettingsSingleton::instance().getSampleRate()) << " Hz\n"
<< "Source: " << SettingsSingleton::instance().getSource() << "\n"
outFileHeader.close();
}
QFile outFile(SettingsSingleton::instance().getFileName() + ".txt");
QTextStream out(&outFile);
for (int i ; i<input.size();i++){
const EcgStreamObject tmp=input.at(i);
out << tmp.toText() << endl; //"\n";
}
outFile.close();
}
}
I have my "DataStream" input variable that I want to pass to the method and save as a ".txt" file, however I do no know how to call the method "void DataLogger::save(DataStream &input)" from a different ".cpp" file where the DataStream variable is located.
I am extremely new to c++ please it as simple as possible please.
Thank you in advance
If I get your question right, it is about how to create and use a header file in c++.
There is already a lot of information about this, for example this article from cplusplus.com or this from learncpp.com.
All you should need to do is add "datalogger.h" to your other file.
When you call the method DataLogger::save from your other file, the program will search for an implementation that was linked to your program (i.e. the file you posted here) and use that to actually perform the work.
Stefan's answer is probably important to look at too as it will cover this case and some other basics if you are going to be working in C/C++ long term.

DicomImage's writeBMP function produces unclear gray image

I am using DCMTK to read and hopefully modify DICOM images. I have the following code:
#include <iostream>
#include <opencv\cv.h>
#include <dcmtk\dcmimgle\dcmimage.h>
int main() {
try {
DicomImage* dicomImage = new DicomImage("C:/Users/Kriselle/Documents/000004.dcm");
if ((dicomImage != NULL) && (dicomImage->isMonochrome())) {
dicomImage->writeBMP("C:/Users/Kriselle/Documents/z.bmp", 8);
std::cout << "z.bmp is created" << std::endl;
}
else {
std::cout << "dicomImage is null or not monochrome" << std::endl;
}
}
catch (cv::Exception e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
All I did was create a DicomImage and write its pixel data to a BMP file with the filename I specified but the image returns only a gray image with the outline of the original image barely recognized.
This is what it should look like: https://www.dropbox.com/s/6dw8nlae8hfvqf6/000004.jpg?dl=0
This is what the code produces: https://www.dropbox.com/s/fff2kch124bzjqy/z.bmp?dl=0
Am I missing something in the code or did I not understand what the function does? Can anyone please enlighten me? Thank you very much!
As you can read in the API documentation of the DicomImage class, the default is that no VOI transformation is enabled when rendering monochrome DICOM images. In your case, this seems to be inappropriate, so you should specify a more appropriate VOI setting (e.g. a min-max window) or use one of the VOI windows stored in the DICOM dataset (if any).
By the way, after loading the image in the constructor, you should also check the status of this process using the getStatus() method.

Mouse click event in qt 3.0.3

Mouse button clicki am trying to create an automatic mouse click event at a particular co ordinate.
This source code moves the mouse pointer to the co ordinate region but it is not clicking.
please help me to solve this problem or suggest any new idea to automate mouse click event.
Note: i am using QT 3.0.3
void mMouseClickFunction()
{
QWidget *d = QApplication::desktop()->screen();
int w=d->width(); // returns desktop width
int h=d->height();
printf("w=%d\nh=%d\n",w,h);
int x,y;
printf("Enter the points...\n");
scanf("%d%d",&x,&y);
QApplication::desktop()->cursor().setPos(x,y);
QPoint pt(x,y);
std::cout << pt.x() << " " << pt.y() << std::endl;
QMouseEvent *e = new QMouseEvent(QEvent::MouseButtonPress, pt,Qt::LeftButton, 0);
QApplication::sendEvent(d, e);
std::cout << "in contentsMousePressEvent"<< e->x() << " " << e->y() << std::endl;
QMouseEvent *p = new QMouseEvent(QEvent::MouseButtonRelease, pt,Qt::LeftButton, 0);
QApplication::sendEvent(d, p);
std::cout << "in contentsMouseReleaseEvent"<< p->x() << " " << p->y() << std::endl;
}
QApplication::sendEvent sends an event internal to the QApplication, not a system wide event. You probably need to be system specific to send out an event like that. Here is the function call for windows:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx
But even with that kind of call, you will be limited to certain windows, unless you have UIAccess to be true, and your program is a signed application located in the right part of the harddrive.
EDIT: Here is a page that has some examples for sending input in Linux:
http://www.linuxquestions.org/questions/programming-9/simulating-a-mouse-click-594576/
Hope that helps.

Resources