Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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.

C#
#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;
 
	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
                        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:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~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:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		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);
                }
                //cvReleaseImage(&frame);
                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)
         {
                // typecast IplImage to Bitmap
                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)
     {
            // How to I record capturing video;
         // Please help me.
     }
 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
Posted
Updated 7-Jun-14 1:48am
v19
Comments
Sergey Alexandrovich Kryukov 4-Jun-14 0:09am    
It's hard to believe that anyone will volunteer to add any reasonable code in your so naive and low-quality code.
As a minimum, you should have tagged your language (C++/CLI, not exactly C++), ".NET" and "System.Windows.Forms".
—SA
njsa 4-Jun-14 15:39pm    
Dear Sergey
Thank you for your suggestion. Do you have any solution for my question?
Sergey Alexandrovich Kryukov 4-Jun-14 15:50pm    
Well, I've done the capture, but you will find one of the solutions from the published CodeProject articles. Just make some effort to try some out and choose the one better for you.
You probably use System.Windows.Forms, don't you? If this is your preference, you need to tag it, as well as System.Drawing. If you want an advice on preference, you should have indicated that.
—SA
njsa 4-Jun-14 16:30pm    
Dear Sergey
I use C++ programming VS2012 for my work. Before, I used opencv library for capturing and recording the captured film from camera using cvCaptureFromCamera() and cvImwrite()respectively. Currently, I would like to make a windows form application to capture and record from camera using buttons on forms.
I don't get any methods for doing it. This is the main reason that I posted a question on Codeproject?
Again, I appreciate from your suggestions.
[no name] 6-Jun-14 9:19am    
Just as an FYI, no sane person is going to download your unknown code from an unknown source, fix it for you and then give it back to you. You seem not to understand the purpose of this section of the website.

1 solution

Please see my comment to the question. It's really not good to answer again to the question answered so many times. Please see:
http://www.codeproject.com/search.aspx?q=Web+camera+capture+Forms&doctypeid=1%3b5[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900