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 can I create image from unsigned char pointer?
Forum Updated to NodeBB v4.3 + New Features

How can I create image from unsigned char pointer?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.1k Views 1 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.
  • O Offline
    O Offline
    onurcevik
    wrote on 18 Dec 2018, 10:47 last edited by
    #1

    I have a struct like this

    struct RGBImage {
          unsigned char   *data; 
          size_t          width;
          size_t          height;
          size_t          size;  
    };
    

    And v4l2 functions to take a picture from camera. When I take a picture and save it to a file its ok but instead of saving image to file I wan't to display image on QGraphicsScene. Here is my code for it:

               auto frame = webcam.frame(); // get picture from camera in RGBImage format
               image = new QImage((char *) frame.data);
    
               QPixmap pixmap = QPixmap::fromImage(*image);
               if (pixmap.isNull())
                   qDebug()<<"pixmap is null";
    
               QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
               scene->addItem(item);
    

    But when I run this program I see that pixmap is null. How can I fix this problem? Or is there a better way to convert unsigned char * to image ?

    J J 2 Replies Last reply 18 Dec 2018, 11:23
    0
    • O onurcevik
      18 Dec 2018, 10:47

      I have a struct like this

      struct RGBImage {
            unsigned char   *data; 
            size_t          width;
            size_t          height;
            size_t          size;  
      };
      

      And v4l2 functions to take a picture from camera. When I take a picture and save it to a file its ok but instead of saving image to file I wan't to display image on QGraphicsScene. Here is my code for it:

                 auto frame = webcam.frame(); // get picture from camera in RGBImage format
                 image = new QImage((char *) frame.data);
      
                 QPixmap pixmap = QPixmap::fromImage(*image);
                 if (pixmap.isNull())
                     qDebug()<<"pixmap is null";
      
                 QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
                 scene->addItem(item);
      

      But when I run this program I see that pixmap is null. How can I fix this problem? Or is there a better way to convert unsigned char * to image ?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 18 Dec 2018, 11:23 last edited by
      #2

      @onurcevik What is the format of the picture you get?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      O 1 Reply Last reply 18 Dec 2018, 13:48
      0
      • O onurcevik
        18 Dec 2018, 10:47

        I have a struct like this

        struct RGBImage {
              unsigned char   *data; 
              size_t          width;
              size_t          height;
              size_t          size;  
        };
        

        And v4l2 functions to take a picture from camera. When I take a picture and save it to a file its ok but instead of saving image to file I wan't to display image on QGraphicsScene. Here is my code for it:

                   auto frame = webcam.frame(); // get picture from camera in RGBImage format
                   image = new QImage((char *) frame.data);
        
                   QPixmap pixmap = QPixmap::fromImage(*image);
                   if (pixmap.isNull())
                       qDebug()<<"pixmap is null";
        
                   QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
                   scene->addItem(item);
        

        But when I run this program I see that pixmap is null. How can I fix this problem? Or is there a better way to convert unsigned char * to image ?

        J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 18 Dec 2018, 11:56 last edited by J.Hilk
        #3

        @onurcevik the constructor of QImage that accepts (only) a datapointer expects a xpm image are you sure your camera image qualifies, if not this fails silently.
        Also, are you sure about the life time of the data, afaik QImage does not make deep copy of the underlying data, I run into this when I created a QImage from a local/temporary QByteArray.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        O 1 Reply Last reply 18 Dec 2018, 13:49
        1
        • J jsulm
          18 Dec 2018, 11:23

          @onurcevik What is the format of the picture you get?

          O Offline
          O Offline
          onurcevik
          wrote on 18 Dec 2018, 13:48 last edited by
          #4

          @jsulm Its an RGBImage.

          1 Reply Last reply
          0
          • J J.Hilk
            18 Dec 2018, 11:56

            @onurcevik the constructor of QImage that accepts (only) a datapointer expects a xpm image are you sure your camera image qualifies, if not this fails silently.
            Also, are you sure about the life time of the data, afaik QImage does not make deep copy of the underlying data, I run into this when I created a QImage from a local/temporary QByteArray.

            O Offline
            O Offline
            onurcevik
            wrote on 18 Dec 2018, 13:49 last edited by
            #5

            @J.Hilk Then how can I display RGBImage in QGraphicsSCene?

            J 1 Reply Last reply 18 Dec 2018, 14:00
            0
            • O onurcevik
              18 Dec 2018, 13:49

              @J.Hilk Then how can I display RGBImage in QGraphicsSCene?

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 18 Dec 2018, 14:00 last edited by J.Hilk
              #6

              @onurcevik you'll have to us one of the other constructors for example:

              http://doc.qt.io/qt-5/qimage.html#QImage-3

              but, the function will need more information, width & height as well as the expected format : http://doc.qt.io/qt-5/qimage.html#Format-enum


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              3

              1/6

              18 Dec 2018, 10:47

              • Login

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