How can i print a QImage to a thermal printer
-
Here i have converted my QImage to QByteArray formal then it showing some error on printer write...anyone pls suggest me any correction or other ways to print image in thermal printer```
int MainWindow::on_pushbutton_graphprint_clicked() { int num = 230; char character = char(num); Printer *p = new Printer(); std::cout << "Trying to open port" << std::endl; bool res = p->open("/dev/ttyS0"); std::cout << "Status: " << res << std::endl; if (!res) { std::cerr << "Error opening port, aborting" << std::endl; return (0);} quint8 bytesWritten = p->write(arr); if (bytesWritten == -1) { qDebug() << "Error writing data to the printer."; p->close(); return 1; } else { qDebug() << "Successfully sent " << bytesWritten << " bytes to the printer."; } p->close(); return 0; \ } -
Here i have converted my QImage to QByteArray formal then it showing some error on printer write...anyone pls suggest me any correction or other ways to print image in thermal printer```
int MainWindow::on_pushbutton_graphprint_clicked() { int num = 230; char character = char(num); Printer *p = new Printer(); std::cout << "Trying to open port" << std::endl; bool res = p->open("/dev/ttyS0"); std::cout << "Status: " << res << std::endl; if (!res) { std::cerr << "Error opening port, aborting" << std::endl; return (0);} quint8 bytesWritten = p->write(arr); if (bytesWritten == -1) { qDebug() << "Error writing data to the printer."; p->close(); return 1; } else { qDebug() << "Successfully sent " << bytesWritten << " bytes to the printer."; } p->close(); return 0; \ }@Mohan-Raj said in How can i print a QImage to a thermal printer:
showing some error on printer write
What error?
Is your printer connected via serial to your machine or why do you use /dev/ttyS0 ? -
@Mohan-Raj said in How can i print a QImage to a thermal printer:
showing some error on printer write
What error?
Is your printer connected via serial to your machine or why do you use /dev/ttyS0 ? -
@jsulm hi!!! thanks for your reply.
yes, i have connected my printer with raspberry pi and main board communication is made via serial using uart.@Mohan-Raj You forgot to answer my first question.
Also, are you able to print anything on that printer via your OS? -
@Mohan-Raj You forgot to answer my first question.
Also, are you able to print anything on that printer via your OS? -
Here i have converted my QImage to QByteArray formal then it showing some error on printer write...anyone pls suggest me any correction or other ways to print image in thermal printer```
int MainWindow::on_pushbutton_graphprint_clicked() { int num = 230; char character = char(num); Printer *p = new Printer(); std::cout << "Trying to open port" << std::endl; bool res = p->open("/dev/ttyS0"); std::cout << "Status: " << res << std::endl; if (!res) { std::cerr << "Error opening port, aborting" << std::endl; return (0);} quint8 bytesWritten = p->write(arr); if (bytesWritten == -1) { qDebug() << "Error writing data to the printer."; p->close(); return 1; } else { qDebug() << "Successfully sent " << bytesWritten << " bytes to the printer."; } p->close(); return 0; \ }@Mohan-Raj said in How can i print a QImage to a thermal printer:
Printer *p = new Printer();
What is Printer?
You still did not post the exact error you get -why?
Try to save your QImage to a file and see whether you can open it in an image viewer.
You also did not say (or show any code) how you're converting the image to QByteArray.
If you want to get support you should provide enough information...Also, why do you convert the QImage to byte array? You can print a QImage like shown here: https://stackoverflow.com/questions/8310681/how-to-print-image-file-from-a-printer-using-qt
QImage img(fileName); QPainter painter(&printer); painter.drawImage(QPoint(0,0),img); painter.end(); -
@Mohan-Raj said in How can i print a QImage to a thermal printer:
Printer *p = new Printer();
What is Printer?
You still did not post the exact error you get -why?
Try to save your QImage to a file and see whether you can open it in an image viewer.
You also did not say (or show any code) how you're converting the image to QByteArray.
If you want to get support you should provide enough information...Also, why do you convert the QImage to byte array? You can print a QImage like shown here: https://stackoverflow.com/questions/8310681/how-to-print-image-file-from-a-printer-using-qt
QImage img(fileName); QPainter painter(&printer); painter.drawImage(QPoint(0,0),img); painter.end();@jsulm That printer is like a library used for thermal printer using that p object i'm using write, close like functions.
to print I can only send the image data as a Byte value only for serial communication and also i can print only monochrome format image in a thermal printer so i have converted it to bytearray values. so, i can't use any other like painter or webview operationsbelow i have added how i converted to bytearray...
QImage graphImage(ui->customPlot_4->size(), QImage::Format_Mono); ui->customPlot_4->render(&graphImage); QString filePath = "/home/pi/git/graph.png"; if (graphImage.save(filePath)) { qDebug() << "Image saved successfully."; } else { qDebug() << "Failed to save the image."; } arr = QByteArray::fromRawData((const char*)graphImage.bits(), graphImage.byteCount()); if(arr.isEmpty() || arr.isNull()) qDebug("empty"); else qDebug("converted to QByteArray"); -
@jsulm That printer is like a library used for thermal printer using that p object i'm using write, close like functions.
to print I can only send the image data as a Byte value only for serial communication and also i can print only monochrome format image in a thermal printer so i have converted it to bytearray values. so, i can't use any other like painter or webview operationsbelow i have added how i converted to bytearray...
QImage graphImage(ui->customPlot_4->size(), QImage::Format_Mono); ui->customPlot_4->render(&graphImage); QString filePath = "/home/pi/git/graph.png"; if (graphImage.save(filePath)) { qDebug() << "Image saved successfully."; } else { qDebug() << "Failed to save the image."; } arr = QByteArray::fromRawData((const char*)graphImage.bits(), graphImage.byteCount()); if(arr.isEmpty() || arr.isNull()) qDebug("empty"); else qDebug("converted to QByteArray");@Mohan-Raj Please read documentation (https://doc.qt.io/qt-6/qbytearray.html#fromRawData):
"The bytes are not copied. The QByteArray will contain the data pointer. The caller guarantees that data will not be deleted or modified as long as this QByteArray and any copies of it exist that have not been modified.".
Since your graphImage is a local variable it does not exist anymore when you're using the byte array. So, the image data in the byte array is invalid then."so, i can't use any other like painter or webview operations" - are you sure? This class seems to support QByteArray - is it a subclass of QPrinter or QPaintDevice?
-
@jsulm That printer is like a library used for thermal printer using that p object i'm using write, close like functions.
to print I can only send the image data as a Byte value only for serial communication and also i can print only monochrome format image in a thermal printer so i have converted it to bytearray values. so, i can't use any other like painter or webview operationsbelow i have added how i converted to bytearray...
QImage graphImage(ui->customPlot_4->size(), QImage::Format_Mono); ui->customPlot_4->render(&graphImage); QString filePath = "/home/pi/git/graph.png"; if (graphImage.save(filePath)) { qDebug() << "Image saved successfully."; } else { qDebug() << "Failed to save the image."; } arr = QByteArray::fromRawData((const char*)graphImage.bits(), graphImage.byteCount()); if(arr.isEmpty() || arr.isNull()) qDebug("empty"); else qDebug("converted to QByteArray");@Mohan-Raj Is there a reason for posting the same problem under two separate accounts? https://forum.qt.io/post/771275
Have you read the (surprisingly informative) manual? Have you sent the commands to put the printer into bit image mode, set bit density, image size, before sending the image data?
What arrangement of bits and scanlines does the printer expect? QImage::Format_Mono: "The image is stored using 1-bit per pixel. Bytes are packed with the most significant bit (MSB) first." The format described in the manual is not arranged the same way.
What version of Qt are you using? QImage::byteCount() was marked, "This function is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code."