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. QT QImage Read From Error

QT QImage Read From Error

Scheduled Pinned Locked Moved Unsolved General and Desktop
qimageqthreadsignals & slotsqfileinfo
23 Posts 5 Posters 3.4k Views
  • 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.
  • M mvsri

    @Christian-Ehrlicher
    This is the code

    while(true)
       {
           bool check = doesFilexExist(image_path);
           if(check){
               QImage image;
               bool result = image.load(image_path);
               QThread::msleep(20);
               if(!image.isNull()){
                   qDebug() << "==> Timestamp for image grabbing: " << QTime::currentTime();
    
                   emit sendImage(image);
                   QFile::remove(image_path);
                   QThread::msleep(50);
               }
           }
           else {
               // Do nothing
           }
       }
    

    @Christian-Ehrlicher said in QT QImage Read From Error:

    So you say the image reading take too long?

    I'm not so sure about that.

    The logic is very simple :
    1. Check if image exists in folder.
    2.if exists, load image
    3.emit the image
    4. delete image from the folder

    Note: 3rd party camera stores a single image in that folder

    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by J.Hilk
    #14

    @mvsri Have you thought about using QFileSystemWatcher to listen to changes in the folder,?

    No need to reinvent the wheel, you can simply react to the directoryChanged signal


    If @Christian-Ehrlicher is right, and the camera overwrites the image, than the fileChanged signal will be much handier to use.


    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.

    M 1 Reply Last reply
    0
    • Christian EhrlicherC Christian Ehrlicher

      @mvsri So are you sure the camera does not override the image while you try to load it? Please try without the camera with a static image.

      M Offline
      M Offline
      mvsri
      wrote on last edited by
      #15

      @Christian-Ehrlicher
      I think the camera does override the image when i try to load it because when i tested it with static image the code worked perfectly

      Christian EhrlicherC 1 Reply Last reply
      0
      • J.HilkJ J.Hilk

        @mvsri Have you thought about using QFileSystemWatcher to listen to changes in the folder,?

        No need to reinvent the wheel, you can simply react to the directoryChanged signal


        If @Christian-Ehrlicher is right, and the camera overwrites the image, than the fileChanged signal will be much handier to use.

        M Offline
        M Offline
        mvsri
        wrote on last edited by
        #16

        @J-Hilk said in QT QImage Read From Error:

        @mvsri Have you thought about using QFileSystemWatcher to listen to changes in the folder,?

        I haven't thought about that will try to use it now.

        1 Reply Last reply
        0
        • M mvsri

          @Christian-Ehrlicher
          I think the camera does override the image when i try to load it because when i tested it with static image the code worked perfectly

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #17

          @mvsri said in QT QImage Read From Error:

          I think the camera does override the image when i try to load it because when i tested it with static image the code worked perfectly

          So you've your explanation of the issue - Qt can't do anything against this.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          M 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @mvsri said in QT QImage Read From Error:

            I think the camera does override the image when i try to load it because when i tested it with static image the code worked perfectly

            So you've your explanation of the issue - Qt can't do anything against this.

            M Offline
            M Offline
            mvsri
            wrote on last edited by
            #18

            @Christian-Ehrlicher
            yeah i got that, but the thing is if you see the code,
            after reading the image i'm deleting the image from the folder, so the chances for image override is very less

            Christian EhrlicherC JonBJ 2 Replies Last reply
            0
            • M mvsri

              @Christian-Ehrlicher
              yeah i got that, but the thing is if you see the code,
              after reading the image i'm deleting the image from the folder, so the chances for image override is very less

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #19

              @mvsri Maybe try to rename it before you try to read it.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • B Offline
                B Offline
                Bonnie
                wrote on last edited by
                #20

                Maybe try to find out if that 3rd party camera has other API than save the image to the disk.

                1 Reply Last reply
                0
                • M mvsri

                  @Christian-Ehrlicher
                  yeah i got that, but the thing is if you see the code,
                  after reading the image i'm deleting the image from the folder, so the chances for image override is very less

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #21

                  @mvsri
                  I really don't understand what is going on! What is it that produces that image_path file? Something external? How do you know that whatever is creating it has finished writing the file and flushed it before your thread decides to read it in, such that it might read in something incomplete? :confused:

                  M 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @mvsri
                    I really don't understand what is going on! What is it that produces that image_path file? Something external? How do you know that whatever is creating it has finished writing the file and flushed it before your thread decides to read it in, such that it might read in something incomplete? :confused:

                    M Offline
                    M Offline
                    mvsri
                    wrote on last edited by
                    #22

                    @JonB said in QT QImage Read From Error:

                    @mvsri
                    I really don't understand what is going on! What is it that produces that image_path file? Something external? How do you know that whatever is creating it has finished writing the file and flushed it before your thread decides to read it in, such that it might read in something incomplete? :confused:

                    image_path is nothing but a QString which stores the path to a bmp image in a folder. the path is static it doesn't change.
                    I don't know whether the image writing is finished or not. that's why i used QFile exists to check if the file is created or not and read the image if the path exists.
                    I also have the same doubt whether i'm reading the image in an incomplete mode.

                    @Christian-Ehrlicher said in QT QImage Read From Error:

                    @mvsri Maybe try to rename it before you try to read it.

                    I'm trying it now.

                    Thank you everyone for all the help. will try to update if i found something useful.

                    JonBJ 1 Reply Last reply
                    0
                    • M mvsri

                      @JonB said in QT QImage Read From Error:

                      @mvsri
                      I really don't understand what is going on! What is it that produces that image_path file? Something external? How do you know that whatever is creating it has finished writing the file and flushed it before your thread decides to read it in, such that it might read in something incomplete? :confused:

                      image_path is nothing but a QString which stores the path to a bmp image in a folder. the path is static it doesn't change.
                      I don't know whether the image writing is finished or not. that's why i used QFile exists to check if the file is created or not and read the image if the path exists.
                      I also have the same doubt whether i'm reading the image in an incomplete mode.

                      @Christian-Ehrlicher said in QT QImage Read From Error:

                      @mvsri Maybe try to rename it before you try to read it.

                      I'm trying it now.

                      Thank you everyone for all the help. will try to update if i found something useful.

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by JonB
                      #23

                      @mvsri said in QT QImage Read From Error:

                      image_path is nothing but a QString which stores the path to a bmp image in a folder. the path is static it doesn't change.

                      I know that! My question is about the content of that file.

                      I don't know whether the image writing is finished or not. that's why i used QFile exists to check if the file is created or not and read the image if the path exists.

                      But that doesn't tell you anything about whether it has started but not finished writing to that file, does it? (Unless you are relying on Windows or something not allowing a file to satisfy "exists" until it has been closed, which I would see as dodgy in the extreme.) In which case, you will read in an incomplete image file, maybe that's why you have "black" at the bottom? At least put in a qDebug() << image.sizeInBytes() after loading it (though I'm not sure if that's reliable)....

                      QFile::remove(image_path);

                      It gets worse! This, or renaming: how do you know that at the instant you execute this the camera has not re-started writing to that file for the next capture, and you are (trying to) removing/renaming a file while it is being written anew?

                      Is your camera-image-capture-write-to-file a separate process from your code? How do you know when the capture has started/finished writing to the file?

                      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