3.3.28 VideoDeviceEnumerator

In the lates OZ++ library, based on the video4linux system, we have implemented the following classes to select a video device index from a combobobox of video device names:

  • VideoDevice

  • VideoInputDeviceEnumerator

  • LabeledVideoDeviceComboBox

  • Furthermode, we have also implemented OpenCVVideoCaptureView as a subclass of OpenCVMainView of VideoCapture Application.

    OpenCVMainView
    |
    +--
    OpenCVVideoCaptureView

    The following VideoDeviceEnumerator is a simple example to select a video device name from the LabeledVideoDeviceCombobox, and open a video device corresponding the selected device name, and read a frame from the VideoCapature stream.






    //
    //VideoDeviceEnumrator.cpp
    //Copyright (c) 2017 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
    #include <oz++/motif/RowColumn.h>
    #include <oz++/motif/PushButton.h>
    #include <oz++/motif/LabeledVideoDeviceComboBox.h>
    #include <oz++/opencv/OpenCVVideoCaptureView.h>
    #include <oz++/opencv/OpenCVImageView.h>
    #include <oz++/opencv/OpenCVScaleComboBox.h>
    #include <oz++/opencv/OpenCVScrolledImageView.h>
    
    namespace OZ {
    
    class MainView :public OpenCVVideoCaptureView {
    
    private:
      //Inner class starts.
      class VideoImageView: public OpenCVImageView {
      private:
        cv::Mat image; 
        cv::Mat scaledImage;
    
      public:
        void display()
        {
          show(scaledImage);
        }
    
      public:
        VideoImageView(View* parent, const char* name, Args& args)
        :OpenCVImageView(parent, name, args)
        {
        } 
    
        void render(cv::Mat& frame, int scalingRatio)
        {
          image = frame;
          rescale(image, scaledImage, scalingRatio);
    
          invalidate();
          refresh();
        }
      };
      //Inner class ends
        
    private:
      SmartPtr<VideoImageView>  videoImage;
      SmartPtr<RowColumn>       controlPane;
    
      int                       imageScalingRatio;
      SmartPtr<OpenCVScaleComboBox> scaleComboBox;
    
      int                       deviceIndex;
    
      SmartPtr<LabeledVideoDeviceComboBox> videoDevice;
    
      SmartPtr<PushButton>     startButton;
    
      SmartPtr<PushButton>     stopButton;
    
      void resize(Dimension w, Dimension h)
      {
        int CP_WIDTH = 200;
        w = w- CP_WIDTH;
        if (videoImage && controlPane) {
          videoImage  ->resize(0, 0, w, h, 0);
          controlPane ->reshape(w+2, 0, CP_WIDTH-4, h);
        }
      }
    
      void scaleChanged(Action& action)
      {
        int val = scaleComboBox->getScale();
    
        if (val > 0 && imageScalingRatio != val) {
          imageScalingRatio = val;
          printf("ScalingRatio %d\n", val);
        }
      } 
    
      void videoDeviceChanged(Action& action)
      {
        deviceIndex = videoDevice->getSelectedPosition();
    
        printf("deviceIndex %d\n", deviceIndex);
      }
    
      void start(Action& action)
      {
         setDeviceIndex(deviceIndex);
         startCapture();
         printf("StartCapture: %d\n", deviceIndex);
      }
    
      void stop(Action& action)
      {
         stopCapture();
         printf("StopCapture\n");
      }
    
    public:
      MainView(OpenCVApplication& applet, const char* name, Args& args)
      :OpenCVVideoCaptureView(applet, name, args) 
      ,imageScalingRatio(100)
      ,deviceIndex(0)
      {
        try {
          BulletinBoard* bboard = getBulletinBoard();
          Args ar; 
          ar.reset();
          videoImage = new VideoImageView(bboard, "", ar);
    
          ar.reset();
          controlPane = new RowColumn(bboard, "", ar);
          const char* defaultScale = "100%";
    
          ar.reset();
          CompoundString scaler("Scale");
          ar.set(XmNlabelString, scaler);
          ar.set(XmNdefaultScale, defaultScale);
          scaleComboBox = new OpenCVScaleComboBox(controlPane, "", ar);
          scaleComboBox->addCallback(XmNselectionCallback, this,
            (Callback)&MainView::scaleChanged, NULL);
    
          ar.reset();
          CompoundString device("VideoDevice");
          ar.set(XmNlabelString, device);
          videoDevice = new LabeledVideoDeviceComboBox(controlPane, "", ar);
          videoDevice->addCallback(XmNselectionCallback, this,
            (Callback)&MainView::videoDeviceChanged, NULL);
    
          ar.reset();
          startButton = new PushButton(controlPane, "Start Capture", ar);
          startButton->addCallback(XmNactivateCallback, this,
            (Callback)&MainView::start, NULL);
    
          ar.reset();
          stopButton = new PushButton(controlPane, "Stop Capture", ar);
          stopButton->addCallback(XmNactivateCallback, this,
            (Callback)&MainView::stop, NULL);
    
          sendConfigureEvent(); 
    
        } catch(OZ::Exception& ex) {
          caught(ex);
        }
      }
    
      virtual void render()
      {
        try {
          cv::Mat frame;
          if (readVideoFrame(frame)) {
            if (videoImage) {
              videoImage->render(frame, imageScalingRatio);
            }
          }
        } catch (...) {
        }
      }
    };
    
    }
    
    //
    int main(int argc, char** argv) 
    {
      try {
        const char*  appclass = argv[0];
        OpenCVApplication applet(appclass, argc, argv);
    
        Args args;
        args.set(XmNwidth,  620);
        args.set(XmNheight, 400);
        //args.set(XmNvideoDeviceIndex, 0);
        args.set(XmNcaptureAutoStart, false);
        MainView view(applet, argv[0], args);
        view.realize();
    
        applet.run(&view, 20); //20 msec
        
      } catch (OZ::Exception& ex) {
        caught(ex);
      }
      return 0;
    }
    
    



    Last modified: 7 Sep. 2017

     Last modified: 7 Sep. 2017

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