QT开发: 如何调用TSCLIB库打印本地图片?
我已经完成了以下代码:
void MainWindow::on_pushButton_clicked()
{
ui->pushButton->setText("TSC打印测试");
typedef int (*TSCabout)();
typedef int (*TSCopenport)(char*);
typedef int (*TSCsendcommand)(char*);
typedef int (*TSCcloseport)();
typedef int (*TSCdownloadpcx)(char*, char*);
QLibrary tscdll("C:\\TSCLIB.dll");
if(tscdll.load())
{
TSCabout about = reinterpret_cast<TSCabout>(tscdll.resolve("about"));
TSCopenport openport=reinterpret_cast<TSCopenport>(tscdll.resolve("openport"));
TSCcloseport closeport=reinterpret_cast<TSCcloseport>(tscdll.resolve("closeport"));
TSCsendcommand sendcommand=reinterpret_cast<TSCsendcommand>(tscdll.resolve("sendcommand"));
TSCdownloadpcx downloadpcx = reinterpret_cast<TSCdownloadpcx>(tscdll.resolve("downloadpcx"));
if(about == nullptr)
{
QMessageBox::warning(this, "Error", "Unable to resolve 'about' function");
return;
}
else if(openport==nullptr)
{
QMessageBox::warning(this, "Error", "Unable to resolve 'openport' function");
return;
}
else if(closeport==nullptr)
{
QMessageBox::warning(this, "Error", "Unable to resolve 'openport' function");
return;
}
else if(sendcommand==nullptr)
{
QMessageBox::warning(this, "Error", "Unable to resolve 'openport' function");
return;
}
else if(downloadpcx==nullptr)
{
QMessageBox::warning(this, "Error", "Unable to resolve 'downloadpcx' function");
return;
}
char printerName[] = "TSC TTP-243 Pro";
openport(printerName);
sendcommand(const_cast<char*>("SIZE 50 mm, 50 mm"));
sendcommand(const_cast<char*>("GAP 2 mm, 0 mm"));
sendcommand(const_cast<char*>("DIRECTION 0"));
sendcommand(const_cast<char*>("CLS"));
// Download the image to the printer as a PCX file
QString imagePath = "C:/Users/DELL/Desktop/QT_Sample/combined.png";
QByteArray imageData;
QFile imageFile(imagePath);
if(imageFile.open(QIODevice::ReadOnly))
{
qDebug() << "Image file opened successfully";
imageData = imageFile.readAll();
imageFile.close();
}
else
{
qDebug() << "Failed to open image file at path" << imagePath;
QMessageBox::warning(this, "Error", "Unable to open image file");
closeport();
return;
}
qDebug() << "Image data size: " << imageData.size();
qDebug() << "PCX file path: " << "D:/combined.pcx";
downloadpcx(const_cast<char*>("D:/combined.pcx"), imageData.data());
// Print the image using the PCX file
sendcommand(const_cast<char*>("PUTPCX 10,10,\"combined.pcx\""));
sendcommand(const_cast<char*>("PRINT 1"));
closeport();
}
else
{
QMessageBox::warning(this, "Error", tscdll.errorString());
}
return;
}
但是在运行到downloadpcx(const_cast<char*>("D:/combined.pcx"), imageData.data());时,就跳出弹窗:Could not open file.
如下如所示:
查看了相应路径下的文件,发现downloadpcx函数并没有成功把combined.png转为combined.pcx。 路径下根本没有这个pcx文件。
但是其他的功能都正常,如果只是打印文字,是可以调到TSC打印机打印的。
究竟如何才能用TSC打印PNG图片啊?!怎么解决Could not open file的问题啊?! 求大家指点!