SOL9 Sample: VideoWriter

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


//2017/03/30

#define _CONSOLE_

#include <sol/Pair.h>
#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>
#include <sol/opencv/OpenCVVideoWriter.h>

#include "Resource.h"

namespace SOL {

class MainView :public OpenCVVideoCaptureView {

private:
  ////////////////////////////////////////////////////////////////////////////////////////
  //Inner class starts.
  class CapturedImageView :public OpenCVImageView {
  private:
    cv::Mat capturedImage;
    
    cv::Mat& getMat()
    {
      return capturedImage;
    }
    
  public:  
    void display()
    {
      show(capturedImage);
    }
    
  public:
    CapturedImageView(View* parent, const char* name, Args& args)
    :OpenCVImageView(parent, name, args)
    {
    }
    
    void setImage(cv::Mat& image)
    {
      capturedImage = image;
    }
  };
  
  //Inner class ends.
  ////////////////////////////////////////////////////////////////////////////////////////
  
    
  SmartPtr<CapturedImageView>  capturedImageView;
  int                          fps;

  int                          ksize;
  SmartPtr<LabeledTrackBar>    ksizeTrackBar;
  FileDialog                   filedlg;
  StringT<char>                filename;
  
  OpenCVVideoWriter            writer;

  void resize(int w, int h)
  {
    if (capturedImageView) {
      capturedImageView   -> 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),
  fps(20)
  {
    fps = (int)args.get(XmNfps);
    
    try {
      Args ar;
      capturedImageView = new CapturedImageView(this, "cvwindow1", ar); 
      
      addCallback(XmNmenuCallback, IDM_SAVE, this, 
          (Callback)&MainView::save, NULL);
      
      addCallback(XmNmenuCallback, IDM_CLOSE, this, 
          (Callback)&MainView::close, NULL);
      addCallback(XmNmenuCallback, IDM_EXIT, this,
          (Callback)&MainView::confirm, NULL);

      ar.reset();
      ar.set(XmNfilter, FileDialog::getVideoFilesFilter());
      ar.set(XmNaccessMode, FileDialog::SAVE);

      filedlg.create(this, "VideoSave", ar);
      
      setImageViewSize(capturedImageView);
      
    } catch (Exception& ex) {
      caught(ex);
    }
  }

  ~MainView()
  {
    writer.close();
  }


  void close(Action& action)
  {
    char message[MAX_PATH];
    sprintf_s(message, CountOf(message), "Are you sure to close: %s?", (const char*)filename);
    int rc = MessageBox(NULL, message, "Confirmation", 
                MB_OKCANCEL|MB_ICONEXCLAMATION);
    if (rc == IDOK) {
      writer.close();
      char title[MAX_PATH];
      _stprintf_s(title, CountOf(title), _T("Closed: %s"), (const char*)filename);
      MessageBox(NULL, title, "Information", 
                MB_OK|MB_ICONEXCLAMATION);
      setText(getAppName());
    }
  }
  
  void save(Action& action)
  {
    try {
      filedlg.popup(action);
      if (action.getResult()) {
        cv::VideoCapture& cap = getVideoCapture();
        filename = filedlg.getFileName();
        
        char title[MAX_PATH*2];
        _stprintf_s(title, CountOf(title), _T("%s - %s"), (const char*)filename, getAppName());
        setText(title);
        
        writer.open(cap, (const char*)filename, fps);
      }
    } catch (SOL::Exception& ex) {
      caught(ex);
      
    } catch (...) {
      char message[MAX_PATH];
      sprintf_s(message, CountOf(message), "Failed to open VideoWriter:%s", (const char*)filename);
      MessageBox(NULL, message, "Error", 
                MB_OK|MB_ICONEXCLAMATION);
    }
  }
  
  void render()
  {
    cv::Mat frame;
    if (readVideoFrame(frame)) {
      if (!frame.empty() && capturedImageView) {
        capturedImageView -> setImage(frame);
        capturedImageView -> display();
      
        if (writer.isOpened()) {
          writer.write(frame);
        }
      }
    }
  }
};
}


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,  420);
    args.set(XmNheight, 420);
    args.set(XmNvideoDeviceIndex, 0);
    
    args.set(XmNfps,    25);
    args.set(XmNcaptureAutoStart, true);
    
    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.