#include <ctime>
#include <fstream>
#include <iostream>
#include <raspicam/raspicam.h>
#include <raspicam/raspicam_cv.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#define CAM_ID 0
#define MAX_CAPTURE 10000
using namespace std;using namespace cv;using namespace raspicam;
int main()
{
    cout << "Testovani kamery ..." << endl;
    VideoCapture cam_web(CAM_ID);   // Web kamera
    RaspiCam_Cv cam_rpi;            // RPi kamera
    // Zmena rozliseni u RPi kamery na 640x480 px
    cam_rpi.set(CV_CAP_PROP_FRAME_WIDTH, 640);
    cam_rpi.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
    cam_rpi.open();
    time_t timer_begin,timer_end;

    if (!cam_web.isOpened())
    {
        cout << "Web kamera nebyla pripojena." << endl;
        return -1;
    }
    if (!cam_rpi.isOpened())
    {
        cout << "RPi kamera nebyla pripojena." << endl;
        return -2;
    }
    Mat img_web;Mat img_rpi;
    cout << "Mereni RPi kamery - zahajeno" << endl;
    time ( &timer_begin );      // Start timer
    for (int i = 0; i < MAX_CAPTURE; i++)
    {
        cam_rpi.grab();                 // Porizeni snimku z RPi kamery
        cam_rpi.retrieve(img_rpi);
    }
    time ( &timer_end );        // Stop timer
    cam_rpi.release();
    cout << "Mereni RPi kamery - ukonceno" << endl;
    double secondsElapsed = difftime ( timer_end,timer_begin );
    cout<< "RPi kamera: " << secondsElapsed<<" sekund na "<< MAX_CAPTURE<<"  snimku : FPS = "<<  ( float ) ( ( float ) ( MAX_CAPTURE ) /secondsElapsed ) <<endl;
    cout << "Mereni WEB kamery - zahajeno" << endl;
    time ( &timer_begin );      // Start timer
    for (int i = 0; i < MAX_CAPTURE; i++)
    {
        cam_web >> img_web;             // Porizeni snimku z web kamery
    }
    time ( &timer_end );        // Stop timer
    cam_rpi.release();
    cout << "Mareni WEB kamery - ukonceno" << endl;
    secondsElapsed = difftime ( timer_end,timer_begin );
    cout<< "WEB kamera:" <<secondsElapsed<<" sekund na "<< MAX_CAPTURE<<"  snimku : FPS = "<<  ( float ) ( ( float ) ( MAX_CAPTURE ) /secondsElapsed ) <<endl;
    return 0;
}

