PDA

View Full Version : URGENT HELP Code128 barcode printing



georgeky
25th July 2011, 14:28
I am having a major issue printing a readable barcode from QT on an epson M-T500 printer thermal receipt printer.

i am using the font from http://www.dafont.com/code-128.font

It prints th ebarcode but somehow its not printing the correct order

I even tried to hardcode the char with ascii code still does not work.

Any help will be much appreciated.

The Barcode text: MHB-6TG-6TG-GGHJ

My sample code:




QPrinter printer;
QPainter painter;
painter.begin(&printer);
double scaleFactor;
scaleFactor = 1.0;
QFont barcodefont = QFont("Code128", 32, QFont::Normal);
barcodefont.setLetterSpacing(QFont::AbsoluteSpacin g,1.0);
painter.setFont(barcodefont);
char mytext[20];

mytext[0]= 204;
mytext[1]=77;
mytext[2]=72;
mytext[3]=66;
mytext[4]=45;
mytext[5]=54;
mytext[6]=84;
mytext[7]=71;
mytext[8]=45;
mytext[9]=54;
mytext[0]=84;
mytext[11]=71;
mytext[12]=45;
mytext[13]=71;
mytext[14]=71;
mytext[15]=72;
mytext[16]=74;
mytext[17]=104;
mytext[18]=206;
mytext[19]=0;
qDebug() << mytext;
painter.drawText(-30, 370,mytext);
painter.end();

ChrisW67
25th July 2011, 23:32
I assume the '\xCC' and '\xCE' characters are a start and end marker. Your array index 10 is mistyped, and I have converted it to a QByteArray. This code:


#include <QtGui>
#include <QDebug>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

// QPrinter printer;
// printer.setOutputFormat(QPrinter::PdfFormat);
// printer.setOutputFileName("test.pdf");

QImage image(640, 100, QImage::Format_Mono);
image.fill(1);

QPainter painter;
painter.begin(&image);

QFont barcodefont = QFont("code128", 32, QFont::Normal);
barcodefont.setLetterSpacing(QFont::AbsoluteSpacin g,10.0); //deliberately spread out
painter.setFont(barcodefont);

QByteArray mytext("\xccMHB-6TG-6TG-GGHJ\xce");
qDebug() << mytext;
painter.drawText(10, 90, mytext);
painter.end();

image.save("test.png");

return app.exec();
}

produces this output (this is the PNG, but the PDF looks the same):
6716

The characters look good here.

georgeky
26th July 2011, 09:24
Thank You soo much for your help :-) Worked like a charm