The .avi file video player sample code.

 

# Architecture :

  1. ) CTransForm 

  2. ) IAviPlayer

  3. ) CMyAviPlayer

  4. ) myCV

  5. ) Form1

arch

 

#1. CTransForm :

  see " [ OpenCV ] Convert IplImage " - http://cuby5566.pixnet.net/blog/post/34436440

 

#2. IAviPlayer :

  a. ) Header file

 
#pragma once

using namespace System;
using namespace System::Drawing;

class IAviPlayer {

protected:
//---------------------------------------------------------
//-- Parameter --
//---------------------------------------------------------
	CvCapture* capture;
//---------------------------------------------------------
	IplImage* iplSource;
	IplImage* iplProcessed;
//---------------------------------------------------------
	char* chrFileName;
	CvSize frameSize;
//---------------------------------------------------------
	int iFps;
	int iNowFrame;
	int iTotalFrame;

//---------------------------------------------------------
//-- Function --
//---------------------------------------------------------
	virtual void imgProcess(void) = 0;

public:
//---------------------------------------------------------
//-- Function --
//---------------------------------------------------------
	IAviPlayer();
	~IAviPlayer();
//---------------------------------------------------------
	void initial(void);
	void release(void);
//---------------------------------------------------------
	void setFileName(String^ strFileName);
//---------------------------------------------------------
	void playVideo(void);
	void stopVideo(void);

//---------------------------------------------------------
//-- Inline Function --
//---------------------------------------------------------
	int getFps(void)		  { return this->iFps; }
	int getNowFrame(void)	  { return this->iNowFrame; }
	int getTotoalFrame(void)  { return this->iTotalFrame; }
//---------------------------------------------------------
	int isCapture(void)		  { return cvGrabFrame( this->capture ); }
//---------------------------------------------------------
	IplImage* getImgSrc(void) { return this->iplSource; }
	IplImage* getImgPro(void) { return this->iplProcessed; }

};
 

 

  b. ) Cpp file

 
#pragma once
#include "stdafx.h"
#include "Form1.h"
#include "AviPlayer.h"

using namespace System;
using namespace System::Drawing;

//=================================================================
//=== Constructor / Destructor ===
//=================================================================

IAviPlayer::IAviPlayer() { this->initial(); }
IAviPlayer::~IAviPlayer() { this->release(); }

//=================================================================
//=== Initial ===
//=================================================================

void IAviPlayer::initial(void) {

	this->capture = NULL;
	this->iplSource = NULL;
	this->iplProcessed = NULL;
	this->chrFileName = NULL;
//---------------------------------------------------------
	this->iFps = 0;
	this->iTotalFrame = 0;
	this->iNowFrame = 0;
//---------------------------------------------------------
	this->frameSize = cvSize( 0 , 0 );
}

//=================================================================
//=== Release ===
//=================================================================

void IAviPlayer::release(void) {

	if( this->capture != NULL )	cvReleaseCapture( & this->capture );
	if( this->iplProcessed != NULL ) cvReleaseImage( & this->iplProcessed );
}

//=================================================================
//=== Set FileName ===
//=================================================================

void IAviPlayer::setFileName(String^ strFileName) {

	this->chrFileName = CTransForm::StringToChar( strFileName );
	this->capture = cvCaptureFromAVI( this->chrFileName );
//---------------------------------------------------------
	this->iFps = (int) cvGetCaptureProperty( this->capture, CV_CAP_PROP_FPS );
	this->iTotalFrame = (int) cvGetCaptureProperty( this->capture, CV_CAP_PROP_FRAME_COUNT );
	this->iNowFrame = 0;
//---------------------------------------------------------
	this->frameSize = cvSize( (int) cvGetCaptureProperty( this->capture, CV_CAP_PROP_FRAME_WIDTH ) ,
							  (int) cvGetCaptureProperty( this->capture, CV_CAP_PROP_FRAME_HEIGHT ) );
}

//=================================================================
//=== Play Video ===
//=================================================================

void IAviPlayer::playVideo(void) {

	this->iplSource = cvRetrieveFrame( this->capture );
	this->iNowFrame = (int) cvGetCaptureProperty( this->capture , CV_CAP_PROP_POS_FRAMES );
//---------------------------------------------------------
	this->imgProcess();
}

//=================================================================
//=== Stop Video ===
//=================================================================

void IAviPlayer::stopVideo(void) {

	this->iNowFrame = 0;
	cvReleaseCapture( & this->capture );
	this->capture = cvCaptureFromAVI( this->chrFileName );	// reLoad
}
 

 

#3. CMyAviPlayer :

  a. ) Header file

 
#pragma once
#include "AviPlayer.h"

using namespace System;
using namespace System::Drawing;

class CMyAviPlayer : public IAviPlayer {

private:
//---------------------------------------------------------
//-- Function --
//---------------------------------------------------------
	void imgProcess(void);

public:
//---------------------------------------------------------
//-- Inline Function --
//---------------------------------------------------------
	CMyAviPlayer() {}
	~CMyAviPlayer() {}

};
 

 

  b. ) Cpp file

 
#pragma once
#include "stdafx.h"
#include "Form1.h"
#include "Method.h"

using namespace System;
using namespace System::Drawing;

//=================================================================
//=== Image Process ===
//=================================================================

void CMyAviPlayer::imgProcess(void) {

	if( this->iplProcessed!=NULL ) cvReleaseImage( & this->iplProcessed );
	this->iplProcessed = cvCloneImage( this->iplSource );
//---------------------------------------------------------
	IplImage* iplTemp = cvCreateImage( cvGetSize( this->iplProcessed ) , IPL_DEPTH_8U , 1 );
//---------------------------------------------------------
	cvCvtColor( this->iplProcessed , iplTemp , CV_BGR2GRAY );
	cvCvtColor( iplTemp , this->iplProcessed , CV_GRAY2RGB );
//---------------------------------------------------------
	cvReleaseImage( & iplTemp );
}
 

 

#4. myCV :

  a. ) Header file

 
#pragma once
#include "cv.h"
#include "highgui.h"
#include "TransForm.h"
#include "Method.h"

using namespace System;
using namespace System::Drawing;

class myCV {

private:
//---------------------------------------------------------
//-- Parameter --
//---------------------------------------------------------
	static CMyAviPlayer* aviPlayer;

public:
//---------------------------------------------------------
//-- Function --
//---------------------------------------------------------
	static void initial(void);
	static void release(void);
//---------------------------------------------------------
	static void setFileName(String^ strFileName);
//--------------------------------------------------------- 
	static void playVideo(void);
	static void stopVideo(void);
//---------------------------------------------------------
	static Bitmap^ getVideoFrame(void);
	static Bitmap^ getProcessFrame(void);

//---------------------------------------------------------
//-- Inline Function --
//---------------------------------------------------------
	myCV() {}
	~myCV() {}
//---------------------------------------------------------
	static int isPlaying(void)	 { return aviPlayer->isCapture(); }
//---------------------------------------------------------
	static int getInterval(void) { return 1000.0 / aviPlayer->getFps(); }
	static int getProcess(void)  { return aviPlayer->getNowFrame()*100.0/aviPlayer->getTotoalFrame(); }
};
 

 

  b. ) Cpp file

 
#pragma once
#include "stdafx.h"
#include "Form1.h"
#include "myCV.h"

using namespace System;
using namespace System::Drawing;

//=================================================================
//=== Declaration ===
//=================================================================

CMyAviPlayer* myCV::aviPlayer;

//=================================================================
//=== Initial ===
//=================================================================

void myCV::initial(void) { aviPlayer = new CMyAviPlayer(); }
void myCV::release(void) { delete aviPlayer; }

//=================================================================
//=== Action ===
//=================================================================

void myCV::setFileName(String^ strFileName) { aviPlayer->setFileName( strFileName ); }
//---------------------------------------------------------
void myCV::playVideo(void) { aviPlayer->playVideo(); }
void myCV::stopVideo(void) { aviPlayer->stopVideo(); }
//---------------------------------------------------------
Bitmap^ myCV::getVideoFrame(void)   { return CTransForm::IplimgToBitmap( aviPlayer->getImgSrc() ); }
Bitmap^ myCV::getProcessFrame(void) { return CTransForm::IplimgToBitmap( aviPlayer->getImgPro() ); }
 

 

#5. Form1 :

 

 
#pragma once
#include "myCV.h"

/*

blablabla.....

*/

//=================================================================
//=== Initial ===
//=================================================================

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {

	myCV::initial();
	this->btnPlay->Enabled = false;
}

//=================================================================
//=== Closing ===
//=================================================================

private: System::Void Form1_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e) {

	myCV::release();
}

//=================================================================
//=== OpenFileDialog ===
//=================================================================

private: System::Void fileToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {

	this->openFileDialog1->ShowDialog();
}

//=================================================================
//=== Open File OK ===
//=================================================================

private: System::Void openFileDialog1_FileOk(System::Object^  sender, System::ComponentModel::CancelEventArgs^  e) {

	myCV::setFileName( this->openFileDialog1->FileName );
	this->btnPlay->Enabled = true;
}

//=================================================================
//=== Buttun Play ===
//=================================================================

private: System::Void btnPlay_Click(System::Object^  sender, System::EventArgs^  e) {

	if( this->btnPlay->Text == "Play" )
	{
		this->btnPlay->Text = "Pause";
		this->timer1->Interval = myCV::getInterval();
		this->timer1->Enabled = true;
	}
	else
	{
		this->btnPlay->Text = "Play";
		this->timer1->Enabled = false;
	}
}

//=================================================================
//=== Timer ===
//=================================================================

private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {

	if( myCV::isPlaying() )
	{
		myCV::playVideo();
	}
	else
	{
		myCV::stopVideo();
		this->btnPlay->Text = "Play";
		this->timer1->Enabled = false;
		this->progressBar1->Value = 0;
		return;
	}
//---------------------------------------------------------
	this->pbImg->Image = ( this->chkShow->Checked ) ? myCV::getProcessFrame() : myCV::getVideoFrame();
	this->pbImg->Refresh();
	this->progressBar1->Value = myCV::getProcess();
}
 

 

 

arrow
arrow
    創作者介紹
    創作者 Cuby 56 的頭像
    Cuby 56

    Cuby56

    Cuby 56 發表在 痞客邦 留言(0) 人氣()