SOL9 Sample: TextDrawing

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


//2017/03/30

#define _CONSOLE_

#include <sol/ModuleFileName.h>
#include <sol/opencv/OpenCVApplicationView.h>

#include <sol/opencv/OpenCVNamedWindow.h>
#include <sol/PushButton.h>
#include <sol/opencv/OpenCVInnerView.h>

namespace SOL {

class MainView :public OpenCVApplicationView {

private:
  //Inner class starts.
  class SimpleView :public OpenCVNamedWindow {
  private:
    cv::Mat image;
    
    void display()
    {
      show(image);
    }

    void drawText(cv::Mat image, const char* string)
    {
       std::string text = string;
       int fontFace = CV_FONT_HERSHEY_COMPLEX ;//FONT_HERSHEY_SCRIPT_SIMPLEX;
       double fontScale = 2;
       int thickness    = 2;
       int baseline     = 0;
       cv::Size textSize = getTextSize(text, fontFace,
                            fontScale, thickness, &baseline);
       baseline += thickness;

       cv::Point textOrg((image.cols - textSize.width)/2,
              (image.rows + textSize.height)/2);

       putText(image, text, textOrg, fontFace, fontScale,
            Scalar::all(255), thickness, 8);
    }
    
  public:
    SimpleView(View* parent, const TCHAR* name, Args& args)
    :OpenCVNamedWindow(parent, name, args)
    {
      try {
        const char* filename = (const char*)args.get(XmNimageFileName);
        int imageLoadingFlag = args.get(XmNimageLoadingFlag);

        image = readImage(filename, imageLoadingFlag);

        drawText(image, "Hello world.");
        
      } catch (SOL::Exception ex) {
        caught(ex);
      }
    }
    
    ~SimpleView()
    {
    }

  
  };
  //Inner class ends.

  SmartPtr<SimpleView>  view;
  SmartPtr<PushButton>  quit;


  void resize(int w, int h)
  {
    if (view && quit) {
      view -> reshape(0, 0, w-100,    h);
      quit -> reshape(w-100 + 10, 10, 80, 30);      
    }
  }

  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(OpenCVApplication& applet, const TCHAR* name, Args& args)
  :OpenCVApplicationView(applet, name, args)
  {
    try {
      Args ar;
      ar.set(XmNimageFileName, "..\\images\\earth.png");
      ar.set(XmNimageLoadingFlag, CV_LOAD_IMAGE_COLOR);
      view = new SimpleView(this, "cvwindow", ar); 

      ar.reset();
      quit = new PushButton(this, "Quit", ar);
      quit -> addCallback(XmNactivateCallback, this, 
          (Callback)&MainView::confirm, NULL); 
      addCallback(XmNmenuCallback, IDM_EXIT, this,
          (Callback)&MainView::confirm, NULL);
      
    } catch (Exception& ex) {
      caught(ex);
    }
  }

  ~MainView()
  {
  }
};
}

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

    Args args;
    args.set(XmNwidth,  640);
    args.set(XmNheight, 560);
    MainView view(applet, name, args);
    view.realize();

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


Last modified: 2 Dec. 2017

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