其他分享
首页 > 其他分享> > 字模上位机实现

字模上位机实现

作者:互联网

具体功能

1.对输入的文字取模,得到每个文字16x16的数据32byte。

2.将32byte数据写入到文件中并保存下来。

捕获

#include "mainwidget.h"

mainWidget::mainWidget(QWidget *parent)
    : QWidget(parent)
{
    paintArea = new PaintArea;        //定义控键设置参数
    pLineEdit = new QLineEdit;
    pTextBrower = new QTextBrowser;
    pPushButton = new QPushButton;
    pPushBtn    = new QPushButton;
    plabel = new QLabel;
    //pinputString = new QString;
    pLineEdit->setFixedWidth(500);
    pLineEdit->setFixedHeight(25);
    plabel->setText("请输入内容");


    pPushButton->setText("取模");
    pPushButton->setFixedHeight(25);
    pPushBtn->setText("保存");
    pPushBtn->setFixedHeight(25);
    pTextBrower->setFixedHeight(300);

    rightLayout = new QGridLayout;        //划分窗口
    rightLayout->addWidget(pLineEdit,0,0);
    rightLayout->addWidget(pPushButton,0,1);
    rightLayout->addWidget(plabel,1,0);
    rightLayout->addWidget(pPushBtn,1,1);

    QVBoxLayout *pv1Layout = new QVBoxLayout(this);
    pv1Layout->addLayout(rightLayout);
    pv1Layout->addWidget(pTextBrower);

    connect(pPushButton,SIGNAL(clicked()),this,SLOT(pushbutton_clicked()));    //取模按键槽函数连接
    connect(pPushBtn,SIGNAL(clicked()),this,SLOT(pushbt_clicked()));        //保存按键槽函数链接
}

mainWidget::~mainWidget()
{

}

void mainWidget::pushbutton_clicked()

{
    pTextBrower->setPlainText(" ");            //清除空间显示
    if(pLineEdit->text() == NULL)            //输入控件为空则返回
    {
        return;
    }
    qDebug() << "button clocked";            //取目标文字
    qDebug() << pLineEdit->text();
    pinputString = pLineEdit->text();
    qDebug() << QString(pinputString[0]);
    qDebug() << QString(pinputString[1]);
    qDebug() << pinputString.size();

    QSize size(16,16);                //设置图片大小
    QImage image(size,QImage::Format_ARGB32);        //设置图片格式    
    hanzicode = 0;

    image.fill(Qt::white);                //填充白色背景

    QPainter painter(&image);                //为图片定义画笔并设置
    painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
    QPen ppen = painter.pen();
    ppen.setColor(Qt::black);        
    QFont pfont = painter.font();                //定义字体        
    pfont.setBold(true);
    pfont.setPixelSize(16);                //字体大小
    painter.setPen(ppen);
    painter.setFont(pfont);


    for(uchar k = 0;k < pinputString.size(); k++)        //遍历输入文字
    {
        image.fill(Qt::white);
        painter.drawText(image.rect(),Qt::AlignLeft | Qt::AlignVCenter,QString(pinputString[k]));        //将目标文字写入图片中
        image.save("C:/Users/Administrator/Desktop/LED_ARY/build-codeary-Desktop_Qt_5_9_9_MinGW_32bit-Debug/debug/myImage.png");

        for(unsigned char i = 0;i < 16;i++)            //对目标图片像素遍历,让图片二值化
        {
            hanzicode = 0;
            for(unsigned char j = 0;j < 16; j++)
            {
                hanzicode >>= 1;
                if(image.pixel(i,j) == 0xff000000)
                {
                    hanzicode |= 0x8000;
                }
            }

            //qDebug("%",hanzicode);
            qDebug("%s",qPrintable(QString::number(hanzicode,2).rightJustified(16,'0')));
            hziCode[2*i+1] = uchar(hanzicode >>8);      //内围PIN 1,3,5,7,9...        //将得到数值存储到字模数组中
            hziCode[2*i] =uchar( hanzicode);            //外围PIN 0,2,4,6,8...   
        }                                    //得到数据显示字符串
        pstring = "0x"+QString::number(hziCode[0],16).rightJustified(2,'0') + "," + "0x"+QString::number(hziCode[1],16).rightJustified(2,'0') + "," +"0x"+QString::number(hziCode[2],16).rightJustified(2,'0')+ "," + "0x"+QString::number(hziCode[3],16).rightJustified(2,'0') + "," \
            + "0x"+QString::number(hziCode[4],16).rightJustified(2,'0') + "," + "0x"+QString::number(hziCode[5],16).rightJustified(2,'0') + "," + "0x"+QString::number(hziCode[6],16).rightJustified(2,'0') + "," + "0x"+QString::number(hziCode[7],16).rightJustified(2,'0') + "," \
            + "0x"+QString::number(hziCode[8],16).rightJustified(2,'0') + "," + "0x"+QString::number(hziCode[9],16).rightJustified(2,'0') + "," + "0x"+QString::number(hziCode[10],16).rightJustified(2,'0') + "," + "0x"+QString::number(hziCode[11],16).rightJustified(2,'0') + "," \
            + "0x"+QString::number(hziCode[12],16).rightJustified(2,'0') + "," + "0x"+QString::number(hziCode[13],16).rightJustified(2,'0') + "," + "0x"+QString::number(hziCode[14],16).rightJustified(2,'0') + "," + "0x"+QString::number(hziCode[15],16).rightJustified(2,'0') + "," + "\n" \
            + "0x"+QString::number(hziCode[16],16).rightJustified(2,'0') + "," + "0x"+QString::number(hziCode[17],16).rightJustified(2,'0') + "," + "0x"+QString::number(hziCode[18],16).rightJustified(2,'0') + "," + "0x"+QString::number(hziCode[19],16).rightJustified(2,'0') + "," \
            + "0x"+QString::number(hziCode[20],16).rightJustified(2,'0') + "," + "0x"+QString::number(hziCode[21],16).rightJustified(2,'0') + "," + "0x"+QString::number(hziCode[22],16).rightJustified(2,'0') + "," + "0x"+QString::number(hziCode[23],16).rightJustified(2,'0') + "," \
            + "0x"+QString::number(hziCode[24],16).rightJustified(2,'0') + "," + "0x"+QString::number(hziCode[25],16).rightJustified(2,'0') + "," + "0x"+QString::number(hziCode[26],16).rightJustified(2,'0') + "," + "0x"+QString::number(hziCode[27],16).rightJustified(2,'0') + "," \
            + "0x"+QString::number(hziCode[28],16).rightJustified(2,'0') + "," + "0x"+QString::number(hziCode[29],16).rightJustified(2,'0') + "," + "0x"+QString::number(hziCode[30],16).rightJustified(2,'0') + "," + "0x"+QString::number(hziCode[31],16).rightJustified(2,'0') + "," ;

        if(k == 0)                                //显示字符数据
        {
            pTextBrower->setPlainText(pstring);

        }
        else
        {
            pTextBrower->append(pstring);
        }
    }
    plabel->setText("取模完成");
}

void mainWidget::pushbt_clicked()
{
    qDebug() << "保存文件";                            //将数据保存到目标文件中
    QFile file("out.txt");

    if(file.open(QFile::WriteOnly | QFile::Truncate))
    {
        QTextStream out(&file);
        out << pTextBrower->toPlainText() << qSetFieldWidth(10) << center <<endl;
    }
    plabel->setText("文件已保存为out.txt");
}
上位机使用效果

捕获

点击保存后会在软件根目录生成out.txt的保存文件。

标签:hziCode,字模,16,实现,0x,number,rightJustified,上位,QString
来源: https://www.cnblogs.com/logicexpression/p/14775521.html