Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Trucos (https://www.clubdelphi.com/foros/forumdisplay.php?f=52)
-   -   Gestionar la Camara Web Usando AVICAP32.DLL (https://www.clubdelphi.com/foros/showthread.php?t=80884)

KATODO 25-08-2008 20:27:17

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]

KATODO 25-08-2008 20:31:08

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

jhonny 24-10-2008 17:34:59

Muy interesante, gracias KATODO.


La franja horaria es GMT +2. Ahora son las 14:21:32.

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