PDA

View Full Version : How to take screenshots and save the binary data in the blob field?



Gokulnathvc
29th June 2011, 06:14
How to take screenshots and save the binary data in the blob field of the MYSQL Database??
How to connect to the MYSQL database ,tables and save the long binary data into the blob field of the database??

ChrisW67
29th June 2011, 07:47
How to take screenshots
Back here you claim to already be doing this... guess not. What have you tried? Where else have you looked for help? Have you looked in the Qt Docs? Have you looked at open source projects (http://sourceforge.net/projects/lightscreen/) that do this sort of thing?

and save the binary data in the blob field of the MYSQL Database??
How to connect to the MYSQL database ,tables and save the long binary data into the blob field of the database??
Which parts or parts of the answers to the other times you have asked related questions are you having difficulty with? What have you tried?

Gokulnathvc
29th June 2011, 08:54
I have tried the following codes:


QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");


db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("DRIVER={MYSQL ODBC 3.51 Driver};FIL={MYSQL};DBQ=screengrabber");
db.setHostName("localhost");
db.setConnectOptions("CLIENT_ODBC");
//db.setDatabaseName("screengrabber");
db.setUserName("root");
db.setPassword("1");
ok = db.open();

for(;;)
{

originalPixmap =QPixmap(); // clear image for low memory situations
// on embedded devices.
originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());

QString strfname;
strfname.sprintf("%d",c);
originalPixmap.save("D:\\image"+strfname+".jpeg","jpeg");
c++;
char Data;
QFile file("D:\\image"+strfname+".jpeg");
file.open(QIODevice::ReadOnly);
file.seek(0);
QByteArray buf;
buf=file.read(250000);

QSqlQuery query;
query.prepare("INSERT INTO log VALUES('grab_date='2011-04-26 15:55:09',ip_address='172.16.0.51',image=:val');");
query.bindValue ( ":val", buf);

QString strquery;

strquery = "INSERT INTO log (grab_date,ip_address,image) VALUES ( '";

strquery += '2011-04-26 15:55:09';
strquery += "' , '";
strquery += '172.16.0.51';
strquery += "' , ";
strquery += buf;
strquery += " );";

//QMessageBox::warning(NULL,"DriveList",strquery,QMessageBox::Ok);

QSqlQuery insertQuery( strquery, db);
bool done = insertQuery.exec();}

ChrisW67
29th June 2011, 09:29
Lines 1-10: you should be using the native MySQL driver, so I guess you didn't manage to build that. This will do but isn't ideal.
Lines 16-17: Why construct a null pixmap only to replace it immediately on the next line?
Line 20-29: If you don't want the data in a file on disk why save it to a file?
Line 21:

Warning: We do not recommend using QString::sprintf() in new Qt code. Instead, consider using QTextStream or arg(), both of which support Unicode strings seamlessly and are type-safe. Here's an example that uses QTextStream: ...
Line 26: Are you sure the file opened successfully? You don't bother to check.
Line 27: Unnecessary as this is the default position in a newly opened file.
Line 29: Fails to give you the whole file if the file is large than 250000 bytes.

Line 32: This is not valid SQL as was pointed out the last time you asked. You said you tried the corrected version provided... I guess you didn't bother. Get into the habit of checking return codes. QSqlQuery::prepare() returns false on failure. When it does so QSqlQuery::lastError() is useful. Get into the habit of using the error message the system provides.
Line 34: I guess you didn't want to execute the query you prepared at 32 :( Shame really because it is close to working.

Lines 37-44: Building queries like this is clumsy, error prone, dangerous and, in the case of binary data, not useful. Go back to line 32.

Line 49: Have you checked "done"?

Gokulnathvc
29th June 2011, 10:44
I am getting in:

line :11 'ok' as true;

line 26: file is opened successfully.

line 27: removed

line 29: size will be less than that.

line 32: I dont know about this query, i didnt use this also. So i need more guidance towards this issue.

line 49: 'done' is false.

ChrisW67
29th June 2011, 11:41
Line 26: Good, but irrelevant because you don't want the data in a file anyway. How else can you QPixmap::save()? What is a QIODevice and how might you use a QBuffer?

Line 32: read the thread I linked to. The answer is there. You cannot use an SQL database without learning some (very basic) SQL.

Line 49: Of course it is. The SQL is invalid and there is no way to fix it with arbitrary binary data. That is one of the reasons you use the bind variable approach from line 32.

Gokulnathvc
29th June 2011, 14:40
How to get the binary data from the QPixmap with out creating the file in the disk??

stampede
29th June 2011, 15:41
It's in docs (QPixmap::save).

ChrisW67
29th June 2011, 22:50
How to get the binary data from the QPixmap with out creating the file in the disk??

Not only is it in the docs, but I provided you with a link to every page of the docs that you should need to capture this data without using a file on disk. What have you tried?

wysota
30th June 2011, 16:56
Lines 16-17: Why construct a null pixmap only to replace it immediately on the next line?
I think it might make sense. If you are low on memory and you execute this method more than once then upon entering into the method a large pixmap might still be using a significant amount of memory. Creating a new one and assigning it to the same variable first creates the pixmap (which requires resources) and only then assigns it to the original variable which clears the old pixmap. So at some point in time you have two pixmaps and can run out of memory.

Which doesn't change a fact that saving a file just to read it back is kind of a funny approach :) Even if no API existed to save it to memory, it would still be funny :)

ChrisW67
30th June 2011, 22:47
Fair call. I misread it as creating the QPixmap object inside the loop, which would ensure it was destroyed at the bottom of each trip through the for() loop.