Programación General > C++ Builder

 Capturar Imagen Videocamara

<< < (2/2)

touch:
Clase para captura de video con webcam , me funciona muy bien


--- Código: Text --- //---------------------------------------------------------------------------/* * Copyright (c) Allan Petersen, 2001. * This is a tutorial for a simple capture system using the win32 api * for accessing your webcam * * (c) Copyright 2001, Allan Petersen * ALL RIGHTS RESERVED * Permission to use, copy, modify, and distribute this software for * any purpose and without fee is hereby granted, provided that the above * copyright notice appear in all copies and that both the copyright notice * and this permission notice appear in supporting documentation, and that * the name of Allan Petersen not be used in advertising * or publicity pertaining to distribution of the software without specific, * written prior permission. * * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU &#34;AS-IS&#34; * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL ALLAN * PETERSEN BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT, * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF * THIRD PARTIES, WHETHER OR NOT ALLAN PETERSEN HAS BEEN * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE. * * Contact Allan Petersen at &#60;support@allanpetersen.com&#62; or visit * www.allanpetersen.com * *///--------------------------------------------------------------------------- #include &#60;vcl.h&#62;#pragma hdrstop #include &#60;stdio.h&#62;#include &#34;c_cap.h&#34; //---------------------------------------------------------------------------#pragma package(smart_init)//---------------------------------------------------------------------------LRESULT CALLBACK fcb(HWND w, LPVIDEOHDR h){        // En 'h-&#62;lpData' tenemos los datos del fotograma        // antes de ser visualizado. En este ejemplo a 320x240 24bits,        // por lo tanto la información de cada pixel estara del siguiente        // modo BLUE-GREEN-RED.... o sea, 3 bytes por cada pixel        // En 'h-&#62;dwBytesUsed' tenemos la cantidad de bytes de la imagen        // Para este ejemplo, como demostración vamos a poner la imagen        // en negativo. Aquí podriamos tanto modificar la imagen antes        // de ser vista o bien comprimir estos datos y enviarlos por la red...   typedef struct {BYTE b,g,r;} PIXEL;    char *DatosDib;    DatosDib=new char[h-&#62;dwBytesUsed];    int j=0;    for (DWORD i=0; i&#60;h-&#62;dwBytesUsed/3; i++)    {         PIXEL *pixel = &((PIXEL*)h-&#62;lpData)[i];       /* pixel-&#62;r = ~pixel-&#62;r;        pixel-&#62;g = ~pixel-&#62;g;        pixel-&#62;b = ~pixel-&#62;b; */        DatosDib[j]=pixel-&#62;r;        j++;        DatosDib[j]=pixel-&#62;g;        j++;        DatosDib[j]=pixel-&#62;b;        j++;    }//    frmTomarFoto-&#62;Image1-&#62;Assig(h-&#62;lpdata);         // devolvemos TRUE para que la función siga capturando mas fotogramas  return TRUE; }//---------------------------------------------------------------------------  __fastcall TCap::TCap (HWND Handle){    // create video capture window    ParentHandle = Handle;      hwndVideo = capCreateCaptureWindow(                    (LPSTR) &#34;0&#34;,                    WS_CHILD | WS_VISIBLE                    ,                     0, 0, 300, 200,                    (HWND) Handle,                    (int) 0);       pStringCapDrivers = new TStringList;    SelectedDevice = -1;}  __fastcall TCap::~TCap (){     delete pStringCapDrivers;     capPreview(hwndVideo, FALSE); // end preview    capDriverConnect(hwndVideo, SelectedDevice);    capDriverDisconnect(hwndVideo); // disconnect from driver} //---------------------------------------------------------------------------// enumerate the installed capture drivers//---------------------------------------------------------------------------int TCap::EnumCapDrv (){  char szDeviceName[80]; // driver name  char szDeviceVersion[80]; // driver version  char str[161]; // concatinated string  int xcap; // counter     xcap = 0;    pStringCapDrivers-&#62;Clear ();    do  {        if (capGetDriverDescription(xcap, szDeviceName, sizeof(szDeviceName),                  szDeviceVersion, sizeof(szDeviceVersion))){             sprintf (str, &#34;%s, %s&#34;, szDeviceName, szDeviceVersion);            pStringCapDrivers-&#62;AddObject (str, (TObject *)xcap);            }        else {            break;            }        xcap++;        } while (true);     return 0;} //---------------------------------------------------------------------------//  connecting to selected device and starts preview//---------------------------------------------------------------------------void TCap::Connect (int Selected){    CAPSTATUS CapStatus;    int       hsize;      // capDlgVideoDisplay(hwndVideo);      // connect to the driver    if (SelectedDevice != -1) {        capPreview (hwndVideo, FALSE);        capDriverConnect(hwndVideo, SelectedDevice);        }     if (!capDriverConnect(hwndVideo, Selected)) {        // ---- Unable to connect to driver         return;        }     // update the driver capabilities    capDriverGetCaps (hwndVideo, sizeof(CAPDRIVERCAPS), &CapDrvCaps);    capDlgVideoFormat(ParentHandle);    // Are there new image dimensions    capGetStatus(hwndVideo, &CapStatus, sizeof(CAPSTATUS));    hsize = GetSystemMetrics(SM_CYMENU);    hsize += GetSystemMetrics(SM_CYCAPTION);     // ---- Rescaling the windows    SetWindowPos(hwndVideo, NULL, 0, 0, CapStatus.uiImageWidth,                CapStatus.uiImageHeight, SWP_NOZORDER | SWP_NOMOVE);    SetWindowPos(ParentHandle, NULL, 0, hsize, CapStatus.uiImageWidth,                CapStatus.uiImageHeight+hsize, SWP_NOZORDER | SWP_NOMOVE);                          //  Cambiar formato de video a 320x240 24bits de color        // tambien podriamos ponerlo manualmente en esta        // resolución usando la función 'capDlgVideoFormat(w);'    BITMAPINFO bi;    ZeroMemory(&bi, sizeof(bi));    bi.bmiHeader.biSize        = sizeof(BITMAPINFOHEADER);    bi.bmiHeader.biWidth       = 320;    bi.bmiHeader.biHeight      = 240;    bi.bmiHeader.biPlanes      = 1;    bi.bmiHeader.biBitCount    = 24;    bi.bmiHeader.biCompression = BI_RGB;    if (!capSetVideoFormat(hwndVideo, &bi, sizeof(bi)))      throw Exception(&#34;No se puede cambiar el formato de video&#34;);        // Definimos la función que sera        // llamada antes de visualizar cada fotograma    if (!capSetCallbackOnFrame(hwndVideo, fcb))      throw Exception(&#34;callback no instalado&#34;);        // Previsualizar captura     // set preview rate to 33.3 miliseconds, or 30 FPS    capPreviewRate (hwndVideo, 33.3);     // start preview video    capPreview (hwndVideo, TRUE);     // ---- Remember selected device    SelectedDevice = Selected; } //---------------------------------------------------------------------------//  Get access to the video source format box//---------------------------------------------------------------------------void TCap::Format (){    int       hsize;     CAPSTATUS CapStatus;     capDlgVideoFormat(hwndVideo);    // Are there new image dimensions    capGetStatus(hwndVideo, &CapStatus, sizeof(CAPSTATUS));     hsize = GetSystemMetrics(SM_CYMENU);    hsize += GetSystemMetrics(SM_CYCAPTION);     SetWindowPos(ParentHandle, NULL, 0, hsize, CapStatus.uiImageWidth,                CapStatus.uiImageHeight+hsize, SWP_NOZORDER | SWP_NOMOVE);    SetWindowPos(hwndVideo, NULL, 0, 0, CapStatus.uiImageWidth,                CapStatus.uiImageHeight, SWP_NOZORDER | SWP_NOMOVE);}//---------------------------------------------------------------------------//  Get access to the video source dialog box//---------------------------------------------------------------------------void TCap::Source (){    capDlgVideoSource(hwndVideo);} //---------------------------------------------------------------------------//  capture a frame and save it//---------------------------------------------------------------------------void TCap::CaptureFrame (char *FileName){    capFileSaveDIB (hwndVideo, FileName);} 
archivo de cabecera


--- Código: Text --- //--------------------------------------------------------------------------- //#ifndef c_capH//#define c_capH//---------------------------------------------------------------------------#include &#60;vfw.h&#62; // video for windows library class TCap{private: protected:    HWND ParentHandle;    HWND hwndVideo;    CAPDRIVERCAPS CapDrvCaps; // driver capabilities     int     SelectedDevice; public:    TStringList     *pStringCapDrivers;     int EnumCapDrv();    void Connect (int Selected);    void Format ();    void Source ();    void CaptureFrame (char *FileName);  __fastcall TCap(HWND Handle);  __fastcall ~TCap(); };  //#endif  
ejemplo de funcionamiento:


--- Código: Text ---  HWND w;   w = capCreateCaptureWindow(&#34;Mi Video&#34;, WS_CHILD  ,                               1, 1, 320, 240, panelCaptura-&#62;Handle, 0);    if (!w)        throw Exception(&#34;No se pudo seleccionar una ventana de captura&#34;);        // Se conecta con el primer dispositivo de captura que encuentra    bool bRet;    for (int iCapDrv=0; iCapDrv&#60;10; iCapDrv++)    {        bRet = capDriverConnect(w, iCapDrv);        if (bRet) break;    }    if (!bRet)       {        Timer1-&#62;Enabled=false; // Desactivar Reloj        throw Exception(&#34;Error: no se encontraron dispositivos.&#34;);       }        // Mostramos en el titulo del form el nombre del driver seleccionado    char szNombreDrv[256];    /*    if ( capDriverGetName(w, szNombreDrv, sizeof(szNombreDrv)) )        Caption = szNombreDrv;      */        //  Cambiar formato de video a 320x240 24bits de color        // tambien podriamos ponerlo manualmente en esta        // resolución usando la función 'capDlgVideoFormat(w);'    BITMAPINFO bi;    ZeroMemory(&bi, sizeof(bi));    bi.bmiHeader.biSize        = sizeof(BITMAPINFOHEADER);    bi.bmiHeader.biWidth       = 320;    bi.bmiHeader.biHeight      = 240;    bi.bmiHeader.biPlanes      = 1;    bi.bmiHeader.biBitCount    = 24;    bi.bmiHeader.biCompression = BI_RGB;    if (!capSetVideoFormat(w, &bi, sizeof(bi)))      throw Exception(&#34;No se puede cambiar el formato de video&#34;);        // Definimos la función que sera        // llamada antes de visualizar cada fotograma    if (!capSetCallbackOnFrame(w, fcb))      throw Exception(&#34;callback no instalado&#34;);    capPreviewScale(w, false);    capPreviewRate(w, 1);   
ahora insercion de la funcion fcb que es llamada como el hook


--- Código: Text --- LRESULT CALLBACK fcb(HWND w, LPVIDEOHDR h){    BITMAPINFO bi;    ZeroMemory(&bi, sizeof(bi));    bi.bmiHeader.biSize        = sizeof(BITMAPINFOHEADER);    bi.bmiHeader.biWidth       = 320;    bi.bmiHeader.biHeight      = 240;    bi.bmiHeader.biPlanes      = 1;    bi.bmiHeader.biBitCount    = 24;    bi.bmiHeader.biCompression = BI_RGB;    std::auto_ptr&#60;Graphics::TBitmap&#62; bmp(new Graphics::TBitmap);    bmp-&#62;PixelFormat = pf24bit;    bmp-&#62;Width       = bi.bmiHeader.biWidth;    bmp-&#62;Height      = bi.bmiHeader.biHeight;    SetDIBits(bmp-&#62;Canvas-&#62;Handle, bmp-&#62;Handle, 0,        bi.bmiHeader.biHeight, h-&#62;lpData, &bi, DIB_RGB_COLORS);    std::auto_ptr&#60;TJPEGImage&#62; jpg(new TJPEGImage);    jpg-&#62;CompressionQuality = 50;    jpg-&#62;Assign(bmp.get());   // aqui esta elobjeto jpg, has con el lo que quieras  return TRUE&#59;} 

Navegación

[0] Índice de Mensajes

[*] Página Anterior

Ir a la versión completa