Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to generate the barcode 128 in Qpixmap ?
Forum Updated to NodeBB v4.3 + New Features

How to generate the barcode 128 in Qpixmap ?

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 4 Posters 5.0k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mrjjM mrjj

    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.

    M Offline
    M Offline
    Mihaill
    wrote on last edited by Mihaill
    #11

    @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();
    

    2.png

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Mihaill
      wrote on last edited by
      #12

      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();
      
      mrjjM 1 Reply Last reply
      0
      • M Mihaill

        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();
        
        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #13

        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.cpp

        It 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.

        M 1 Reply Last reply
        0
        • mrjjM mrjj

          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.cpp

          It 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.

          M Offline
          M Offline
          Mihaill
          wrote on last edited by Mihaill
          #14

          @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)
          
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #15

            Hi,

            Take the code inside that method and apply it on a QImage.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #16

              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.

              alt text

              M 1 Reply Last reply
              1
              • mrjjM mrjj

                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.

                alt text

                M Offline
                M Offline
                Mihaill
                wrote on last edited by
                #17

                @mrjj Strange, but I have such code clips picture.2.png

                mrjjM 1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mihaill
                  wrote on last edited by
                  #18

                  The program or draws normally, or increases in 1,25 depending on the text. Why so?

                  1 Reply Last reply
                  0
                  • M Mihaill

                    @mrjj Strange, but I have such code clips picture.2.png

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #19

                    @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");
                    
                    1 Reply Last reply
                    1
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #20

                      @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.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Mihaill
                        wrote on last edited by
                        #21

                        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.
                        2.png

                        2.png
                        Here 2 pictures of the same size, but one the size of the shaded zone higher in 1,25. Why so?

                        mrjjM 1 Reply Last reply
                        0
                        • M Mihaill

                          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.
                          2.png

                          2.png
                          Here 2 pictures of the same size, but one the size of the shaded zone higher in 1,25. Why so?

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by mrjj
                          #22

                          @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);

                          alt text

                          1 Reply Last reply
                          1
                          • M Offline
                            M Offline
                            Mihaill
                            wrote on last edited by
                            #23

                            Thanks, now works fine

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved