I'm using qt 5.15 i have to develop application for Code 128 barcode generator and now I want to reduce the barcode strips. How do I create customized barcode strips?
My code is below,
Code128.h
#ifndef _PROMIXIS_CODE128_H
#define _PROMIXIS_CODE128_H
#include <QString>
#include <QList>
#include <QStringList>
class Code128
{
typedef char * Symbol;
enum Symbols { SYM_CodeC=99, SYM_CodeB = 100, SYM_StartA=103, SYM_StartB, SYM_StartC, SYM_Stop, SYM_NA };
static Code128::Symbol symbols[108];
enum Mode { MODE_A, MODE_B, MODE_C, IDLE };
static Symbol symbolCode(quint8 c );
static void addSymbolCodeChar( char c, QStringList & symbols, int & checksum );
static void addSymbolCodeInt(quint8 value, QStringList & symbols, int & checksum );
static quint8 digitCount( const QString & data, int startPos );
public:
typedef QList<quint8> BarCode;
static BarCode encode( const QString & data );
};
#endif // _PROMIXIS_CODE128_H
Code128item.h
#ifndef CODE128ITEM_H
#define CODE128ITEM_H
#include <QGraphicsItem>
#include "code128.h"
class Code128Item : public QGraphicsItem
{
bool m_TextVisible;
float m_Width;
float m_Height;
QString m_Text;
Code128::BarCode m_Code;
int m_CodeLength;
bool m_HighDPI;
public:
Code128Item();
void setText ( const QString & text );
void setWidth( float width );
void setHeight( float height );
void setTextVisible( bool visible );
void setHighDPI(bool highDPI);
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget * widget);
};
#endif // CODE128ITEM_H
code128.cpp
#include "code128.h"
Code128::Symbol Code128::symbols[108] = {
"212222", "222122", "222221", "121223", "121322", "131222", "122213", "122312", "132212", "221213",
"221312", "231212", "112232", "122132", "122231", "113222", "123122", "123221", "223211", "221132",
"221231", "213212", "223112", "312131", "311222", "321122", "321221", "312212", "322112", "322211",
"212123", "212321", "232121", "111323", "131123", "131321", "112313", "132113", "132311", "211313",
"231113", "231311", "112133", "112331", "132131", "113123", "113321", "133121", "313121", "211331",
"231131", "213113", "213311", "213131", "311123", "311321", "331121", "312113", "312311", "332111",
"314111", "221411", "431111", "111224", "111422", "121124", "121421", "141122", "141221", "112214",
"112412", "122114", "122411", "142112", "142211", "241211", "221114", "413111", "241112", "134111",
"111242", "121142", "121241", "114212", "124112", "124211", "411212", "421112", "421211", "212141",
"214121", "412121", "111143", "111341", "131141", "114113", "114311", "411113", "411311", "113141",
"114131", "311141", "411131", "211412", "211214", "211232", "2331112", ""
};
Code128::Symbol Code128::symbolCode(quint8 c)
{
if ( c > SYM_Stop )
{
return symbols[107];
}
return symbols[c];
}
void Code128::addSymbolCodeChar(char c, QStringList &symbols, int &checksum)
{
// keep characters in valid range.
if ( c < 32 )
{
c = 32;
}
if ( c > 126 )
{
c = 32;
}
quint8 value = (unsigned int)c - 32;
Code128::Symbol symbol = symbolCode(value);
symbols += symbol;
checksum += value * ( symbols.count() == 1 ? 1 : symbols.count() - 1 );
}
void Code128::addSymbolCodeInt(quint8 value, QStringList &symbols, int &checksum)
{
Code128::Symbol symbol = symbolCode(value);
symbols += symbol;
checksum += value * ( symbols.count() == 1 ? 1 : symbols.count() - 1 );
}
quint8 Code128::digitCount(const QString &data, int startPos)
{
quint8 cnt = 0;
for ( ;startPos < data.length();startPos++ )
{
if ( !data.at(startPos).isDigit() )
{
break;
}
cnt++;
}
cnt &= 0xfe; // only pairs please.
return cnt;
}
Code128::BarCode Code128::encode(const QString &data)
{
QStringList symbols;
int checkSum = 0;
if ( data.length() > 100 || data.length() == 0 )
{
return BarCode();
}
int pos = 0;
Mode mode = IDLE;
while ( pos < data.length() )
{
quint8 dc = digitCount(data, pos);
if ( dc >= 4 )
{
if ( mode != MODE_B )
{
addSymbolCodeInt(SYM_CodeB, symbols, checkSum);
mode = MODE_B;
}
dc = dc>>1;
for ( int i=0; i < dc; i++ )
{
QString v = data.mid(pos,2);
int value = v.toInt();
addSymbolCodeInt(value, symbols, checkSum);
pos+=2;
}
}
else
{
if ( mode != MODE_B )
{
addSymbolCodeInt(SYM_CodeB, symbols, checkSum);
mode = MODE_B;
}
addSymbolCodeChar( data.at(pos).toLatin1(), symbols, checkSum );
pos++;
}
}
quint8 remainder = checkSum % 103;
addSymbolCodeInt(remainder, symbols, checkSum);
/* quint8 remainder = checkSum % 103;
addSymbolCodeInt(remainder, symbols, checkSum);
addSymbolCodeInt(SYM_Stop, symbols, checkSum);*/
QString code = symbols.join("");
BarCode b;
for(int i=0;i<code.length();i++)
{
QString v = code.at(i);
b << v.toInt();
}
return b;
}
code128item.cpp
#include "code128item.h"
#include <QPainter>
#include "code128.h"
Code128Item::Code128Item() :
m_TextVisible(true),
m_Width(200),
m_Height(80),
m_CodeLength(0)
{
}
void Code128Item::setText(const QString &text)
{
m_Code = Code128::encode(text);
m_Text = text;
m_CodeLength = 0;
for (int i=0;i<m_Code.length();i++)
{
m_CodeLength+=m_Code[i];
}
}
void Code128Item::setWidth(float width)
{
m_Width = width;
}
void Code128Item::setHeight(float height)
{
m_Height = height;
}
void Code128Item::setTextVisible(bool visible)
{
m_TextVisible = visible;
}
void Code128Item::setHighDPI(bool highDPI)
{
m_HighDPI = highDPI;
}
QRectF Code128Item::boundingRect() const
{
return QRectF(0,0, m_Width, m_Height);
}
void Code128Item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
float lineWidth = m_Width / m_CodeLength;
/*
* This code tries to fit the barcode right inside the width. If the code
* is too long this means that the bar width goes below one pixel. Which in
* turn means we get no barcode. On printers this is not a problem too fast
* as they have 600 DPI usually. Screens with 96 DPI run out faster.
*
*/
if ( !m_HighDPI )
{
lineWidth = qRound(lineWidth);
if ( lineWidth < 1 )
{
lineWidth = 1;
}
}
float fontHeight = painter->fontMetrics().height();
float left = 0;
for (int i=0;i<m_Code.length();i++)
{
float width = m_Code[i] * lineWidth;
if ( i % 2 == 0 )
{
QRectF bar(left, 0, width, m_Height - fontHeight );
painter->fillRect(bar, Qt::SolidPattern);
}
left+= width;
}
if ( m_TextVisible )
{
QRectF box(0, m_Height - fontHeight , left, fontHeight);
painter->drawText(box, m_Text, Qt::AlignHCenter | Qt::AlignVCenter);
}
}
MainWindow.CPP
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->graphicsView->setScene(&m_Scene);
m_Barcode = new Code128Item();
m_Barcode->setWidth( 200 );
m_Barcode->setHeight( 80 );
m_Barcode->setPos(0,0);
m_Barcode->setText("Welcome");
m_Barcode->setHighDPI(true);
m_Scene.addItem( m_Barcode );
m_Scene.update();
m_Barcode->update();
ui->lineEdit->setText("Welcome");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_lineEdit_textChanged(const QString &arg1)
{
m_Barcode->setText(arg1);
m_Barcode->update();
m_Scene.update();
}
void MainWindow::on_actionExit_triggered()
{
close();
}
I want this image format reduced barcode stripes
Please give me any suggession to make a customized barcode.
Thank you...
Code128::encode
. A minimal reproducible example would therefore be the actual and desired contents ofm_code
without all of the Qt crap around it.MODE_B
thing stands out as a likely reason why your barcode is longer than you want it to be. Up to you to decide if it is really necessary or if whatever scanner you intend to use actually can do without it.