000
06.05.2008, 12:55 Uhr
KOR
|
Hi ich habe im internet auf www.allanpetersen.com ein Programm gefunden was einen recht einfach ein Bild von einer USB Webcam als bmp speichern lässt: unit1.cpp:
C++: |
//---------------------------------------------------------------------------
#include <vcl.h> #pragma hdrstop
#include "Unit1.h" #include "c_cap.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1;
TCap *Cap; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { int ii; TMenuItem *NewItem;
Timer1->Enabled = false; Cap = new TCap (Handle);
Cap->EnumCapDrv ();
for (ii=0; ii<Cap->pStringCapDrivers->Count; ii++) { NewItem = new TMenuItem(Options1); NewItem->Caption = Cap->pStringCapDrivers->Strings[ii]; NewItem->Tag = ii; NewItem->Checked = ii==0 ? true:false; NewItem->OnClick = MyMenyClick; Options1->Add (NewItem); } if (ii > 0 ) Cap->Connect (0); } //--------------------------------------------------------------------------- void __fastcall TForm1::MyMenyClick(TObject *Sender) { int ii; TMenuItem *MenuItem;
MenuItem = reinterpret_cast <TMenuItem *> (Sender);
if (MenuItem) { // ---- Disable rest og checked menu for (ii=0; ii<Options1->Count; ii++) { Options1->Items[ii]->Checked = false; } MenuItem->Checked = true; Cap->Connect (MenuItem->Tag); } } //--------------------------------------------------------------------------- void __fastcall TForm1::Format1Click(TObject *Sender) { Cap->Format (); } //--------------------------------------------------------------------------- void __fastcall TForm1::Source1Click(TObject *Sender) { Cap->Source (); } //--------------------------------------------------------------------------- void __fastcall TForm1::Exit1Click(TObject *Sender) { Close (); } //--------------------------------------------------------------------------- void __fastcall TForm1::Save1Click(TObject *Sender) { if (SavePictureDialog1->Execute ()) { Cap->CaptureFrame (SavePictureDialog1->FileName.c_str()); } } //--------------------------------------------------------------------------- void __fastcall TForm1::MyStuff(TObject *Sender) { Cap->CaptureFrame ("pic.bmp"); }
|
c_cap.cpp
C++: |
//---------------------------------------------------------------------------
#include <vcl.h> #pragma hdrstop
#include <stdio.h> #include "c_cap.h"
//--------------------------------------------------------------------------- #pragma package(smart_init)
__fastcall TCap::TCap (HWND Handle) { // create video capture window ParentHandle = Handle; hwndVideo = capCreateCaptureWindow( (LPSTR) "My Capture Window", WS_CHILD | WS_VISIBLE, 0, 0, 300, 200, (HWND) Handle, (int) 1);
pStringCapDrivers = new TStringList; SelectedDevice = -1; }
__fastcall TCap::~TCap () {
delete pStringCapDrivers;
capCaptureStop(hwndVideo); capPreview(hwndVideo, FALSE); // end preview capDriverConnect(hwndVideo, SelectedDevice); capDriverDisconnect(hwndVideo); // disconnect from driver capCaptureAbort(hwndVideo); }
//--------------------------------------------------------------------------- // 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->Clear (); do { if (capGetDriverDescription(xcap, szDeviceName, sizeof(szDeviceName), szDeviceVersion, sizeof(szDeviceVersion))) { sprintf (str, "%s, %s", szDeviceName, szDeviceVersion); pStringCapDrivers->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);
//-----------------------------------------------------------------------------
//**********Eigene Veränderungen an dem Header********************************* //----------------------------------------------------------------------------- //Die Standarteinstellungen laden DWORD dwSize; // Länge der Formateigenschaften BITMAPINFO pbiInfo; // Bitmap-Eigenschaften der Datei
dwSize=capGetVideoFormatSize(hwndVideo); // Messen der Länge
capGetVideoFormat(hwndVideo, &pbiInfo, dwSize); // Momentane Formatierung laden
// Einstellen der neuen Werte DWORD dwWidth,dwHeight,dwBPP;
dwWidth=640; // Horizontale Auflösung dwHeight=480; // Vertikale Auflösung dwBPP=24; // Farbtiefe
pbiInfo.bmiHeader.biWidth = dwWidth; pbiInfo.bmiHeader.biHeight = dwHeight; pbiInfo.bmiHeader.biBitCount = (WORD) dwBPP;
pbiInfo.bmiHeader.biSizeImage = 0; // Größe der reservierten Bytes pbiInfo.bmiHeader.biCompression = BI_RGB; // Komprimierung (BI_RGB=keine Kompr.) pbiInfo.bmiHeader.biClrUsed = 0; // Maximale Anzahl der dargestellten pbiInfo.bmiHeader.biClrImportant = 0; // Farben, 0=alle Farben pbiInfo.bmiHeader.biPlanes = 1; // Einstellung der Treiberebenen. Muss 1 sein
pbiInfo.bmiColors->rgbBlue = 0; // Eingabe der Farbverstärkung der pbiInfo.bmiColors->rgbGreen = 0; // Grundfarben. 0=keine Verstärkung pbiInfo.bmiColors->rgbRed = 0; pbiInfo.bmiColors->rgbReserved = 0; // Muss auf 0 gesetzt sein
//Zurückschreiben zur API capSetVideoFormat(hwndVideo, &pbiInfo, dwSize); //-----------------------------------------------------------------------------
// Die neu eingestellte Bildgröße wird in den CapStatus geladen capGetStatus(hwndVideo, &CapStatus, sizeof(CAPSTATUS));
hsize = GetSystemMetrics(SM_CYMENU); hsize += GetSystemMetrics(SM_CYCAPTION);
// Das Anzeigefenster wird neu aufgebaut 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);
// Stellt die Bildwiederholrate des Vorschaufensters ein capPreviewRate (hwndVideo, 33.3);
// startet die Bildvorschau capPreview (hwndVideo, TRUE);
// Das geöffnete Aufnahmegerät wird gespeichert 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); } //---------------------------------------------------------------------------
|
mein Problem ist ich möchte die Daten garnicht auf die Festplatte speichern ich möchte nen 2d Array haben welches die grauwerte der einzelnen pixel enthält. dann hätte ich gerne zu der GUI(die ja leider gebraucht wird) noch eine console für zb iostream(cout) etc. Vielen dank für eure Hilfe. |