Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Otros temas > Trucos
Registrarse FAQ Miembros Calendario Guía de estilo Buscar Temas de Hoy Marcar Foros Como Leídos

Los mejores trucos

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 25-08-2008
KATODO KATODO is offline
Miembro
 
Registrado: jul 2008
Posts: 19
Poder: 0
KATODO Va por buen camino
Gestionar la Camara Web Usando AVICAP32.DLL

Hola Amigos...

Como he visto que hay varios temas donde preguntan como gestionar la camara Web y a pesar que existe un control llamado tsCap32 para Delphi, me he dado cuenta que este componente no se puede agregar a C++ Builder y menos si es Codegear, por tal razón decidi escribir una clase en C++ llamada WebCam que hereda de TPanel y gestiona la Camara Web.

Para agregar el control a un formulario debe hacerce en tiempo de ejecucion con una instruccion new.

Allí les dejo la clase y un ejemplo para que lo prueben.

La idea de esto seria que siguieran agregando funcionalidad a la clase y tal vez crear un componente y publicarlo en sourceforge.net

PD: Por cierto funciona tanto para Windows Xp como para Windows Vista y el proyecto fue desarrollado en CodeGear RAD Studio 2007, pero pueden agregarla facilmente a cualquier version de C++ Builder.

Aquí dejo el código de la Clase

webcam.h
Código:
//---------------------------------------------------------------------------
#ifndef webcamH
#define webcamH

#include <vcl.h>
#include <vfw.h>
class WebCam : public TPanel{
	HWND hWndCam;
	int device;
	bool grabbing;
	public:
		_fastcall WebCam(TComponent* Owner) : TPanel(Owner), hWndCam(0), device(0), grabbing(false) {};
		_fastcall ~WebCam(){ closeVideoCam(); };
		AnsiString __fastcall getDeviceName(int dev);
		bool __fastcall openVideoCam(int dev);
		bool __fastcall closeVideoCam();
		bool __fastcall saveAsBMP(AnsiString fileName);
		bool __fastcall saveAsJPG(AnsiString fileName);
		bool __fastcall startCaptureAVI(AnsiString fileName);
		bool __fastcall stopCaptureAVI();
};
//---------------------------------------------------------------------------
#endif
WebCam.cpp
Código:
//---------------------------------------------------------------------------


#pragma hdrstop

#include "webcam.h"
#include <stdio.h>
#include <jpeg.hpp>

//---------------------------------------------------------------------------

#pragma package(smart_init)
////////////////////////////////////////////////////////////////////////////////
//Retorna el nombre del dispositivo
AnsiString __fastcall WebCam::getDeviceName(int dev){
	char name[100], description[100];
	bool result;

	result=capGetDriverDescriptionA(dev,name,100,description,100);

	if (!result)
		return AnsiString("");

	return AnsiString(name)+AnsiString(description);
}
////////////////////////////////////////////////////////////////////////////////
//Abre la camara en modo de video
bool __fastcall WebCam::openVideoCam(int dev){

	if(!closeVideoCam()) return false;
	
	hWndCam = capCreateCaptureWindowA("My Own Capture Window", WS_CHILD | WS_VISIBLE ,
									0, 0, Width, Height,
									Handle, dev);
	if (hWndCam){
		device = dev;
		SendMessage(hWndCam, WM_CAP_DRIVER_CONNECT, device, 0);
		SendMessage(hWndCam,WM_CAP_SET_PREVIEWRATE,30,0);
		SendMessage(hWndCam, WM_CAP_SET_PREVIEW,(WPARAM)true,0);
		capPreviewScale(hWndCam,true);

		return true;
	}

	return false;
}
////////////////////////////////////////////////////////////////////////////////
//Cierra la camara
bool __fastcall WebCam::closeVideoCam(){
	bool result = true;

	if(grabbing)
		stopCaptureAVI();
		
	if (hWndCam){
		result = SendMessage(hWndCam, WM_CAP_DRIVER_DISCONNECT, 0, 0);

		if(result){
			hWndCam = 0;
			device = 0;
			grabbing = false;
		}

	}

	return result;
}
////////////////////////////////////////////////////////////////////////////////
//Salva un snapshot como BMP
bool __fastcall WebCam::saveAsBMP(AnsiString fileName){
	if (hWndCam){
     	fileName = ChangeFileExt(fileName, ".bmp");
		return SendMessage(hWndCam, WM_CAP_FILE_SAVEDIB, 0, (long)fileName.c_str());
	}

	return false;
}
////////////////////////////////////////////////////////////////////////////////
//Salva un snapshot en un formato especifico
bool __fastcall WebCam::saveAsJPG(AnsiString fileName){
	Graphics::TBitmap *pic;
	TJPEGImage *jpg;
	AnsiString tempFile;

	if (hWndCam){
		tempFile = ExtractFilePath(fileName)+"\\image.bmp";
		if(SendMessage(hWndCam, WM_CAP_FILE_SAVEDIB, 0, (long)tempFile.c_str())){
			pic = new Graphics::TBitmap();
			jpg = new TJPEGImage();
			pic->LoadFromFile(tempFile);
			fileName = ChangeFileExt(fileName, ".jpg");
			jpg->Assign(pic);
			jpg->SaveToFile(fileName);
			pic->Free();
			jpg->Free();
			remove(tempFile.c_str());
			return true;
		}
	}
	return false;
}
////////////////////////////////////////////////////////////////////////////////
//Inicia la grabación de video en formato AVI
bool __fastcall WebCam::startCaptureAVI(AnsiString fileName){
	bool result = false;
	CAPTUREPARMS capParams;

	if(grabbing)
		stopCaptureAVI();

	if (hWndCam){

		result = SendMessage(hWndCam, WM_CAP_FILE_SET_CAPTURE_FILE, 0, (long)fileName.c_str());

		if(result){
			capCaptureGetSetup(hWndCam,&capParams, sizeof(CAPTUREPARMS));
			capParams.fYield = true;
			capParams.fAbortLeftMouse = false;
			capParams.fAbortRightMouse = true;
			capCaptureSetSetup(hWndCam,&capParams, sizeof(CAPTUREPARMS));
			result = SendMessage(hWndCam, WM_CAP_SEQUENCE, 0, 0);
		}
	}

	grabbing = result;
	return  result;
}
////////////////////////////////////////////////////////////////////////////////
//Detiene la grabación de video
bool __fastcall WebCam::stopCaptureAVI(){
	bool result = false;

	if (hWndCam && grabbing) {
		result = SendMessage(hWndCam, WM_CAP_STOP, 0, 0);
		if(result) grabbing = false;
	}

	return result;
}
Aquí dejo un ejemplo en C++Builder Codegear RAD Studio 2007:

[HTML]
<div style="padding:6px">








<fieldset class="fieldset">
<legend>Archivos Adjuntos</legend>
<table cellpadding="0" cellspacing="3" border="0">
<tr>
<td><img class="inlineimg" src="images/attach/zip.gif" alt="Tipo de Archivo: zip" width="16" height="16" border="0" style="vertical-align:baseline" /></td>
<td><a href="attachment.php?attachmentid=1344&amp;d=1219525186">WebCam.zip</a> (8,8 KB, 4 visitas)</td>
</tr>
</table>
</fieldset>




</div>
[/HTML]
Responder Con Cita
  #2  
Antiguo 25-08-2008
KATODO KATODO is offline
Miembro
 
Registrado: jul 2008
Posts: 19
Poder: 0
KATODO Va por buen camino
Upsss. parece que no funcionó la etiqueta HTML.

Aqui dejo el link del ejemplo:

http://www.clubdelphi.com/foros/attachment.php?attachmentid=1344&d=1219525186
Responder Con Cita
  #3  
Antiguo 24-10-2008
Avatar de jhonny
jhonny jhonny is offline
Jhonny Suárez
 
Registrado: may 2003
Ubicación: Colombia
Posts: 7.058
Poder: 29
jhonny Va camino a la famajhonny Va camino a la fama
Muy interesante, gracias KATODO.
Responder Con Cita
Respuesta


Herramientas Buscar en Tema
Buscar en Tema:

Búsqueda Avanzada
Desplegado

Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro


La franja horaria es GMT +2. Ahora son las 14:28:40.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi