How to generate the barcode 128 in Qpixmap ?
-
This library not works https://github.com/ftylitak/qzxing, not work encoder.
-
@Mihaill said in How to generate the barcode 128 in Qpixmap ?:
_PROMIXIS_CODE128_H
Hi
There is an example included
https://github.com/promixis/Code128/blob/master/src/mainwindow.cpp
did you try it ? -
ok but you can render the scene to an image.
QImage image(fn); QPainter painter(&image); painter.setRenderHint(QPainter::Antialiasing); scene.render(&painter); image.save("file_name.png")
and you dont need to show the scene to user etc.
-
@mrjj It's dont works:
QImage image; QPainter painter(&image); painter.setRenderHint(QPainter::Antialiasing); m_Scene.render(&painter);
This works, but the first line is obtained more rest. I don't understand why. And there is a line on top. Please tell me how to remove it.
QRectF r =m_Barcode->boundingRect(); QPixmap pixmap(r.width(), r.height()); pixmap.fill(QColor(0, 0, 0, 0)); QPainter painter(&pixmap); //painter.setBrush(QBrush(QColor(0, 0, 0, 0))); painter.drawRect(r); m_Scene.render(&painter); //m_Scene.r //m_Scene.render(&painter, QRectF(), m_Barcode->sceneBoundingRect()); painter.end();
-
It's work, but the barcode is not placed all and therefore not read the picture. How do I know the size of the barcode?
m_Barcode = new Code128Item(); m_Barcode->setWidth( 200 ); m_Barcode->setHeight( 80 ); m_Barcode->setPos(0,0); m_Barcode->setText("Promixis"); m_Scene.addItem( m_Barcode ); m_Scene.update(); m_Barcode->update(); // QRectF r =m_Barcode->boundingRect(); // QPixmap pixmap(r.width(), r.height()); QPixmap pixmap(m_Barcode->boundingRect().width(), 80); pixmap.fill(QColor(0, 0, 0, 0)); QPainter painter(&pixmap); //painter.setBrush(QBrush(QColor(0, 0, 0, 0))); //painter.drawRect(r); m_Scene.render(&painter); //m_Scene.r //m_Scene.render(&painter, QRectF(), m_Barcode->sceneBoundingRect()); painter.end();
-
Hi
Im not sure what
' but the barcode is not placed all and therefore not read the picture'
means ?The rendered image is cut off ?
Anyway, did check out
void Code128Item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
in code128item.cppIt seems it would be easy to make a version that would just draw to an image as you want.
Its not really tied to QGraphicsScene and would work in a normal paintEvent. -
@mrjj Yes, the rendered image is clipped.
If you do so, the image is also cropped. Need as the otherwise set width QPixamp.
QPixmap pixmap(m_Barcode->boundingRect().width(), 80); pixmap.fill(QColor(0, 0, 0, 0)); QPainter painter(&pixmap); //painter.setBrush(QBrush(QColor(0, 0, 0, 0))); //painter.drawRect(r); // m_Scene.render(&painter); // //m_Scene.render(&painter, QRectF(), m_Barcode->sceneBoundingRect()); // painter.end(); QWidget widget; QStyleOptionGraphicsItem styleOptionGraphicsItem ; m_Barcode->paint(&painter, &styleOptionGraphicsItem, &widget); //(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
-
Hi,
Take the code inside that method and apply it on a QImage.
-
Hi
It turns out its not even using the other parameters (QStyleOptionGraphicsItem , widget) so you can directly just call it to paint on pixmap.QPixmap pix(200, 80); // match the actual values if u change them QPainter paint(&pix); pix.fill( Qt::white ); m_Barcode->paint(&paint,nullptr, nullptr); pix.save("e:/test.png");
and get a perfect sized image.
-
@Mihaill
Very strange. it works flawless here.
You have the default sizes ?m_Barcode = new Code128Item(); m_Barcode->setWidth( 200 ); m_Barcode->setHeight( 80 ); m_Barcode->setPos(0,0); m_Barcode->setText("Promixis"); QPixmap pix(200, 80); // MUST match the setWidth, setHeight QPainter paint(&pix); pix.fill( Qt::white ); m_Barcode->paint(&paint,nullptr, nullptr); pix.save("e:/test.png");
-
@Mihaill said in How to generate the barcode 128 in Qpixmap ?:
the program or draws normally, or increases in 1,25 depending on the text. Why so?
I dont know what this means ?
The images becomes bigger with bigger text ?
That is normal for barcode 128.
The data limit is normally 48 chars but its not hard limit and it will expand the barcode to the point where its too wide for pratical use. -
I understand that the number of sticks is changing. But the pixel size of the image changes by 1.25 times or 1. And the picture is cropped and the barcode is not correct.
Here 2 pictures of the same size, but one the size of the shaded zone higher in 1,25. Why so? -
@Mihaill
Hi
the programmer made it so, it seems.void Code128Item::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) { 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; } }
so it tries to fit in area and might alter the lineWidth. and hence the barcode has a different size.
try
m_Barcode->setHighDPI(true);