• Sábado 20 de Abril de 2024, 18:24

Autor Tema:  Aplicacion Con Camara  (Leído 1801 veces)

betitono1

  • Miembro MUY activo
  • ***
  • Mensajes: 235
    • Ver Perfil
Aplicacion Con Camara
« en: Viernes 23 de Enero de 2004, 17:57 »
0
Hola que tal alguien podria de cirme como puedo hacer una aplicacion que me muestre lo que capta la camara que tiene la PC, como si fuera un chat.,

Saludos.

_Viktor

  • Miembro MUY activo
  • ***
  • Mensajes: 271
    • Ver Perfil
    • http://AyudaCBuilder.foros.st
Re: Aplicacion Con Camara
« Respuesta #1 en: Viernes 23 de Enero de 2004, 19:08 »
0
Esto te servira ;)


Código: Text
  1.  
  2. //---------------------------------------------------------------------------
  3. /*
  4. * Copyright (c) Allan Petersen, 2001.
  5. * This is a tutorial for a simple capture system using the win32 api
  6. * for accessing your webcam
  7. *
  8. * (c) Copyright 2001, Allan Petersen
  9. * ALL RIGHTS RESERVED
  10. *
  11. * Contact Allan Petersen at <support@allanpetersen.com> or visit
  12. * www.allanpetersen.com
  13. *
  14. */
  15. //---------------------------------------------------------------------------
  16.  
  17. #include <vcl.h>
  18. #pragma hdrstop
  19.  
  20. #include <stdio.h>
  21. #include "c_cap.h"
  22. #include "ServerVideo.h"
  23.  
  24. //---------------------------------------------------------------------------
  25. #pragma package(smart_init)
  26.  
  27. __fastcall TCap::TCap (HWND Handle)
  28. {
  29.     // create video capture window
  30.     ParentHandle = Handle;
  31.     hwndVideo = capCreateCaptureWindow(
  32.                     (LPSTR) "My Capture Window",
  33.                     WS_CHILD | WS_VISIBLE,
  34.                     0, 0, 300, 200,
  35.                     frmServerVideo->panelCaptura->Handle,
  36.                     (int) 0);
  37.  
  38.               /*      w = capCreateCaptureWindow("Mi Video",
  39.                     WS_CHILD | WS_VISIBLE,
  40.                        1, 1, 320, 240,
  41.                        Panel1->Handle, 0); */
  42.  
  43.  
  44.     pStringCapDrivers = new TStringList;
  45.     SelectedDevice = -1;
  46. }
  47.  
  48.  
  49. __fastcall TCap::~TCap ()
  50. {
  51.  
  52.     delete pStringCapDrivers;
  53.  
  54.     capPreview(hwndVideo, FALSE); // end preview
  55.     capDriverConnect(hwndVideo, SelectedDevice);
  56.     capDriverDisconnect(hwndVideo); // disconnect from driver
  57. }
  58.  
  59. //---------------------------------------------------------------------------
  60. // enumerate the installed capture drivers
  61. //---------------------------------------------------------------------------
  62. int TCap::EnumCapDrv ()
  63. {
  64.    char szDeviceName[80]; // driver name
  65.    char szDeviceVersion[80]; // driver version
  66.    char str[161]; // concatinated string
  67.    int xcap; // counter
  68.  
  69.     xcap = 0;
  70.     pStringCapDrivers->Clear ();
  71.     do  {
  72.         if (capGetDriverDescription(xcap, szDeviceName, sizeof(szDeviceName),
  73.                            szDeviceVersion, sizeof(szDeviceVersion))){
  74.  
  75.             sprintf (str, "%s, %s", szDeviceName, szDeviceVersion);
  76.             pStringCapDrivers->AddObject (str, (TObject *)xcap);
  77.             }
  78.         else {
  79.             break;
  80.             }
  81.         xcap++;
  82.         } while (true);
  83.  
  84.     return 0;
  85. }
  86.  
  87. //---------------------------------------------------------------------------
  88. //  connecting to selected device and starts preview
  89. //---------------------------------------------------------------------------
  90. void TCap::Connect (int Selected)
  91. {
  92.     CAPSTATUS CapStatus;
  93.     int       hsize;
  94.  
  95.       // capDlgVideoDisplay(hwndVideo);
  96.       // connect to the driver
  97.     if (SelectedDevice != -1) {
  98.         capPreview (hwndVideo, FALSE);
  99.         capDriverConnect(hwndVideo, SelectedDevice);
  100.         }
  101.  
  102.     if (!capDriverConnect(hwndVideo, Selected)) {
  103.         // ---- Unable to connect to driver
  104.         return;
  105.         }
  106.  
  107.     // update the driver capabilities
  108.     capDriverGetCaps (hwndVideo, sizeof(CAPDRIVERCAPS), &CapDrvCaps);
  109.  
  110.     capDlgVideoFormat(ParentHandle);
  111.  
  112.     // Are there new image dimensions
  113.     capGetStatus(hwndVideo, &CapStatus, sizeof(CAPSTATUS));
  114.  
  115.     hsize = GetSystemMetrics(SM_CYMENU);
  116.     hsize += GetSystemMetrics(SM_CYCAPTION);
  117.  
  118.     // ---- Rescaling the windows
  119.     SetWindowPos(hwndVideo, NULL, 0, 0, CapStatus.uiImageWidth,
  120.                 CapStatus.uiImageHeight, SWP_NOZORDER | SWP_NOMOVE);
  121.     SetWindowPos(ParentHandle, NULL, 0, hsize, CapStatus.uiImageWidth,
  122.                 CapStatus.uiImageHeight+hsize, SWP_NOZORDER | SWP_NOMOVE);
  123.  
  124.     // set preview rate to 33.3 miliseconds, or 30 FPS
  125.     capPreviewRate (hwndVideo, 33.3);
  126.  
  127.     // start preview video
  128.     capPreview (hwndVideo, TRUE);
  129.  
  130.     // ---- Remember selected device
  131.     SelectedDevice = Selected;
  132. }
  133.  
  134. //---------------------------------------------------------------------------
  135. //  Get access to the video source format box
  136. //---------------------------------------------------------------------------
  137. void TCap::Format ()
  138. {
  139.     int       hsize;
  140.  
  141.     CAPSTATUS CapStatus;
  142.  
  143.     capDlgVideoFormat(hwndVideo);
  144.     // Are there new image dimensions
  145.     capGetStatus(hwndVideo, &CapStatus, sizeof(CAPSTATUS));
  146.  
  147.     hsize = GetSystemMetrics(SM_CYMENU);
  148.     hsize += GetSystemMetrics(SM_CYCAPTION);
  149.  
  150.     SetWindowPos(ParentHandle, NULL, 0, hsize, CapStatus.uiImageWidth,
  151.                 CapStatus.uiImageHeight+hsize, SWP_NOZORDER | SWP_NOMOVE);
  152.     SetWindowPos(hwndVideo, NULL, 0, 0, CapStatus.uiImageWidth,
  153.                 CapStatus.uiImageHeight, SWP_NOZORDER | SWP_NOMOVE);
  154. }
  155. //---------------------------------------------------------------------------
  156. //  Get access to the video source dialog box
  157. //---------------------------------------------------------------------------
  158. void TCap::Source ()
  159. {
  160.     capDlgVideoSource(hwndVideo);
  161. }
  162.  
  163. //---------------------------------------------------------------------------
  164. //  capture a frame and save it
  165. //---------------------------------------------------------------------------
  166. void TCap::CaptureFrame (char *FileName)
  167. {
  168.     capFileSaveDIB (hwndVideo, FileName);
  169. }
  170.  
  171.  
  172.  
  173.  
  174. Archivode cabecera
  175.  
  176. Código:
  177.  
  178. //---------------------------------------------------------------------------
  179.  
  180. #ifndef c_capH
  181. #define c_capH
  182. //---------------------------------------------------------------------------
  183. #include <vfw.h> // video for windows library
  184.  
  185. class TCap
  186. {
  187. private:
  188.  
  189. protected:
  190.     HWND ParentHandle;
  191.     HWND hwndVideo;
  192.     CAPDRIVERCAPS CapDrvCaps; // driver capabilities
  193.  
  194.     int     SelectedDevice;
  195.  
  196. public:
  197.     TStringList     *pStringCapDrivers;
  198.     int EnumCapDrv();
  199.     void Connect (int Selected);
  200.     void Format ();
  201.     void Source ();
  202.     void CaptureFrame (char *FileName);
  203.   __fastcall TCap(HWND Handle);
  204.   __fastcall ~TCap();
  205. };
  206.  
  207.  
  208. #endif
  209.  
  210.  
  211.  

Saludos!
_Viktor _Yañez_
"Ser inteligente no es ser mas, solo equivocarse menos y no usar visual basic"

http]

betitono1

  • Miembro MUY activo
  • ***
  • Mensajes: 235
    • Ver Perfil
Re: Aplicacion Con Camara
« Respuesta #2 en: Sábado 28 de Febrero de 2004, 18:33 »
0
Hola gracias por la respuesta, esta aplicacion como la puedo hacer para que le envie el contenido que digitaliza la camaran hacia otra pc.

_Viktor

  • Miembro MUY activo
  • ***
  • Mensajes: 271
    • Ver Perfil
    • http://AyudaCBuilder.foros.st
Re: Aplicacion Con Camara
« Respuesta #3 en: Sábado 28 de Febrero de 2004, 19:48 »
0
Hola, este codigo que pongo a continuacion lo uso para enviar una imagen del escritorio de un pc a otro usando TServStrm y su respectivo cliente:

Código: Text
  1.  
  2.  
  3. #include <memory>
  4. #include <jpeg.hpp>
  5.  
  6. HDC dc = GetDC(0);
  7. Graphics::TCanvas *ScreenCanvas = new Graphics::TCanvas;
  8. ScreenCanvas->Handle = dc;
  9.  
  10. std::auto_ptr<TJPEGImage>JImage(new TJPEGImage);
  11. Image1->Picture->Bitmap->Width = Screen->Width;
  12. Image1->Picture->Bitmap->Height= Screen->Height;
  13. TRect rect = Rect(0,0,Screen->Width, Screen->Height);
  14. Image1->Picture->Bitmap->Canvas->CopyRect(rect,   ScreenCanvas, rect);
  15. JImage->Assign(Image1->Picture->Graphic);
  16. JImage->CompressionQuality = 19;
  17. JImage->Compress();
  18.  
  19. std::auto_ptr<TMemoryStream> Stream(new  TMemoryStream);
  20.  
  21. Jimage->SaveToStream(Stream.get());
  22. Str->PostIt(Stream.get());
  23.  
  24.  
  25.  

Y esto para recibir:

Código: Text
  1.  
  2. void __fastcall TForm1::StrmSerMSG(TComponent *Sender,
  3.       const AnsiString sFrom, TStream *strm)
  4. {
  5.     std::auto_ptr<TJPEGImage> JImage(new TJPEGImage);
  6.     JImage->LoadFromStream(strm);
  7.     Image1->Canvas->Draw(0, 0, JImage.get());
  8. }
  9.  
  10.  
 

Claro esto almacena la imagen en memoria en ningun momento se forma un archivo, si tu tienes que tomar la imagen desde un archivo solo tienes que modificar esto:

Código: Text
  1.  std::auto_ptr<TMemoryStream> Stream(new  TMemoryStream);
  2.  Jimage->SaveToStream(Stream.get());
  3.  Str->PostIt(Stream.get());
  4.  

Por esto:

 
Código: Text
  1. TFileStream *MyFStream;
  2.  MyFStream = new TFileStream("cap.jpg", fmOpenRead);
  3.  Str->PostIt(MyFStream);
  4.  MyFStream->Free();
  5.  

Si tienes alguna duda, me avisa, vere que puedo hacer...  Esto facilmente lo puedes modificar para el codigo anterior.


Saludos
_Viktor _Yañez_
"Ser inteligente no es ser mas, solo equivocarse menos y no usar visual basic"

http]