0

I want to develop a PhotoBooth and for this, I want to display the last picture I have made with OpenCV in C++ on a Raspberry Pi. So when a new photo is in the source folder, this photo must be loaded. How can I load the newest photo?

My code until now. I load a specific image named "1.bmp", but I want to load with a non-specific name:

int main() 
{
    Mat image;
    image = imread("1.bmp");

    namedWindow( "Display", WINDOW_AUTOSIZE );
    imshow( "Display", image ); 

    waitKey(0); 
    return 0;
}

Thanks for your answers.

Chringo
  • 131
  • 2
  • 12
Michael
  • 13
  • 4
  • 1
    What have you tried so far? This question seems a little too broad, Is the "last picture made" also the newest photo in the "source folder"? Where is the source folder? Is it local, on a network share, etc? Do you want to implement some sort of folder watching or is this something that just runs once, grabs the most recent file and then displays it? Please narrow the scope of your question, or describe the specific problem you are having. – pstrjds Mar 25 '17 at 15:12
  • Thanks for your answer. I don't know how to load the newest photo in a local folder (the camera is connected to the PC and save the images on it). The last picture which is made with the camera is automatically the newest photo in the source folder. Everytime a new photo is made with the camera, this must be display. I hope now my question is better to understand. – Michael Mar 25 '17 at 15:19
  • Have you considered looking at file timestamps? The `stat` and `fstat` functions spring to mind, as well as `opendir` & `readdir`. – Jesper Juhl Mar 25 '17 at 15:28
  • Look at this thread: http://stackoverflow.com/questions/168409/how-do-you-get-a-directory-listing-sorted-by-creation-date-in-python – ZdaR Mar 25 '17 at 15:29
  • Thanks for your answers Jesper Juhl and ZdaR. I try it out. – Michael Mar 25 '17 at 15:34

1 Answers1

0

I am no C++ programmer, but this should give you a pretty good idea of how to do it:

#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>

int main()
{
   struct dirent *drnt;
   struct stat stbuf;
   DIR *dr;

   // Open the current directory 
   dr=opendir(".");
   long long newest=0;
   std::string name;

   // Iterate through directory entries 
   while (drnt=readdir(dr)){

      // stat the entry to get its type and age
      stat(drnt->d_name,&stbuf);

      // Check files only - not directories. You may want to do a strcmp() against "BMP" or "bmp" here
      if(S_ISREG(stbuf.st_mode)){
         long long ns=stbuf.st_mtimespec.tv_sec * 1000000000 + stbuf.st_mtimespec.tv_nsec;
         std::cout << drnt->d_name << ": " << ns << std::endl;

         // Note this puppy if newer than current newest
         if(ns>newest){
            newest=ns;
            name=drnt->d_name;
         }
      }
   }
   closedir(dr);

   // Output name of newest
   std::cout << name << std::endl;
}
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432