For C++ version, I wrote the following code according to an open source project to capture camera with pressing on a button:
For executing code bellow, test your written project using opencv 2.4.6 in VS2012 and add header file and libraries to avoid linker errors. then, make up a win form and add bellow code to it and run.
#pragma once
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
namespace nader
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
IplImage* frame = 0;
CvCapture* capture = 0;
int cameraBusy = 0;
int cameraStatus = 0;
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
button1_Click:Click += (gcnew System::EventHandler(this, &Form1::button1_Click));
button2_Click:Click += (gcnew System::EventHandler(this, &Form1::button2_Click));
button3_Click:Click += (gcnew System::EventHandler(this, &Form1::button3_Click));
}
protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::ComponentModel::BackgroundWorker^ backgroundWorker1;
protected:
private: System::Windows::Forms::PictureBox^ pictureBox1;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::SaveFileDialog^ saveFileDialog1;
private:
System::ComponentModel::Container ^components;
<pre lang="cs">private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
}
private: System::Void backgroundWorker1_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e)
{
BackgroundWorker^ worker = dynamic_cast<BackgroundWorker^>(sender);
capture = cvCreateCameraCapture(1);
while(cameraStatus){
if (cameraBusy) continue;
frame = cvQueryFrame(capture);
if(!frame) continue;
worker->ReportProgress(1);
}
cvReleaseCapture(&capture);
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
cameraStatus = 1;
backgroundWorker1->RunWorkerAsync(10);
}
private: System::Void DrawCvImage(IplImage *CvImage,System::Windows::Forms::PictureBox^ pbx)
{
if ((pbx->Image == nullptr) || (pbx->Width != CvImage->width)||(pbx->Height != CvImage->height))
{
pbx->Width = CvImage->width;
pbx->Height = CvImage->height;
Bitmap^ bmpPicBox = gcnew Bitmap(pbx->Width, pbx->Height);
pbx->Image = bmpPicBox;
}
Graphics^g = Graphics::FromImage(pbx->Image);
Bitmap^ bmp = gcnew Bitmap(CvImage->width, CvImage->height, CvImage->widthStep,
System::Drawing::Imaging::PixelFormat::Format24bppRgb, IntPtr(CvImage->imageData));
g->DrawImage(bmp, 0, 0, CvImage->width, CvImage->height);
pbx->Refresh();
delete g;
}
private: System::Void backgroundWorker1_ProgressChanged(System::Object^ sender, System::ComponentModel::ProgressChangedEventArgs^ e)
{
if(!cameraBusy && cameraStatus)
{
cameraBusy = 1;
IplImage *destination = cvCreateImage(cvSize(640, 480),frame->depth,frame->nChannels);
cvResize(frame, destination);
DrawCvImage(destination,pictureBox1);
cvReleaseImage(&destination);
cameraBusy = 0;
}
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
cameraStatus = 0;
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e)
{
}
private: System::Void saveFileDialog1_FileOk(System::Object^ sender, System::ComponentModel::CancelEventArgs^ e) {
}
};
}
http://up.persianscript.ir/uploads/fc0a-Record-Video-NaderJSA.zip
Your comments are appreciated.
Best wishes
NaderJSA