PDA

View Full Version : Interface printer with QT Application



Ratheendrans
1st August 2011, 15:24
Dear All,

I want to print the form image on to thermal printer interfaced to its serial port.

I am able to save the image on to a file.



QPixmap pixmap(this->size());
this->render(&pixmap);
pixmap.save("myfile.pgm", "PGM");


The thermal printer interfaced is capable to print the image with the following command from serial port.

ESC # print Logo.
ESC # nW nH Data String
i.e in hex format
<1Bh> <23h> <n1h> <n2h> <Hex String>

[Description]
Prints Logo/Image in the predefined area
Spacing: n x 0.125mm (1 dot size)
[nW x nH]: Print Area Size (Width x Height) (this will also equal to total number of bytes.
nW = Number of Bytes to be Printer per line
Max: 48 bytes
nH = Total Bytes / Number of Bytes Per line
Max: 255 bytes

can any one let me know the image format which I can use so that I execute the print command directly.

Or I have use a library to convert the standard file format to a Hex data.

Thanks in Advance.
ratheendran

MarekR22
1st August 2011, 15:48
Why doesn't you use QPrinter (http://doc.qt.nokia.com/latest/qprinter.html) and QPrintDialog (http://doc.qt.nokia.com/latest/qprintdialog.html)?

Ratheendrans
1st August 2011, 16:43
Thanks for your response.

My application is for embedded linux and the interface printer needs image data written to serial port. so my Idea is to take the image of the form and write its hex data to serial port

I am not sure how ' QPrinter and QPrintDialog' is use-full. can you please eloborate on how this can be used in my application.

stampede
1st August 2011, 17:46
Print Area Size (Width x Height) (this will also equal to total number of bytes.
In order to get pixel data, you need to convert QPixmap to QImage (http://doc.qt.nokia.com/latest/qimage.html), the quoted text indicates that you are forced to use 8 bits per pixel, this implies QImage::Format_Indexed8 (http://doc.qt.nokia.com/latest/qimage.html#Format-enum) format (I dont see any other 8-bit formats). You can get access to pixel data using QImage::constBits (http://doc.qt.nokia.com/latest/qimage.html#constBits) method.

MarekR22
2nd August 2011, 09:53
I would say this printer is black and withe (no gray scale). He quote documentation "nW = Number of Bytes to be Printer per line Max: 48 bytes" so I assume one byte is 8 pixels.
probably this is a printer like in cash register (48 pixels per line is to small value to present any useful information).
I agree that he should do conversion to QImage but using format QImage::Format_Mono (http://doc.qt.nokia.com/latest/qimage.html#Format_Mono).
So code would look more or less like this:

Q_ASSSERT(pixmap.depth()==1);
const QImage image = pixmap.toImage();
QByteArray buffer;
buffer.append('\x1B');
buffer.append('\x23');

int lineSize = (image.width()+7)/8;
buffer.append((char)lineSize);
buffer.append((char)image.hieht());
for(int i=0; i<image.hieht(); ++i)
buffer.append(image.scanLine(i), lineSize);

QFile file("com1:"); // serial port name on device on embedded system (here used Windows CE)
if (!file.open(QIODevice::WriteOnly))
return;
file.write(buffer);
file.waitForBytesWritten(3000);// wait max 3 seconds
file.close();

stampede
2nd August 2011, 10:25
48 pixels per line is to small value to present any useful information
I don't agree with this one, but probably you are right with the image format.