Webcam, c++



  • Hallo bin neu in der Programierung und habe dein ein Problem wo ich nicht weiter komme
    ich habe diesen Code im www gefunden

    #include <vcl.h>
    #pragma hdrstop

    #include <stdio.h>
    #include <clipbrd.hpp>
    #include <algorith.h>
    #include "c_cap.h"

    // ---------------------------------------------------------------------------
    #pragma package(smart_init)

    TCap *pCap = NULL;

    LRESULT PASCAL cap_CallbackProc(HWND hWnd, LPVIDEOHDR lpVHdr);

    // ---------------------------------------------------------------------------
    // Callback
    // ---------------------------------------------------------------------------
    LRESULT PASCAL cap_CallbackProc(HWND hWnd, LPVIDEOHDR lpVHdr) {
    int vfs;
    int xx, yy, ix = 0;
    unsigned char *pByte;
    void *pData;
    BITMAPINFO BmpInfo;

    if (!pCap)
    return(-1);

    capGetVideoFormat(pCap->hwndVideo, &BmpInfo, sizeof(BITMAPINFO));
    pCap->OnFrame();
    SendMessage(pCap->ParentHandle, UM_CAPONFRAME, 0, 0);
    return 0;
    }

    // ---------------------------------------------------------------------------
    // Constructor
    // ---------------------------------------------------------------------------
    __fastcall TCap::TCap(HWND Handle) {
    RECT Rect;

    pCap = this;
    pBitMapFrame = NULL;
    // create video capture window
    ParentHandle = Handle;

    GetClientRect(ParentHandle, &Rect);
    /*
    SetWindowPos(hwndVideo, NULL, 0, 0, Rect.right-Rect.left,
    Rect.bottom-Rect.top , SWP_NOZORDER | SWP_NOMOVE);
    */

    hwndVideo = capCreateCaptureWindow((LPSTR)"My Capture Window",
    WS_CHILD | WS_VISIBLE, 0, 0, Rect.right - Rect.left,
    Rect.bottom - Rect.top, (HWND)Handle, (int)1);

    capOverlay(hwndVideo, true);

    pStringCapDrivers = new TStringList;
    SelectedDevice = -1;
    }

    // ---------------------------------------------------------------------------
    // Destructor
    // ---------------------------------------------------------------------------
    __fastcall TCap::~TCap() {

    delete pStringCapDrivers;

    capPreview(hwndVideo, FALSE); // end preview
    capDriverConnect(hwndVideo, SelectedDevice);
    capDriverDisconnect(hwndVideo); // disconnect from driver
    delete pBitMapFrame;
    }

    // ---------------------------------------------------------------------------
    // enumerate the installed capture drivers
    // ---------------------------------------------------------------------------
    int TCap::EnumCapDrv() {
    char szDeviceName[80]; // driver name
    char szDeviceVersion[80]; // driver version
    AnsiString Str; // concatinated string
    int xcap; // counter

    pStringCapDrivers->Clear();
    for (xcap = 0; xcap < 10; xcap++) {
    if (capGetDriverDescription(xcap, szDeviceName, sizeof(szDeviceName), szDeviceVersion, sizeof(szDeviceVersion))) {
    Str.sprintf("%s, %s", szDeviceName, szDeviceVersion);
    pStringCapDrivers->AddObject(Str, (TObject*)xcap);
    }
    }

    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);

    capSetCallbackOnFrame(hwndVideo, cap_CallbackProc);

    capDlgVideoFormat(ParentHandle);

    // Are there new image dimensions
    capGetStatus(hwndVideo, &CapStatus, sizeof(CAPSTATUS));
    hdc = GetDC(hwndVideo);

    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);
    */
    Height = CapStatus.uiImageHeight;
    Width = CapStatus.uiImageWidth;

    // set preview rate to 33.3 miliseconds, or 30 FPS
    capPreviewRate(hwndVideo, 33.3);

    // start preview video
    capPreview(hwndVideo, TRUE);

    capPreviewScale(hwndVideo, TRUE);

    // ---- Get extended information on the capture device
    capCaptureGetSetup(hwndVideo, &CapParms, sizeof(CapParms));

    CapParms.dwRequestMicroSecPerFrame = 66667 * 0.5;
    CapParms.fYield = FALSE;
    CapParms.fCaptureAudio = FALSE;

    capCaptureSetSetup(hwndVideo, &CapParms, sizeof(CapParms));

    // ---- 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);

    if (pBitMapFrame) {
    delete pBitMapFrame;
    pBitMapFrame = NULL;
    }
    }

    // ---------------------------------------------------------------------------
    // Resize
    // ---------------------------------------------------------------------------
    void TCap::Resize() {
    RECT Rect;

    GetClientRect(ParentHandle, &Rect);

    SetWindowPos(hwndVideo, NULL, 0, 0, Rect.right - Rect.left,
    Rect.bottom - Rect.top, SWP_NOZORDER | SWP_NOMOVE);
    }

    // ---------------------------------------------------------------------------
    // Preview
    // ---------------------------------------------------------------------------
    void TCap::Preview(bool Enabled) {
    // preview video
    capPreview(hwndVideo, Enabled);
    }

    // ---------------------------------------------------------------------------
    // CaptureFrame
    // ---------------------------------------------------------------------------
    void TCap::CaptureFrame(void) {
    // preview video
    capCaptureSingleFrame(hwndVideo);
    }

    // ---------------------------------------------------------------------------
    // Get access to the video source dialog box
    // ---------------------------------------------------------------------------
    void TCap::Source() {
    capDlgVideoSource(hwndVideo);
    }

    // ---------------------------------------------------------------------------
    // capture a frame and save it
    // ---------------------------------------------------------------------------
    void TCap::SaveToFile(char *FileName) {
    capFileSaveDIB(hwndVideo, FileName);
    }

    // ---------------------------------------------------------------------------
    // CopyToClipBoard
    // ---------------------------------------------------------------------------
    void TCap::CopyToClipBoard() {
    capEditCopy(hwndVideo);
    }

    // ---------------------------------------------------------------------------
    // CopyToBitmap
    // ---------------------------------------------------------------------------
    void TCap::CopyToBitmap(Graphics::TBitmap *BitMap) {
    TClipboard *pCB = Clipboard();

    capEditCopy(hwndVideo);
    BitMap->LoadFromClipboardFormat(CF_BITMAP, pCB->GetAsHandle(CF_BITMAP), 0);
    EmptyClipboard();
    return;

    if (!pBitMapFrame) {
    pBitMapFrame = new Graphics::TBitmap;
    pBitMapFrame->Width = Width;
    pBitMapFrame->Height = Height;
    pCap->pBitMapFrame->PixelFormat = pf32bit;
    pCap->BitSize = 3;
    }

    COLORREF PixelColor;
    int xx, yy;
    int *pData;

    for (yy = 0; yy < pCap->pBitMapFrame->Height; yy++) {
    pData = (int*)pCap->pBitMapFrame->ScanLine[yy];
    for (xx = 0; xx < pCap->pBitMapFrame->Width; xx++) {
    PixelColor = GetPixel(hdc, xx, yy);
    pData[xx] = PixelColor;
    }
    }
    BitMap->Assign(pBitMapFrame);
    }

    // ---------------------------------------------------------------------------
    // virtual On Frame function to react on a new frame
    // ---------------------------------------------------------------------------
    void TCap::OnFrame(void) {

    }

    // ---------------------------------------------------------------------------
    //
    // ---------------------------------------------------------------------------
    void TCap::VideoCompression(void) {
    HIC hIC;
    try {

    /*
    hIC = ICLocate (ICTYPE_VIDEO, 1L, (LPBITMAPINFOHEADER) &hdc, NULL, ICMODE_COMPRESS);

    ICConfigure (hIC, hwndVideo);
    */
    capDlgVideoCompression(hwndVideo);
    }
    catch(...) {
    }
    }

    // ---------------------------------------------------------------------------
    //
    // ---------------------------------------------------------------------------
    void TCap::SetCaptureFile(AnsiString FileName) {
    capFileSetCaptureFile(hwndVideo, FileName.c_str());
    }

    // ---------------------------------------------------------------------------
    //
    // ---------------------------------------------------------------------------
    void TCap::Stop() {
    capCaptureStop(hwndVideo);
    }

    // ---------------------------------------------------------------------------
    //
    // ---------------------------------------------------------------------------
    void TCap::Capture() {
    CAPTUREPARMS CaptureParms;
    BITMAPINFO BitmapInfo;

    capCaptureGetSetup(hwndVideo, &CaptureParms, sizeof(CAPTUREPARMS));
    CaptureParms.fAbortLeftMouse = false;
    CaptureParms.fAbortRightMouse = false;
    // CaptureParms.vKeyAbort = NULL;
    capCaptureSetSetup(hwndVideo, &CaptureParms, sizeof(CAPTUREPARMS));

    capGetVideoFormat(hwndVideo, &BitmapInfo, sizeof(BITMAPINFO));
    capCaptureSequence(hwndVideo);
    }

    jetzt habe ich versuche alles was ich nicht brauche wegzulassen ...
    also das er ein einzelnes bild auf der festplatte speichert
    aber immer wenn ich das mache stürzt das program ab habt ihr nen Tip?


  • Mod

    Benutz einen Debugger und finde mit diesem raus, wo und warum der Absturz auftritt.



  • /*
    read_image.c: Foto mit einer Webcam machen und das Bild im PPM-Format ueber stdout ausgeben
    */
    # include <stdio.h>
    # include <unistd.h>
    # include <fcntl.h>
    # include <sys/ioctl.h>
    # include <linux/videodev.h>

    # define MAX_BYTES (640*480*3)

    unsigned char image[MAX_BYTES];

    int main()
    {
    int fd;
    long length;
    struct video_window video_win;

    if ((fd = open("/dev/video", O_RDONLY)) == -1)
    {
    perror("read_image: Can't open device");
    return(1);
    }

    if (ioctl(fd, VIDIOCGWIN, &video_win) == -1)
    {
    perror("read_image: Can't get video window");
    return(1);
    }
    length = video_win.width * video_win.height * 3;
    if (length > MAX_BYTES)
    {
    fprintf(stderr, "read_image: Image too large.\n");
    return(1);
    }

    if (read(fd, image, length) == -1)
    {
    perror("read_image: Error while reading");
    return(1);
    }

    printf("P6\n%d %d\n255\n", video_win.width,
    video_win.height);
    fwrite(image, 3, video_win.width*video_win.height,
    stdout);

    close(fd);

    return(0);

    habe es nie zum laufen bekommen aber vieleicht bekommst nen denkanstoß



  • das debbugen habe ich schon probiert aber irgendwie werde ich leider nicht draus schlau immer wenn ich ein fehler behebe habe ich nen neuen
    bin nicht so firm in c++



  • Dann lass die Finger davon.



  • Lies dir bitte mal den Thread "Du brauchst Hilfe" durch. Die Chancen stehen gut dass sich niemand zig Zeilen unformatierten Code antut, vor allem dann nicht wenn in den ersten Zeilen zu sehen ist, dass du C-Header benutzt, die in C++ seit '98 veraltet sind (<stdio.h>)



  • das selbe problem habe ich auch. Ich versuche genau das selbe komme aber auch nicht weiter


Anmelden zum Antworten