How to generate the barcode 128 in Qpixmap ?
-
@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);