868 lines
33 KiB
C++
868 lines
33 KiB
C++
|
|
#include "QBarCode.h"
|
|
#include "ui_QBarCode.h"
|
|
#include "QImage"
|
|
#include "QFile"
|
|
#include "QFileInfo"
|
|
#include "QDebug"
|
|
#include "stdio.h"
|
|
#include "Public.h"
|
|
#include "QTextEdit"
|
|
//#include "SysPara.h"
|
|
#include "InkJetPrinter.h"
|
|
|
|
static int dotn[]={5,7,9,12,16,24,32,40};
|
|
#define AUTOREF false
|
|
|
|
int AutoCount1=0;
|
|
#define MDTFLAG 0xff00FF10
|
|
#define QRFLAG 0xff00FF20
|
|
#define BARFLAG 0xff00FF30
|
|
|
|
extern InkJetPrinter *pInkJectPrinter;
|
|
|
|
QBarCode::QBarCode(QWidget *parent) :
|
|
QDialog(parent),
|
|
ui(new Ui::QBarCode)
|
|
{
|
|
ui->setupUi(this);
|
|
bcOK=false;
|
|
connect(ui->BarCodeGenerate,SIGNAL(pressed()),this,SLOT(ImgSave()));
|
|
connect(ui->pushBarPreview,SIGNAL(pressed()),this,SLOT(onEnCodeBar()));
|
|
connect(ui->BarCodeScale,SIGNAL(valueChanged(int)),this,SLOT(onScaleChange(int)));
|
|
//connect(ui->spinBoxXPos,SIGNAL(valueChanged(int)),this,SLOT(onXposChange(int)));
|
|
connect(ui->lineEditModule,SIGNAL(textChanged(QString)),this,SLOT(onBarCodeRefresh(QString)));
|
|
//connect(ui->pushBtnOpenFile,SIGNAL(pressed()),this,SLOT(generateCharRamData()));
|
|
AutoRefresh = false;
|
|
ui->labFileName->setText(tr("文件未保存"));
|
|
ui->lineEditModule->setText("BEC301B030");
|
|
fontSize = 9;
|
|
scene = new QGraphicsScene;
|
|
|
|
//ui->BarCodeType->addItem("BARCODE_128B");
|
|
//ui->BarCodeType->addItem("BARCODE_128C");
|
|
//ui->BarCodeType->addItem("BARCODE_128RAW");
|
|
ui->BarCodeType->addItem(tr("BARCODE_128"));
|
|
ui->BarCodeType->addItem(tr("BARCODE_93"));
|
|
//ui->BarCodeType->addItem(tr("自适应编码"));
|
|
ui->BarCodeType->addItem("BARCODE_ISBN");
|
|
ui->BarCodeType->addItem("BARCODE_EAN");
|
|
ui->BarCodeType->addItem("BARCODE_UPC");
|
|
ui->BarCodeType->addItem("BARCODE_39");
|
|
//ui->BarCodeType->addItem("BARCODE_CBR");
|
|
//ui->BarCodeType->addItem("BARCODE_CBR");
|
|
//ui->BarCodeType->addItem("BARCODE_PLS");
|
|
//ui->BarCodeType->addItem("BARCODE_MSI");
|
|
ui->BarCodeHeigh->addItem(tr("5点"));
|
|
ui->BarCodeHeigh->addItem(tr("7点"));
|
|
ui->BarCodeHeigh->addItem(tr("9点"));
|
|
ui->BarCodeHeigh->addItem(tr("12点"));
|
|
ui->BarCodeHeigh->addItem(tr("16点"));
|
|
ui->BarCodeHeigh->addItem(tr("24点"));
|
|
//ui->BarCodeHeigh->addItem(tr("24点"));
|
|
//onEnCodeBar();
|
|
//ui->BarCodeFont->setCurrentIndex(1);
|
|
ui->BarCodeHeigh->setCurrentIndex(3);
|
|
ui->BarCodeType->setCurrentIndex(0);
|
|
//ui->BarCodeScale->setValue(2);
|
|
//ui->spinBoxXPos->setValue(1);
|
|
}
|
|
|
|
QBarCode::~QBarCode()
|
|
{
|
|
delete ui;
|
|
}
|
|
void QBarCode::TextBarCode(QString txt,int index)
|
|
{
|
|
int ps = 1, pcl = 0, oflags;
|
|
ps = 0; pcl = 0;
|
|
oflags = BARCODE_OUT_PS | BARCODE_OUT_NOHEADERS;
|
|
/* and my data as code-128B autodetected */
|
|
|
|
//qDebug()<<"TextBarCode, txtLen:"<<txt.length()<<txt<<index;
|
|
switch(index)
|
|
{
|
|
case 0: Barcode_Encode_and_Print((char *)txt.toStdString().c_str(),stdout, 510, 60, 0, 0,
|
|
BARCODE_128 | oflags);
|
|
// oflags);
|
|
break;
|
|
case 1:
|
|
Barcode_Encode_and_Print((char *)txt.toStdString().c_str(),stdout, 0, 0, 0, 0,
|
|
BARCODE_93| oflags);
|
|
break;
|
|
// case 2:
|
|
// Barcode_Encode_and_Print((char *)txt.toStdString().c_str(),stdout, 0, 0, 0, 0,
|
|
// oflags);
|
|
// //BARCODE_ISBN | oflags);
|
|
|
|
// break;
|
|
case 2:
|
|
Barcode_Encode_and_Print((char *)txt.toStdString().c_str(),stdout, 0, 0, 0, 0,
|
|
// oflags);
|
|
BARCODE_ISBN | oflags);
|
|
break;
|
|
case 3:
|
|
Barcode_Encode_and_Print((char *)txt.toStdString().c_str(),stdout, 0, 0, 0, 0,
|
|
BARCODE_EAN | oflags);
|
|
break;
|
|
case 4:
|
|
Barcode_Encode_and_Print((char *)txt.toStdString().c_str(),stdout, 510, 60, 0, 0,
|
|
BARCODE_UPC | oflags);
|
|
break;
|
|
case 5:
|
|
Barcode_Encode_and_Print((char *)txt.toStdString().c_str(),stdout, 510, 60, 0, 0,
|
|
BARCODE_39 | oflags);
|
|
break;
|
|
case 6:
|
|
Barcode_Encode_and_Print((char *)txt.toStdString().c_str(),stdout, 510, 60, 0, 0,
|
|
BARCODE_CBR | oflags);
|
|
|
|
break;
|
|
case 7:
|
|
Barcode_Encode_and_Print((char *)txt.toStdString().c_str(),stdout, 510, 60, 0, 0,
|
|
BARCODE_PLS| oflags);
|
|
|
|
break;
|
|
case 8:
|
|
Barcode_Encode_and_Print((char *)txt.toStdString().c_str(),stdout, 510, 60, 0, 0,
|
|
BARCODE_MSI | oflags);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void QBarCode::bindSysPara(SysPara *sysPara){
|
|
this->sysPara =sysPara;
|
|
}
|
|
|
|
void QBarCode::onEnCodeBar(){
|
|
QString txt2 =ui->lineEditModule->text().trimmed();
|
|
QString txt3 =ui->lineEditLine->text().trimmed();
|
|
QString txt4 =ui->lineEditLine1->text().trimmed();
|
|
QString txtVar1,txtVar2;
|
|
QString txtVar5,txtVar6;
|
|
inkColor = ui->comboBoxInkColor->currentIndex()?1:0;
|
|
QString txt;
|
|
if(sysPara)
|
|
{
|
|
sysPara->para.bcOption.var1Type =1;
|
|
sysPara->para.bcOption.var2Type =2;
|
|
txtVar1=getVarTxt(ui->BarCodeVarEN1->isChecked(),sysPara->para.bcOption.var1Type);
|
|
txtVar2=getVarTxt(ui->BarCodeVarEN2->isChecked(),sysPara->para.bcOption.var2Type);
|
|
txtVar5=getVarTxt(ui->checkBoxBarDate->isChecked(),3);
|
|
txtVar6=getVarTxt(ui->checkBoxBarTime->isChecked(),4);
|
|
if(ui->BarCodeVarEN1->isChecked()) ui->lineEditVar1->setText(txtVar1);
|
|
if(ui->BarCodeVarEN2->isChecked()) ui->lineEditVar2->setText(txtVar2);
|
|
}
|
|
else qDebug()<<"no para init.";
|
|
|
|
// %06d%04d TextBarCode, txtLen: 30 "BCD030000025000000020000340000" 1
|
|
if(sysPara->para.isCheckInsertTime)
|
|
{
|
|
txt = txt2 + " " + txtVar5 + " " + txtVar6 + " " + txtVar1;
|
|
}
|
|
else
|
|
{
|
|
txt = txt2 + txtVar1+txt3+txtVar2+ txt4;
|
|
}
|
|
ui->label_Txt->setText(txt);
|
|
TextBarCode(txt, ui->BarCodeType->currentIndex());
|
|
ShowBarCode(&imgBar);
|
|
//imgBar.save(QString().sprintf("BARV%d.bmp",imgBar.height()+ ui->BarCodeType->currentIndex()*100 ));
|
|
}
|
|
|
|
QString QBarCode::getLabe_TxtString()
|
|
{
|
|
return ui->label_Txt->text();
|
|
}
|
|
|
|
QString QBarCode::getVarTxt(bool varEn,unsigned varType)
|
|
{
|
|
int tmp=1;
|
|
int varNum= (varType==1)? sysPara->para.bcOption.var1Num:
|
|
(varType==2)? sysPara->para.bcOption.var2Num:
|
|
4;
|
|
QString txtVar="";
|
|
char fmt[]="%04d";
|
|
sprintf(fmt,"%%%02dd",varNum);
|
|
while(varNum-->0) tmp*=10;
|
|
//qDebug()<<"fmt"<<fmt<<varNum<<tmp<<varType;
|
|
if(varEn)
|
|
{
|
|
if(varType==0) { }
|
|
else if(varType==1) {
|
|
int var1 = QString::number(sysPara->status.calandar_and_dat.cnt1CurrValue,16).toInt();
|
|
txtVar.sprintf(fmt,var1%tmp); //变量1
|
|
}
|
|
else if(varType==2){
|
|
int var2 = QString::number(sysPara->status.calandar_and_dat.cnt2CurrValue,16).toInt();
|
|
txtVar.sprintf(fmt,var2%tmp); //变量2
|
|
}
|
|
else if(varType==3)
|
|
{ //日期
|
|
// txtVar.sprintf("%04d%02d%02d",sysPara->status.calandar_and_dat.year,
|
|
// sysPara->status.calandar_and_dat.month,
|
|
// sysPara->status.calandar_and_dat.day);
|
|
QHash<int, QString> dateFormat;
|
|
dateFormat.insert(0,"yyyy-MM-dd");
|
|
dateFormat.insert(1,"yyyy/MM/dd");
|
|
dateFormat.insert(2,"yyyyMMdd");
|
|
dateFormat.insert(3,"yyyy年MM月dd日");
|
|
dateFormat.insert(4,"yyyy-MM-dd hh:mm");
|
|
dateFormat.insert(5,"yyyy/MM/dd hh:mm");
|
|
dateFormat.insert(6,"yyyyMMddhhmm");
|
|
dateFormat.insert(7,"yyyy年MM月dd日 hh:mm");
|
|
dateFormat.insert(8,"hh:mm");
|
|
dateFormat.insert(9,"dd MM yy");
|
|
dateFormat.insert(10,"dd/MM/yy");
|
|
dateFormat.insert(11,"dd/MM/yyyy");
|
|
dateFormat.insert(12,"dd-MM-yy");
|
|
dateFormat.insert(13,"dd-MM-yyyy");
|
|
|
|
txtVar = QDateTime::currentDateTime().toString(dateFormat.value(sysPara->para.ExpirySplitMode));
|
|
}
|
|
else if(varType==4)
|
|
{ //时间
|
|
// txtVar.sprintf("%02d%02d",sysPara->status.calandar_and_dat.hour,
|
|
// sysPara->status.calandar_and_dat.minute);
|
|
//sysPara->status.calandar_and_dat.second);
|
|
txtVar = QDateTime::currentDateTime().toString("hh:mm");
|
|
}
|
|
else {}
|
|
|
|
}
|
|
return txtVar;
|
|
}
|
|
|
|
/*
|
|
* Do it all in one step
|
|
*/
|
|
int QBarCode::Barcode_Encode_and_Print(char *text, FILE *f, int wid, int hei,
|
|
int xoff, int yoff, int flags)
|
|
{
|
|
struct Barcode_Item * bc;
|
|
|
|
if (!(bc=Barcode_Create(text))) {
|
|
qDebug()<<"mem less then:";//<< -ENOMEM ;
|
|
return -1;
|
|
}
|
|
|
|
if ( Barcode_Position(bc, wid, hei, xoff, yoff, 0.0) < 0
|
|
|| Barcode_Encode(bc, flags) < 0
|
|
//|| Barcode_Print(bc, f, flags) < 0
|
|
) {
|
|
qDebug()<<"EnCode Error:"<< bc->error;
|
|
Barcode_Delete(bc);
|
|
return -1;
|
|
}
|
|
//QFile f1;
|
|
bcText = text;
|
|
bcTextinfo = bc->textinfo;
|
|
bcPartial = bc->partial;
|
|
Barcode_ToImage(bc);// KEY
|
|
Barcode_Delete(bc);
|
|
return 0;
|
|
}
|
|
// 将条形码编码转换成点阵图形
|
|
int QBarCode::Barcode_ToImage(Barcode_Item * bc ){
|
|
|
|
bc->ascii;
|
|
bc->partial;
|
|
bc->textinfo;
|
|
int len =QString(bc->partial).length();
|
|
int BarTypeNumber = ui->BarCodeType->currentIndex();
|
|
|
|
QString txt = bc->ascii;
|
|
txt = txt.trimmed();
|
|
inkColor = ui->comboBoxInkColor->currentIndex()?1:0;
|
|
int xoffset = inkColor?16:8;if((BarTypeNumber == 3) && (inkColor == 0))xoffset+=8;
|
|
int bcWidth1 =0;
|
|
for(int i=0;i<len;i++)
|
|
bcWidth1+= (bc->partial[i]&0x0f);
|
|
|
|
bcHeight = dotn[ui->BarCodeHeigh->currentIndex()] ;//+(inkColor?xoffset/2:0);
|
|
bcWidth = xoffset + bcWidth1*(1+ui->BarCodeDot->currentIndex());
|
|
if((BarTypeNumber == 3) && (inkColor == 0))
|
|
bcWidth += 4;
|
|
QImage image(bcWidth,bcHeight,QImage::Format_ARGB32);
|
|
// inkColor --白墨
|
|
//if(inkColor)
|
|
{
|
|
for(int j=0;j<=bcHeight-1;j++){
|
|
for(int n=0;( n<bcWidth);n++)
|
|
image.setPixel(n,j,inkColor?0xff000000://白墨
|
|
0xff00ff00);// 黑墨
|
|
//for(int n=0;( n<txtWidth);n++)
|
|
// image.setPixel(bcWidth+n,j,0x00ffffff);
|
|
}
|
|
|
|
}
|
|
//else
|
|
{
|
|
//image.fill(AutoCount1?0xff00ff00:0x00ffffff);
|
|
}
|
|
|
|
//0b1a2a4123221223211221132221231213212223112312131311222314111b3c1a1b
|
|
//11:12:1 22:12:2 33:12:3 44:12:4 55:12:5 66:12:6 77:12:7 88:12:8
|
|
//02112141232212232112211322212312132122231123121313112223141112331112
|
|
int i=0;
|
|
//memset(pointMap,0,sizeof(pointMap));
|
|
// unsigned char *img= image.bits();
|
|
unsigned char DotNum=(1+ui->BarCodeDot->currentIndex());
|
|
int xx = xoffset >> 1;
|
|
if((BarTypeNumber == 3) && (inkColor == 0))
|
|
xx = (xoffset >>1 ) + 2;
|
|
int yoffset =0;//(inkColor?xx-1:0);
|
|
unsigned char py= inkColor?bcHeight-1:bcHeight-1;
|
|
|
|
for(int j=yoffset;j<=py ;j++){
|
|
i=xx;
|
|
for(int n=0;( n<len);n++)
|
|
{
|
|
int c= (bc->partial[n])&0x07;
|
|
c = c*DotNum;
|
|
if(n&0x01) {
|
|
for(int r=0;r<c;r++){
|
|
if(((n==1)||(n==3)||(n==29)||(n==31)||(n==57)||(n==59)) && ((BarTypeNumber == 3) && (inkColor == 0))) {
|
|
image.setPixel(i,j,(inkColor?0x00ffffff:0xff000000));
|
|
}
|
|
else if((BarTypeNumber == 3) && (inkColor == 0))
|
|
image.setPixel(i,j-3,(inkColor?0x00ffffff:0xff000000)); // 0xff no touming
|
|
else
|
|
image.setPixel(i,j,(inkColor?0x00ffffff:0xff000000));
|
|
i++;
|
|
}
|
|
}
|
|
else i+=c;
|
|
}
|
|
}
|
|
|
|
if(ui->checkBoxAddText->isChecked())
|
|
{
|
|
OnCharRamData(&image); // add text
|
|
}
|
|
//OnCharRamData2(&image,txtWidth,height); // add text
|
|
//ShowBarCode(&image);
|
|
imgBar = image.copy(image.rect());
|
|
|
|
if(AutoCount1 && pInkJectPrinter->modbus->modbus_up_QRCODE == 0)
|
|
{
|
|
imgBar.setPixel(0,imgBar.height()-1,BARFLAG); // 设置定位标志
|
|
//qDebug()<<"Barcode_ToImage:x,y"<<imgBar.width()<<imgBar.height()<<bcWidth+txtWidth;
|
|
}
|
|
|
|
bcOK=true;
|
|
}
|
|
void QBarCode::ImgSave(){
|
|
|
|
if(bcOK==0)
|
|
onEnCodeBar();
|
|
QString fileName= bcText;
|
|
if(fileName.length()>15) fileName= fileName.left(15);
|
|
fileName.remove(QRegExp("/"));
|
|
fileName.remove(QRegExp("\\"));
|
|
ui->labFileName->setText(fileName+".bmp" );
|
|
this->pictureName = "/home/plg/doc/"+ fileName + ".bmp" ;
|
|
|
|
QString s2="";
|
|
imgBar.rgbSwapped();
|
|
if(pInkJectPrinter->modbus->modbus_up_QRCODE == 1)
|
|
{
|
|
// qDebug()<<"save modbusQR";
|
|
s2 = "modbusQR.bmp";
|
|
this->pictureName=DOC_ROOT_DIR+s2;
|
|
imgBar.save(pictureName);
|
|
pInkJectPrinter->modbus->modbus_up_QRCODE = 0;
|
|
}
|
|
else
|
|
{
|
|
imgBar.save(this->pictureName);
|
|
}
|
|
//qDebug()<<"svae imag"<<pictureName;
|
|
//imgBar.save(QString().sprintf("BARV%d.bmp",imgBar.height()+ ui->BarCodeType->currentIndex()*100 ));
|
|
|
|
sysPara->saveParaToFile();
|
|
}
|
|
|
|
|
|
void QBarCode::onScaleChange(int n){
|
|
//ShowBarCode("tmp.bmp");
|
|
if(bcOK==0)
|
|
onEnCodeBar();
|
|
ShowBarCode(&imgBar);
|
|
}
|
|
|
|
void QBarCode::onXposChange(int n){
|
|
//ShowBarCode(this->pictureName);
|
|
//emit onEnCodeBar();
|
|
if(AutoRefresh) {
|
|
onEnCodeBar();
|
|
// ui->lineEdit->setFocus();
|
|
}
|
|
}
|
|
|
|
void QBarCode::SetOption(bcOption_t *bcOption){
|
|
|
|
AutoCount1=bcOption->var1En | bcOption->var2En;
|
|
|
|
//ui->checkBoxInsertFlag->setChecked(1);//bcOption->AutoCount
|
|
//ui->BarCodeFont->setCurrentIndex(bcOption->font);
|
|
if(bcOption->height<7) ui->BarCodeHeigh->setCurrentIndex(bcOption->height);
|
|
else qDebug()<<"SetOption height"<<bcOption->height;
|
|
ui->BarCodeType->setCurrentIndex(bcOption->type);
|
|
ui->BarCodeScale->setValue(bcOption->scale);
|
|
ui->comboBoxInkColor->setCurrentIndex(bcOption->InkColor?1:0);
|
|
QString s1=bcOption->text;
|
|
ui->lineEditModule->setText(bcOption->TxtModule);
|
|
ui->lineEditLine->setText(bcOption->TxtLine);
|
|
ui->lineEditLine1->setText(bcOption->TxtLine1);
|
|
if(sysPara)
|
|
{
|
|
sysPara->para.bcOption.var1Type =1;
|
|
sysPara->para.bcOption.var2Type =2;
|
|
|
|
txtVar1=getVarTxt(bcOption->var1En,bcOption->var1Type);
|
|
ui->lineEditVar1->setText(txtVar1);
|
|
txtVar2=getVarTxt(bcOption->var2En,bcOption->var2Type);
|
|
ui->lineEditVar2->setText(txtVar2);
|
|
|
|
ui->BarCodeVarEN1->setChecked(bcOption->var1En);
|
|
ui->BarCodeVarEN2->setChecked(bcOption->var2En);
|
|
}
|
|
else qDebug()<<"no para init.";
|
|
|
|
if(s1.length()>15) s1 = s1.left(15);
|
|
s1 +=".bmp";
|
|
this->pictureName=DOC_ROOT_DIR+s1;
|
|
//onEnCodeBar();
|
|
AutoRefresh = AUTOREF;
|
|
|
|
sysPara->para.isCheckBarCodeAddText ? ui->checkBoxAddText->setChecked(true) : ui->checkBoxAddText->setChecked(false);
|
|
ui->checkBoxBarDate->setChecked(sysPara->para.isCheckBarDate);
|
|
ui->checkBoxBarTime->setChecked(sysPara->para.isCheckBarTime);
|
|
}
|
|
|
|
void QBarCode::GetOption(bcOption_t *bcOption){
|
|
bcOption->height = ui->BarCodeHeigh->currentIndex();
|
|
bcOption->type = ui->BarCodeType->currentIndex();
|
|
bcOption->InkColor = ui->comboBoxInkColor->currentIndex()?1:0;
|
|
bcOption->AutoCount = 1;
|
|
|
|
bcOption->var1En = ui->BarCodeVarEN1->isChecked();
|
|
bcOption->var2En = ui->BarCodeVarEN2->isChecked();
|
|
|
|
snprintf(bcOption->text,sizeof(bcOption->text),"%s",bcText.toLatin1().constData());
|
|
if(sysPara){
|
|
snprintf(bcOption->TxtModule,sizeof(bcOption->TxtModule),"%s",ui->lineEditModule->text().toLatin1().constData());
|
|
snprintf(bcOption->TxtLine,sizeof(bcOption->TxtLine),"%s",ui->lineEditLine->text().toLatin1().constData());
|
|
snprintf(bcOption->TxtLine1,sizeof(bcOption->TxtLine1),"%s",ui->lineEditLine1->text().toLatin1().constData());
|
|
}
|
|
|
|
sysPara->para.isCheckBarCodeAddText = ui->checkBoxAddText->isChecked() ? true : false;
|
|
sysPara->para.isCheckBarDate = ui->checkBoxBarDate->isChecked();
|
|
sysPara->para.isCheckBarTime = ui->checkBoxBarTime->isChecked();
|
|
}
|
|
|
|
void QBarCode::GetOption2(bcPara_t *bcOption){
|
|
bcOption->height = imgBar.height();
|
|
bcOption->type = ui->BarCodeType->currentIndex();
|
|
//bcOption->InkColor = ui->comboBoxInkColor->currentIndex()?1:0;
|
|
bcOption->width = imgBar.width();
|
|
//snprintf(bcOption->text,sizeof(bcOption->text),"%s",bcText.toLatin1().constData());
|
|
}
|
|
void QBarCode::on_pushBtnColse_pressed()
|
|
{
|
|
flagInsert = ui->checkBoxInsertFlag->checkState()?1:0;
|
|
|
|
//ui->checkBoxInsertFlag->setChecked(0);
|
|
//qDebug()<<"flagInsert"<<flagInsert<<ui->checkBoxInsertFlag->checkState();图片方面
|
|
//flagCount = ui
|
|
emit onBarCodeClose();
|
|
this->close();
|
|
AutoRefresh = false;
|
|
bcOK = false;
|
|
}
|
|
void QBarCode::onBarCodeRefresh(QString s){
|
|
if(AutoRefresh)
|
|
{
|
|
//onEnCodeBar();
|
|
//ui->lineEdit->setFocus();
|
|
}
|
|
//ui->label_Txt->setText(bcText);
|
|
}
|
|
|
|
void QBarCode::extSetText(QString text)
|
|
{
|
|
//ui->comboBoxType->setCurrentIndex(1);
|
|
//ui->lineEditIn->setText(text);
|
|
//ui->lineEditIn2->setText("");
|
|
//ui->lineEditCNT->setText("");
|
|
// QString strTxt=ui->lineEditModule->text();
|
|
|
|
if(sysPara->para.isCheckInsertTime)
|
|
{
|
|
//ui->lineEditLine->setText(text);
|
|
}
|
|
else
|
|
{
|
|
ui->lineEditModule->setText(text);
|
|
ui->lineEditLine->setText("");
|
|
ui->BarCodeVarEN1->setChecked(false);
|
|
}
|
|
ui->lineEditLine1->setText("");
|
|
ui->BarCodeVarEN2->setChecked(false);
|
|
|
|
pInkJectPrinter->modbus->modbus_up_QRCODE =1;
|
|
|
|
emit onEnCodeBar();
|
|
emit ImgSave();
|
|
}
|
|
|
|
void QBarCode::setFontSize(int PixelSize)
|
|
{
|
|
fontSize = PixelSize;
|
|
}
|
|
int QBarCode::ImgLeftPos(QImage *img){
|
|
int firstValidCol=0;
|
|
bool found =0;
|
|
int w = img->width();
|
|
int h = img->height();
|
|
unsigned char *dat = img->bits(); //if( *(dat+ j*4 + k*w*4 ) ==0x00 )
|
|
for(int j = 0; j <w; j ++){
|
|
for(int k =0; k < h ; k++){
|
|
//QRgb rgb = img->pixel(j,k); if((rgb&0x00ffffff)==0){
|
|
if( *(dat+ j*4 + k*w*4 ) ==0x00 ) {
|
|
if(j>firstValidCol) firstValidCol = j ;
|
|
found=1;
|
|
break;
|
|
}
|
|
}
|
|
if(found) break;
|
|
}
|
|
return firstValidCol;
|
|
}
|
|
|
|
int QBarCode::ImgRightPos(QImage *img){
|
|
int lastValidCol=0;
|
|
bool found=0;
|
|
int scnt=0;
|
|
int w = img->width();
|
|
int h = img->height();
|
|
lastValidCol = w-1;
|
|
unsigned char *dat = img->bits(); //if( *(dat+ j*4 + k*w*4 ) ==0x00 )
|
|
for(int j = w-1; j>0; j --){
|
|
for(int k =0; k < h ; k++){
|
|
scnt++;
|
|
//QRgb rgb = img->pixel(j,k);
|
|
//if((rgb&0x00ffffff)==0){
|
|
if( *(dat+ j*4 + k*w*4 ) ==0x00 )
|
|
{
|
|
if(j<lastValidCol) lastValidCol = j ;
|
|
found=1;
|
|
break;
|
|
}
|
|
}
|
|
if(found) break;
|
|
}
|
|
|
|
//qDebug()<<"ImgRightPos"<<scnt;
|
|
return lastValidCol+1;
|
|
}
|
|
// OK
|
|
void QBarCode::OnCharRamData1(QImage *img1){/* 生成字模数据 */
|
|
|
|
}
|
|
void QBarCode::OnCharRamData2(QImage *img1,int Txtwidth,int height){/* 生成字模数据 横向 */
|
|
|
|
QSize size(Txtwidth,bcHeight);
|
|
QImage image(size,QImage::Format_ARGB32);
|
|
image.fill(0x00ffffff);
|
|
QPainter painter(&image);
|
|
textEdit.setGeometry(0,0,1000,50);
|
|
textEdit.setFixedWidth(image.rect().width());
|
|
textEdit.clearFocus();
|
|
textEdit.ensureCursorVisible();
|
|
//textEdit.inputMethodHints()
|
|
textEdit.show(); //如果不显示,似乎不会绘制
|
|
textEdit.ensureCursorVisible();
|
|
textEdit.document()->drawContents (&painter,image.rect() ); //将text写到picimage.rect()
|
|
textEdit.hide();
|
|
int xoff=0;
|
|
int lastValidRow = (bcHeight>height)? height + (bcHeight-height)/2: height;
|
|
// height
|
|
int startValidRow = (bcHeight>height)? (bcHeight-height)/2: 0;
|
|
ui->labStatus->setText(tr("标签宽度:%1,标签高度:%2,文本宽度:%3").arg(bcWidth).arg(height+bcHeight).arg(Txtwidth));// 文本宽度 标签宽度 条码宽度
|
|
xoff =bcWidth+inkColor*4 ;
|
|
for(int k = 0; k < height; k++){
|
|
for(int j = 0; j < Txtwidth; j ++){
|
|
QRgb rgb = image.pixel(j,k);
|
|
img1->setPixel( xoff+j,startValidRow+k,rgb);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// Test
|
|
void QBarCode::OnCharRamData(QImage *img1){/* 生成字模数据 */
|
|
textEdit.document()->setDocumentMargin(0);
|
|
int Txtwidth ;
|
|
//int height = bcHeight<16?bcHeight:16;
|
|
int height = fontSize;
|
|
int letterSpace=1 ;
|
|
textEdit.clear();
|
|
int DPI = textEdit.logicalDpiX();
|
|
DPI = DPI?DPI:1; //Pixel = DPI * Point / 72
|
|
QFont font = textEdit.font();
|
|
textEdit.insertHtml(QString("<span style=\" font-family:'%1'; font-size:%2pt; font-weight:%3;\">%4</span>")
|
|
.arg(Public::getFontFamily(height)).arg(height*72/DPI).arg(400).arg(bcText)); //600表示加粗,400表示普通
|
|
|
|
font = textEdit.font();
|
|
font.setLetterSpacing(QFont::AbsoluteSpacing,letterSpace);
|
|
font.setFamily(Public::getFontFamily(height));
|
|
font.setPixelSize(height);
|
|
font.setWeight(QFont::Normal);
|
|
textEdit.setFont(font);
|
|
QFontMetrics fontMetrics(font);
|
|
Txtwidth = (fontMetrics.width(bcText));
|
|
QSize size(Txtwidth+(Txtwidth>>2),height+bcHeight);
|
|
QImage image(size,QImage::Format_ARGB32);
|
|
image.fill(0x00ffffff);
|
|
QPainter painter(&image);
|
|
textEdit.setGeometry(0,0,1000,50);
|
|
textEdit.show(); //如果不显示,似乎不会绘制
|
|
textEdit.setFixedWidth(image.rect().width());
|
|
textEdit.document()->drawContents (&painter,image.rect() ); //将text写到picimage.rect()
|
|
textEdit.hide();
|
|
Txtwidth = ImgRightPos(&image);
|
|
image.save("./QbarCodemage.bmp");
|
|
int xoff=0;
|
|
int lastValidRow = height +bcHeight;
|
|
//img1->rect()
|
|
ui->labStatus->setText(tr("标签宽度:%1,标签高度:%2,文本宽度:%3").arg(bcWidth).arg(height+bcHeight).arg(Txtwidth));// 文本宽度 标签宽度 条码宽度
|
|
if( bcWidth >= Txtwidth )
|
|
{
|
|
xoff = (bcWidth-Txtwidth)/2;
|
|
// for(int k = 0; k < height; k++){
|
|
// for(int j = 0; j < Txtwidth; j ++){
|
|
// QRgb rgb = image.pixel(j,k);
|
|
// if((j+xoff)<bcWidth)
|
|
// img1->setPixel( xoff+j,k+bcHeight,rgb);
|
|
// }
|
|
|
|
// }
|
|
|
|
QImage qimg3 = QImage(bcWidth, height+bcHeight + 1, QImage::Format_RGB32);
|
|
qimg3.fill(0xffffff);
|
|
//LogInfo::setRunStatusText(tr("F=%1,S=%2,T=%3").arg(firstPart).arg(secondPart).arg(thirdPart));
|
|
QPainter p(&qimg3);
|
|
|
|
p.drawPixmap(0, 0, img1->width(), img1->height(), QPixmap::fromImage(*img1));
|
|
if((ui->BarCodeType->currentIndex() != 3) || (ui->BarCodeDot->currentIndex() > 0) || (font.pixelSize() > 7) || (ui->comboBoxInkColor->currentIndex() == 1))
|
|
p.drawPixmap(xoff, bcHeight + 1, image.width(), image.height(), QPixmap::fromImage(image));
|
|
else{//LogInfo::setRunStatusText(tr("length=%1").arg(bcText.length()));
|
|
// p.drawPixmap(0, 0, img1->width(), img1->height(), QPixmap::fromImage(*img1));
|
|
// p.drawPixmap(xoff - 12, bcHeight + 1 - 2, image.width(), image.height(), QPixmap::fromImage(image));
|
|
QString firstPart = bcText.mid(0,1); // 第一个字符
|
|
QString secondPart = bcText.mid(1, 6); // 第二个到第六个字符
|
|
QString thirdPart = bcText.mid(7); // 第七个到第十四个字符,注意根据实际情况调整
|
|
if (!firstPart.isEmpty()) {
|
|
QImage partImage = image.copy(0, 0, fontMetrics.width(firstPart), image.height()); // 裁剪出第一部分的图像
|
|
//p.drawPixmap(xoff - 18, bcHeight - 1 , partImage.width(), partImage.height(), QPixmap::fromImage(partImage));//77
|
|
//p.drawPixmap(xoff - 12, bcHeight - 1 , partImage.width(), partImage.height(), QPixmap::fromImage(partImage));//89
|
|
if(Txtwidth < 70)
|
|
p.drawPixmap(xoff - 24, bcHeight - 2 , partImage.width(), partImage.height(), QPixmap::fromImage(partImage));
|
|
else if(Txtwidth < 80)
|
|
p.drawPixmap(xoff - 18, bcHeight - 2 , partImage.width(), partImage.height(), QPixmap::fromImage(partImage));
|
|
else
|
|
p.drawPixmap(xoff - 12, bcHeight - 2 , partImage.width(), partImage.height(), QPixmap::fromImage(partImage));
|
|
}
|
|
if (!secondPart.isEmpty()) {
|
|
int startXSecondPart = xoff - 12 + fontMetrics.width(firstPart); // 增量包括第一部分的宽度和额外间距,77
|
|
QImage partImage = image.copy(0+6, 0, fontMetrics.width(secondPart), image.height()); // 裁剪出第二部分的图像
|
|
if(Txtwidth > 85){
|
|
startXSecondPart = xoff - 10 + fontMetrics.width(firstPart);//89
|
|
partImage = image.copy(0+6, 0, fontMetrics.width(secondPart), image.height()); // 裁剪出第二部分的图像
|
|
}
|
|
else if(Txtwidth < 70){
|
|
startXSecondPart = xoff - 15 + fontMetrics.width(firstPart);
|
|
partImage = image.copy(0+4, 0, fontMetrics.width(secondPart), image.height());
|
|
}
|
|
p.drawPixmap(startXSecondPart, bcHeight - 2, partImage.width(), partImage.height(), QPixmap::fromImage(partImage));
|
|
}
|
|
if (!thirdPart.isEmpty()) {
|
|
int startXThirdPart = xoff - 8 + fontMetrics.width(secondPart)+12; // 类似地,增加第二部分的宽度和间距,77
|
|
QImage partImage = image.copy(0+6+36, 0, fontMetrics.width(thirdPart), image.height());
|
|
if(Txtwidth > 85){
|
|
startXThirdPart = xoff - 10 + fontMetrics.width(secondPart)+12;//89
|
|
partImage = image.copy(0+6+36, 0, fontMetrics.width(thirdPart), image.height()); // 裁剪出第三部分的图像
|
|
}
|
|
else if(Txtwidth < 70){
|
|
startXThirdPart = xoff - 5 + fontMetrics.width(secondPart)+12;
|
|
partImage = image.copy(0+4+30, 0, fontMetrics.width(thirdPart), image.height());
|
|
}
|
|
p.drawPixmap(startXThirdPart, bcHeight - 2, partImage.width(), partImage.height(), QPixmap::fromImage(partImage));
|
|
}
|
|
// p.drawPixmap(0, 0, img1->width(), img1->height(), QPixmap::fromImage(*img1));
|
|
|
|
// p.drawPixmap(xoff, bcHeight + 1, image.width(), image.height(), QPixmap::fromImage(image));
|
|
|
|
// 绘制第二部分字符
|
|
// QImage tempImg = drawTextToImage(secondPart, font, image.colorSpace()); // 使用相同函数绘制第二部分文字到临时图像
|
|
// p.drawPixmap(xoff + 40, bcHeight + 1 - 2, tempImg.width(), tempImg.height(), QPixmap::fromImage(tempImg));
|
|
|
|
// // 绘制第三部分字符
|
|
// QImage tempImg2 = drawTextToImage(thirdPart, font, image.colorSpace()); // 同理,绘制第三部分
|
|
// p.drawPixmap(xoff + 120, bcHeight + 1 - 2, tempImg2.width(), tempImg2.height(), QPixmap::fromImage(tempImg2));
|
|
|
|
//QPixmap pix3 = QPixmap::fromImage(qimg3);
|
|
//ui->label_dislocation->setPixmap(pix3.scaled(ui->label_dislocation->width(), ui->label_dislocation->height(), Qt::KeepAspectRatio));
|
|
}
|
|
*img1 = qimg3.copy(0,0,qimg3.width(),qimg3.height());
|
|
|
|
img1->save("./QbarCodemage1.bmp");
|
|
}
|
|
else {
|
|
int h1 = lastValidRow-1; //24 9
|
|
for(int k = h1; k >= bcHeight; k--){
|
|
for(int j = 0; j < Txtwidth; j ++){
|
|
QRgb rgb = image.pixel(j,k-bcHeight);
|
|
image.setPixel( j,k,rgb);
|
|
image.setPixel(j,k-bcHeight,0x00ffffff);
|
|
}
|
|
} // dd djfdkf djfdkf二维码
|
|
xoff = (Txtwidth-bcWidth)/2;
|
|
for(int k = 0; k < bcHeight; k++){
|
|
|
|
// if(inkColor){
|
|
// if(k<bcHeight-1)
|
|
// for(int j = 0; j <= xoff+1; j ++){
|
|
// image.setPixel( j,k,0xff000000);
|
|
// image.setPixel( Txtwidth-j,k,0xff000000);
|
|
// }
|
|
// }
|
|
for(int j = 0; j < bcWidth; j ++){
|
|
QRgb rgb = img1->pixel(j,k);
|
|
if((xoff+j)<Txtwidth) image.setPixel( xoff+j,k,rgb);
|
|
}
|
|
}
|
|
*img1 = image.copy(0,0,Txtwidth,image.height());
|
|
img1->save("./QbarCodemage2.bmp");
|
|
}
|
|
}
|
|
|
|
void QBarCode::ShowBarCode(QString fileName)
|
|
{
|
|
//scene = new QGraphicsScene;
|
|
scene->clear();
|
|
int scale = ui->BarCodeScale->value();
|
|
scene->setSceneRect( 0,0,bcWidth*(scale),bcHeight*(scale+1)+10);
|
|
scene->setItemIndexMethod(QGraphicsScene::NoIndex);
|
|
//QPixmap pixmap(QPixmap::fromImage(*img));
|
|
QPixmap pixmap(fileName);
|
|
pixmap = pixmap.scaledToHeight(bcHeight*(scale+1));
|
|
/***************设置view的场景*****************************/
|
|
ui->BarCodeView->setScene(scene);
|
|
ui->BarCodeView->setRenderHint(QPainter::Antialiasing);
|
|
ui->BarCodeView->setCacheMode(QGraphicsView::CacheBackground);
|
|
ui->BarCodeView->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
|
|
ui->BarCodeView->setDragMode(QGraphicsView::ScrollHandDrag);
|
|
//ui->graphicsView->resize(400,300);
|
|
ui->BarCodeView->setScene(scene);//设置view的场景
|
|
|
|
}
|
|
void QBarCode::ShowBarCode(QImage *img)
|
|
{
|
|
//scene = new QGraphicsScene;
|
|
scene->clear();
|
|
int scale = ui->BarCodeScale->value();
|
|
scene->setSceneRect( 0,0,bcWidth*(scale+1)+10,bcHeight*(scale));
|
|
scene->setItemIndexMethod(QGraphicsScene::NoIndex);
|
|
QPixmap pixmap(QPixmap::fromImage(*img));
|
|
//pixmap = pixmap.scaledToHeight(bcHeight*(scale+1));
|
|
pixmap = pixmap.scaledToWidth((scale+1)*bcWidth);
|
|
item = scene->addPixmap(pixmap);
|
|
/***************设置view的场景*****************************/
|
|
ui->BarCodeView->setScene(scene);
|
|
ui->BarCodeView->setRenderHint(QPainter::Antialiasing);
|
|
ui->BarCodeView->setCacheMode(QGraphicsView::CacheBackground);
|
|
ui->BarCodeView->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
|
|
ui->BarCodeView->setDragMode(QGraphicsView::ScrollHandDrag);
|
|
//ui->graphicsView->resize(400,300);
|
|
ui->BarCodeView->setScene(scene);//设置view的场景
|
|
}
|
|
|
|
|
|
QString QBarCode::TextToBCImg(bcOption_t *bcVar,QString txt)
|
|
{
|
|
|
|
if(txt.isEmpty()){txt = "";return txt;}
|
|
// 转换条形码
|
|
// ----
|
|
int index = bcVar->barType;
|
|
|
|
TextBarCode(txt, index);
|
|
//bcVar->width = imgBar.width();
|
|
//bcVar->height = imgBar.height();
|
|
bcWidth = imgBar.width();
|
|
bcHeight = imgBar.height();
|
|
//qDebug()<<"TextToBCImg"<<txt<<index;
|
|
return txt;// 返回文件名
|
|
}
|
|
|
|
|
|
int QBarCode::getDat(unsigned char *dat,int varNum)
|
|
{
|
|
int w= imgBar.width();
|
|
int h=imgBar.height();
|
|
int n=0;
|
|
int hStart=0;
|
|
int wStart=0;
|
|
|
|
for(int i = 0 ; i < w; i++)
|
|
{
|
|
unsigned char c=0,c1=0;
|
|
int j=0;
|
|
for( j = hStart; j < h; j++)
|
|
{ QRgb rgb = imgBar.pixel(i,j);
|
|
c |= (rgb == 0xff000000)? (1 << (j%8)):0;
|
|
if(j%8==7) { dat[(i+wStart)*5+(j-hStart)/8]=c; c=0; }
|
|
}
|
|
dat[(i+wStart)*5+(j-hStart)/8]=c;
|
|
}
|
|
|
|
//qDebug()<<"point : "<<n;
|
|
}
|
|
|
|
int QBarCode::getDat2(SysPara* sysPara,int varNum)
|
|
{
|
|
unsigned char *dat;
|
|
bool MaxCol4000;
|
|
if(sysPara->status.flagOfNo67FPGA)
|
|
MaxCol4000 = true;
|
|
else
|
|
MaxCol4000 = false;
|
|
if(MaxCol4000){
|
|
dat =sysPara->status.printDat_Normal;
|
|
}else{
|
|
dat =sysPara->status.printDat;
|
|
}
|
|
int w= imgBar.width();
|
|
int h= imgBar.height();
|
|
int n=0;
|
|
int hStart=0;
|
|
int wStart=0;
|
|
//qDebug()<<w<<h<<"getDat2()";
|
|
//imgBar.save("222BB.bmp");
|
|
for(int i = 0 ; i < w; i++)
|
|
{
|
|
unsigned char c=0,c1=0;
|
|
int j=0;
|
|
for( j = hStart; j < h; j++)
|
|
{ QRgb rgb = imgBar.pixel(i,j);
|
|
c |= (rgb == 0xff000000)? (1 << (j%8)):0;
|
|
if(j%8==7) { dat[(i+wStart)*5+(j-hStart)/8]=c; c=0; }
|
|
}
|
|
dat[(i+wStart)*5+(j-hStart)/8]=c;
|
|
}
|
|
//qDebug()<<"point : "<<n;
|
|
}
|