SOL9 Sample: VideoPlayer

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * VideoPlayer.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


//2017/07/30

#define _CONSOLE_

#include <sol/ModuleFileName.h>
#include <sol/DropFiles.h>
#include <sol/LabeledTrackBar.h>
#include <sol/FileDialog.h>
#include <sol/opencv/OpenCVVideoApplication.h>
#include <sol/opencv/OpenCVVideoCaptureView.h>
#include <sol/opencv/OpenCVImageView.h>

namespace SOL {

class MainView :public OpenCVVideoCaptureView {

private:
  ////////////////////////////////////////////////////////////////////////////////////////
  //Inner class starts.
  class VideoImageView :public OpenCVImageView {
  private:
    cv::Mat videoImage;
    
    cv::Mat& getMat()
    {
      return videoImage;
    }
    
  public:  
    void display()
    {
      show(videoImage);
    }
    
  public:
    VideoImageView(View* parent, const char* name, Args& args)
    :OpenCVImageView(parent, name, args)
    {
    }

    void setImage(cv::Mat& image)
    {
      videoImage = image;
    }
  };
  
  //Inner class ends.
  ////////////////////////////////////////////////////////////////////////////////////////
  
    
  SmartPtr<VideoImageView>  videoImage;
  FileDialog                filedlg;
  
  void resize(int w, int h)
  {
    if (videoImage) {
      videoImage   -> reshape(0,  0,  w,   h);
    }
  }
  
  void confirm(Action& action)
  {
    int rc = MessageBox(NULL, "Are you sure to close this window?", "Confirmation", 
                MB_OKCANCEL|MB_ICONEXCLAMATION);
    if (rc == IDOK) {
      exit(action);
    }
  }
  
public:
  MainView(OpenCVVideoApplication& applet, const char* name, Args& args)
  :OpenCVVideoCaptureView(applet, name, args)
  {
    try {
      Args ar;
      videoImage = new VideoImageView(this, "cvwindow1", ar); 
      
      ar.reset();
      
      ar.set(XmNfilter, FileDialog::getVideoFilesFilter());
      filedlg.create(this, "OpenFile", ar);
      
    } catch (Exception& ex) {
      caught(ex);
    }
  }

  ~MainView()
  {
  }
  

  void open(Action& action)
  {
    try {
      filedlg.popup(action);
      if (action.getResult()) {
        
        cv::VideoCapture& cap = getVideoCapture();
        const char* filename = filedlg.getFileName();
        cap.open(filename);
        
        setImageViewSize(videoImage);
        
        char title[MAX_PATH] = {0};
        sprintf_s(title, CountOf(title), "%s - %s", filename, getAppName());
        setText(title);
      } 
    } catch (SOL::Exception& ex) {
      caught(ex);
    }
  }
  
  void render()
  {
    cv::Mat frame;
    if (readVideoFrame(frame)) {
      if (!frame.empty() && videoImage) {
        videoImage -> setImage(frame);
        videoImage -> display();
      }
    }
  }
};
}


void main(int argc, char** argv) 
{
  try {
    ModuleFileName module(argv[0]);
    
    const char*  name = module.getAppName();

    OpenCVVideoApplication applet(name, argc, argv, 30);

    Args args;
    args.set(XmNwidth,  640);
    args.set(XmNheight, 400);

    MainView view(applet, name, args);
    view.realize();

    applet.run(view);
    
  } catch (SOL::Exception& ex) {
    caught(ex);
  }
}


Last modified: 2 Dec. 2017

Copyright (c) 2017 Antillia.com ALL RIGHTS RESERVED.