码迷,mamicode.com
首页 > 其他好文 > 详细

qt实现小票打印

时间:2015-07-05 11:07:49      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:qt   小票打印   条码打印   

1.需求

需要实现订单结算后的小票打印,要求能打印商品数据和结算数据及将小票号打印成条码,退货或查询时能通过扫描枪扫描条码快速查询到订单信息

2.解决

1.通过将数据转化为html后调用qt进行打印,打印机无法打印img标签中的条码

2.通过将数据转化为html,将条码直接绘制的方法实现了需求

3.代码

Barcode.h

// Barcode.h: interface for the CBarcode class.
//
//  Copyright 2002 Neil Van Eps
//
//////////////////////////////////////////////////////////////////////
#ifndef BARCODE_H
#define BARCODE_H
#include <QString>
#include <QPainter>

// bblib.h
//  must put build number define on third line
#define BUILDNUM 7
//
//
//  Copyright 2002 Neil Van Eps
//

enum Symbology
{
    RATIONALCODABAR,
    TRADITIONALCODABAR,
    I2OF5,
    CODE39,
    COD128,
    UPCEAN,
    CODE93
};

#define     COLORWHITE  0x00FFFFFF
#define     COLORBLACK  0x00000000

class CBarcode  
{
public:
    CBarcode();
    void    LoadData(QString csMessage, const QString &strImagePath, double dNarrowBar, double dFinalHeight, int nStartingXPixel, int nStartingYPixel, double dRatio = 1.0);
    virtual void DrawBitmap(QPainter *painter = nullptr) = 0;
    //virtual void BitmapToClipboard() = 0;
    virtual ~CBarcode();
    long    GetBarcodePixelWidth();
    long    GetBarcodePixelHeight();
protected:
    QString m_csMessage;
    //HDC       m_hDC;     
    QString m_strImagePath;
    long    m_nFinalBarcodePixelWidth;
    long    m_nNarrowBarPixelWidth;
    long    m_nPixelHeight;
    long    m_nStartingXPixel;
    long    m_nStartingYPixel;
    long    m_nSymbology;
    long    m_nWideBarPixelWidth;
    virtual void DrawPattern(QString csPattern, QPainter *painter) = 0;
};

#endif // !defined(AFX_BARCODE_H__C5D7FCDA_5C8F_4244_AF95_33D6FA93F8EB__INCLUDED_)

Barcode.cpp

// Barcode.cpp: implementation of the CBarcode class.
//
//  Copyright 2002 Neil Van Eps
//
//////////////////////////////////////////////////////////////////////
#include "Barcode.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CBarcode::CBarcode()
{

}

CBarcode::~CBarcode()
{

}

////////////////////////////////////////////////////////////////////////////////////
//
//  Name:
//      LoadData()
//
//  Description:
//
//
//  Arguments:
//
//
//  Return:
//
//
//  Called by:
//
//
////////////////////////////////////////////////////////////////////////////////////
void CBarcode::LoadData(QString csMessage, const QString &strImagePath, double dNarrowBar, double dFinalHeight, int nStartingXPixel, int nStartingYPixel, double dRatio)
{
    int     i, nXAxisDpi, nYAxisDpi, nTemp;
    char    c;

    // values that can be saved without translation
    m_csMessage = csMessage;
    m_nStartingXPixel = nStartingXPixel;//nStartingXPixel*10;
    m_nStartingYPixel = nStartingYPixel;//nStartingYPixel*10;
    //m_hDC = pDC;
    m_strImagePath = strImagePath;

    // load the final attributes that depend on the device context
    m_nPixelHeight = (int)dFinalHeight;//(int)((10*dFinalHeight) + 0.5);
    m_nNarrowBarPixelWidth = (int)(dNarrowBar + 0.5);
    m_nWideBarPixelWidth = (int)(dRatio*m_nNarrowBarPixelWidth);

    switch (m_nSymbology)
    {
    case CODE39:
        // get final character width
        //nTemp = m_csMessage.GetLength() + 2;
        nTemp = m_csMessage.length() + 2;

        // add message
        m_nFinalBarcodePixelWidth = nTemp * ((3 * m_nWideBarPixelWidth) + (7 * m_nNarrowBarPixelWidth));

        break;
    case COD128:
        // get final character width
        //nTemp = m_csMessage.GetLength();
        nTemp = m_csMessage.length();

        m_nFinalBarcodePixelWidth = ((nTemp * 11) + 35)*m_nNarrowBarPixelWidth;

        break;
    case CODE93:
        // get final character width
        //nTemp = m_csMessage.GetLength();
        nTemp = m_csMessage.length();

        m_nFinalBarcodePixelWidth = (((nTemp + 4) * 9) + 1)*m_nNarrowBarPixelWidth;
    case UPCEAN:
    case TRADITIONALCODABAR:
        break;
    }

    return;
}
long CBarcode::GetBarcodePixelHeight()
{
    return m_nPixelHeight;
}

long CBarcode::GetBarcodePixelWidth()
{
    return m_nFinalBarcodePixelWidth;
}

Code39.h

// Code39.h: interface for the CCode39 class.
//
//  Copyright 2002 Neil Van Eps
//
//////////////////////////////////////////////////////////////////////
#ifndef CODE39_H
#define CODE39_H

#include "Barcode.h"

class CCode39 : public CBarcode  
{
public:
    //void BitmapToClipboard();
    void DrawBitmap(QPainter *painter = nullptr);
    CCode39();
    virtual ~CCode39();
private:
    void    DrawPattern(QString csPattern, QPainter *painter);
    QString RetrievePattern(char c);

};

#endif // !defined(AFX_CODE39_H__6FE17747_EADF_4E89_9DCF_7688B04897BC__INCLUDED_)

Code39.cpp

// Code39.cpp: implementation of the CCode39 class.
//
//  Copyright 2002 Neil Van Eps
//
//////////////////////////////////////////////////////////////////////
#include "Code39.h"
#include <QPainter>
#include <QImage>
#include <QPen>
#include <QChar>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CCode39::CCode39()
{
    // code 39
    m_nSymbology = CODE39;
}

CCode39::~CCode39()
{

}

////////////////////////////////////////////////////////////////////////////////////
//
//  Name:
//      DrawBitmap()
//
//  Description:
//      draws a barcode using the previously loaded data
//
//  Arguments:
//      none
//
//  Return:
//      void
//
//  Called by:
//      public class interface
//
////////////////////////////////////////////////////////////////////////////////////
//这里有一个问题 我怎么搞到图片大小呢
void CCode39::DrawBitmap(QPainter *painter)
{
    int     i;
    QString csCurrentPattern;
    DrawPattern(RetrievePattern(‘*‘), painter);

    // draw each character in the message
    for (i=0;i<m_csMessage.length();i++)
        DrawPattern(RetrievePattern(m_csMessage.at(i).toLatin1()), painter);

    // draw stop character, also an asterisk
    DrawPattern(RetrievePattern(‘*‘), painter);
    return;
}

////////////////////////////////////////////////////////////////////////////////////
//
//  Name:
//      DrawPattern()
//
//  Description:
//      draws the passed character pattern at the end of the barcode
//
//  Arguments:
//      CString     csPattern   -   the bar pattern to draw
//
//  Return:
//      void
//
//  Called by:
//      CRationalCodabar::DrawBitmap()
//
////////////////////////////////////////////////////////////////////////////////////

void CCode39::DrawPattern( QString csPattern ,QPainter *painter)
{
    int         i, nXPixel, nYPixel, nTempWidth;

    QPen penbar = QPen(Qt::black);
    penbar.setWidth(1);
    penbar.setStyle(Qt::SolidLine);
    QPen penspace = QPen(Qt::white);
    penspace.setWidth(1);
    penspace.setStyle(Qt::SolidLine);
    painter->setRenderHint(QPainter::Antialiasing, true);
    // initialize X pixel value
    nXPixel = m_nStartingXPixel;

    for (i = 0; i < csPattern.length(); i++)
    {
        // decide if narrow or wide bar
        if (csPattern.at(i) == L‘n‘)
            nTempWidth = m_nNarrowBarPixelWidth;
        else
            nTempWidth = m_nWideBarPixelWidth;

        // X value for loop
        for (nXPixel = m_nStartingXPixel; nXPixel < m_nStartingXPixel + nTempWidth; nXPixel++)
        {
            if (i % 2 == 0)
                painter->setPen(penbar);
            else
                painter->setPen(penspace);
            painter->drawLine(nXPixel, m_nStartingYPixel, nXPixel, m_nStartingYPixel + m_nPixelHeight);
        }

        // advance the starting position
        m_nStartingXPixel += nTempWidth;
    }

    //
    //oDC.SelectObject(hPenOld);

    //::DeleteObject(hPenBar);
    //::DeleteObject(hPenSpace);

    // detach from the device context
    //oDC.Detach();

    return;
}

////////////////////////////////////////////////////////////////////////////////////
//
//  Name:
//      RetrievePattern()
//
//  Description:
//      retrieves the bar pattern for a given character
//
//  Arguments:
//      char    cInputCharacter -   the input character to get the bar pattern for
//
//  Return:
//      CString -   the bar pattern for the input character
//
//  Called by:
//      CRationalCodabar::DrawBitmap()
//
////////////////////////////////////////////////////////////////////////////////////

QString CCode39::RetrievePattern(char c)
{
    QString csCharPattern;

    switch (c)
    {
        case ‘1‘:
            csCharPattern = "wnnwnnnnwn";
            break;
        case ‘2‘:
            csCharPattern = "nnwwnnnnwn";
            break;
        case ‘3‘:
            csCharPattern = "wnwwnnnnnn";
            break;
        case ‘4‘:
            csCharPattern = "nnnwwnnnwn";
            break;
        case ‘5‘:
            csCharPattern = "wnnwwnnnnn";
            break;
        case ‘6‘:
            csCharPattern = "nnwwwnnnnn";
            break;
        case ‘7‘:
            csCharPattern = "nnnwnnwnwn";
            break;
        case ‘8‘:
            csCharPattern = "wnnwnnwnnn";
            break;
        case ‘9‘:
            csCharPattern = "nnwwnnwnnn";
            break;
        case ‘0‘:
            csCharPattern = "nnnwwnwnnn";
            break;
        case ‘A‘:
            csCharPattern = "wnnnnwnnwn";
            break;
        case ‘B‘:
            csCharPattern = "nnwnnwnnwn";
            break;
        case ‘C‘:
            csCharPattern = "wnwnnwnnnn";
            break;
        case ‘D‘:
            csCharPattern = "nnnnwwnnwn";
            break;
        case ‘E‘:
            csCharPattern = "wnnnwwnnnn";
            break;
        case ‘F‘:
            csCharPattern = "nnwnwwnnnn";
            break;
        case ‘G‘:
            csCharPattern = "nnnnnwwnwn";
            break;
        case ‘H‘:
            csCharPattern = "wnnnnwwnnn";
            break;
        case ‘I‘:
            csCharPattern = "nnwnnwwnnn";
            break;
        case ‘J‘:
            csCharPattern = "nnnnwwwnnn";
            break;
        case ‘K‘:
            csCharPattern = "wnnnnnnwwn";
            break;
        case ‘L‘:
            csCharPattern = "nnwnnnnwwn";
            break;
        case ‘M‘:
            csCharPattern = "wnwnnnnwnn";
            break;
        case ‘N‘:
            csCharPattern = "nnnnwnnwwn";
            break;
        case ‘O‘:
            csCharPattern = "wnnnwnnwnn";
            break;
        case ‘P‘:
            csCharPattern = "nnwnwnnwnn";
            break;
        case ‘Q‘:
            csCharPattern = "nnnnnnwwwn";
            break;
        case ‘R‘:
            csCharPattern = "wnnnnnwwnn";
            break;
        case ‘S‘:
            csCharPattern = "nnwnnnwwnn";
            break;
        case ‘T‘:
            csCharPattern = "nnnnwnwwnn";
            break;
        case ‘U‘:
            csCharPattern = "wwnnnnnnwn";
            break;
        case ‘V‘:
            csCharPattern = "nwwnnnnnwn";
            break;
        case ‘W‘:
            csCharPattern = "wwwnnnnnnn";
            break;
        case ‘X‘:
            csCharPattern = "nwnnwnnnwn";
            break;
        case ‘Y‘:
            csCharPattern = "wwnnwnnnnn";
            break;
        case ‘Z‘:
            csCharPattern = "nwwnwnnnnn";
            break;
        case ‘-‘:
            csCharPattern = "nwnnnnwnwn";
            break;
        case ‘.‘:
            csCharPattern = "wwnnnnwnnn";
            break;
        case ‘ ‘:
            csCharPattern = "nwwnnnwnnn";
            break;
        case ‘*‘:
            csCharPattern = "nwnnwnwnnn";
            break;
        case ‘$‘:
            csCharPattern = "nwnwnwnnnn";
            break;
        case ‘/‘:
            csCharPattern = "nwnwnnnwnn";
            break;
        case ‘+‘:
            csCharPattern = "nwnnnwnwnn";
            break;
        case ‘%‘:
            csCharPattern = "nnnwnwnwnn";
            break;
    }

    return csCharPattern;
}

////////////////////////////////////////////////////////////////////////////////////
//
//  Name:
//      BitmapToClipboard()
//
//  Description:
//      puts the specified bitmap on the clipboard
//
//  Arguments:
//      none
//
//  Return:
//      void
//
//  Called by:
//      public class interface - called by users of the class
//
////////////////////////////////////////////////////////////////////////////////////
#if 0
void CCode39::BitmapToClipboard()
{
    CDC                 memDC;
    CBitmap             oBitmap;

    memDC.CreateCompatibleDC(NULL);

    m_hDC = memDC.GetSafeHdc();

    // create compatible, correctly sized bitmap
    oBitmap.CreateCompatibleBitmap(&memDC,m_nFinalBarcodePixelWidth,m_nPixelHeight);

    // select our bitmap into the device context
    CBitmap * oldbm = memDC.SelectObject(&oBitmap);

    // turn area white - stock black bitmap is selected
    memDC.BitBlt(0,0,m_nFinalBarcodePixelWidth,m_nPixelHeight,NULL,0,0,WHITENESS);

    // draw bitmap into memory device context
    DrawBitmap();

    // put bitmap on clipboard
    ::OpenClipboard(NULL);
    ::EmptyClipboard();
    ::SetClipboardData(CF_BITMAP, oBitmap.m_hObject);
    ::CloseClipboard();

    //  deselect object out of device context
    memDC.SelectObject(oldbm);

    // make sure bitmap not deleted with CBitmap object     
    oBitmap.Detach();

    return;
}
#endif

mytest.cpp中的调用

QPrintDialog dialog(&m_printer, this);
if (dialog.exec() == QDialog::Accepted)
{
    QPainter painter(&m_printer);
    CCode39 mycode;
    mycode.LoadData(QString("ST-1435887826818763"), QString("pos.jpg"), 1, 30, 5, 5, 3.0);
    mycode.DrawBitmap(&painter);
}

4.参考

Barcode.h Barcode.cpp Code39.h Code39.cpp是codeproject上的代码用mfc写的 我改动了下

版权声明:本文为博主原创文章,未经博主允许不得转载。

qt实现小票打印

标签:qt   小票打印   条码打印   

原文地址:http://blog.csdn.net/zhang_ruiqiang/article/details/46761165

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!