Ejemplo n.º 1
0
//Camera_FirewireClass::~Camera_FirewireClass () {
//  ;
//}
bool Camera_FirewireClass::Connect(const wxString& camId)
{
    int CamNum, ModeNum;
    bool retval;
    wxArrayString Names;
    std::string str;
    bool debug = false;
    wxTextFile *debugfile;
    int debugstep = 0;

    if (debug) {
        debugfile = new wxTextFile(Debug.GetLogDir() + PATHSEPSTR + wxString::Format("PHD_debug_%ld.txt",wxGetLocalTime()));
        if (debugfile->Exists()) debugfile->Open();
        else debugfile->Create();
        wxDateTime now = wxDateTime::Now();
        debugfile->AddLine(wxString::Format("DEBUG %s %s  -- ", APPNAME, FULLVER) + now.FormatDate() + now.FormatTime());
    }

    try {
        if (debug) { debugfile->AddLine(wxString::Format("1: Init library")); debugfile->Write(); }
        // Init the TIS library
        if( ! DShowLib::InitLibrary( "ISB3200016679" ) ) {  // license key check
            wxMessageBox(_T("Cannot initialize ImageCapture library"),_("Error"),wxOK | wxICON_ERROR);
            return true;
        }

        if (debug) { debugfile->AddLine(wxString::Format("2: Create grabber")); debugfile->Write(); }
        m_pGrabber = new DShowLib::Grabber();

        if (debug) { debugfile->AddLine(wxString::Format("3: Find cameras")); debugfile->Write(); }
        Grabber::tVidCapDevListPtr pVidCapDevList = m_pGrabber->getAvailableVideoCaptureDevices();
        if( pVidCapDevList == 0 || pVidCapDevList->empty() ) {
            wxMessageBox(_("No camera found"));
            return true;
        }
        int NCams = (int) pVidCapDevList->size();
        if (debug) { debugfile->AddLine(wxString::Format("4: Found %d cams",NCams)); debugfile->Write(); }

        // deal with > 1 cam
        CamNum = 0;
        if (NCams > 1) {
            for( Grabber::tVidCapDevList::iterator it = pVidCapDevList->begin();
                it != pVidCapDevList->end(); ++it ) {
                Names.Add(it->toString());
            }
            CamNum = wxGetSingleChoiceIndex(_("Select Camera"),_("Camera"),Names);
            if (CamNum == -1)
                return true;
        }

        if (debug) { debugfile->AddLine(wxString::Format("5: Open Camera")); debugfile->Write(); }
        // Open camera
        retval = m_pGrabber->openDev( pVidCapDevList->at( CamNum ) );
        if (!retval) {
            wxMessageBox(_("Cannot open camera"));
            return true;
        }
        if (debug) { debugfile->AddLine(wxString(pVidCapDevList->at(CamNum).toString())); debugfile->Write(); }

        if (debug) { debugfile->AddLine(wxString::Format("6: Get Video formats")); debugfile->Write(); }
        // Get video formats
        Grabber::tVidFmtListPtr pVidFmtList = m_pGrabber->getAvailableVideoFormats();
        if ((pVidFmtList == 0) || pVidFmtList->empty()) {
            wxMessageBox(_("Cannot get list of video modes"));
            m_pGrabber->closeDev();
            return true;
        }
        int NModes = pVidFmtList->size();
        if (debug) { debugfile->AddLine(wxString::Format("7: Found %d formats",NModes)); debugfile->Write(); }

    //  Names.Clear();
    //  ModeNum = 0;
        wxString Name;
    //  if (NModes > 1) {
            ModeNum = -1;
            int i = 0;
            for( Grabber::tVidFmtList::iterator it = pVidFmtList->begin();
                it != pVidFmtList->end(); ++it, i++ ) {
    //          Names.Add(it->toString());
                Name = wxString(it->toString());
                if (debug) {debugfile->AddLine(wxString(it->toString())); debugfile->Write(); }
                if (Name.Find("Y800") != wxNOT_FOUND) {
                    ModeNum = i;
                    break;
                }
            }
            //ModeNum = wxGetSingleChoiceIndex(_T("Select Mode"),_T("Mode"),Names);
            if (ModeNum == -1) {
                wxMessageBox(_T("Cannot find a Y800 mode"));
                return true;
            }
    //  }

        if (debug) { debugfile->AddLine(wxString::Format("8: Set format %d",ModeNum)); debugfile->Write(); }
        // Set the video format
        m_pGrabber->setVideoFormat(pVidFmtList->at(ModeNum));

        // Set some more format things
        if (debug) { debugfile->AddLine(wxString::Format("9: Set FPS")); debugfile->Write(); }
    //  retval = m_pGrabber->setFPS(7.5);  // No need to run higher than this
    //  if (!retval) wxMessageBox (_T("Could not set to 7.5 FPS"));
        if (debug) { debugfile->AddLine(wxString::Format("10: Turn off auto-exposure")); debugfile->Write(); }
        retval = m_pGrabber->setProperty(CameraControl_Exposure,false);
        if (!retval) wxMessageBox (_T("Could not turn off auto-exposure"));

        // Setup the frame handler
        if (debug) { debugfile->AddLine(wxString::Format("11: Setup frame handler")); debugfile->Write(); }
        pSink = FrameHandlerSink::create(eY800, 4 );  // not sure why I even need 4...
        if (pSink == 0)
            wxMessageBox(_T("Cannot setup frame handler"));

        if (debug) { debugstep = 1; debugfile->AddLine(wxString::Format("12: Set snap mode")); debugfile->Write(); }
        pSink->setSnapMode( true );

        if (debug) { debugstep = 2; debugfile->AddLine(wxString::Format("12a: Setting SinkType")); debugfile->Write(); }
        retval = m_pGrabber->setSinkType(pSink);
        if (!retval) { wxMessageBox("Could not set sink type"); }

        // Get info I need
        if (debug) { debugstep = 3; debugfile->AddLine(wxString::Format("12b: Getting name for mode %d",ModeNum)); debugfile->Write(); }
        Name = wxString(pVidCapDevList->at(CamNum).toString());
        if (debug) { debugstep = 4; debugfile->AddLine(_T(" Name: " + Name)); debugfile->Write(); }
        if (debug) { debugstep = 5; debugfile->AddLine(wxString::Format("12c: Getting size for mode %d",ModeNum)); debugfile->Write(); }
        SIZE sz = pVidFmtList->at(ModeNum).getSize();
        if (debug) {debugstep = 6; debugfile->AddLine(_T("Size found - setting FullSize")); debugfile->Write(); }
        FullSize = wxSize((int)sz.cx, (int)sz.cy);

        if (debug) { debugstep = 7; debugfile->AddLine(wxString::Format("Image: %d %d Camera: ",FullSize.GetWidth(),FullSize.GetHeight()) + Name); debugfile->Write(); }

        // Get the stream prepared, but don't start yet - going to start/stop on each frame grab
        if (debug) { debugstep = 8; debugfile->AddLine(wxString::Format("13: Prepare Live")); debugfile->Write(); }
        retval = m_pGrabber->prepareLive(false); // not using their renderer
        if (!retval) { wxMessageBox("Could not start Live view"); }

        // Get pointer to the exposure duration functin needed
        if (debug) { debugfile->AddLine(wxString::Format("14: Get VCD properties")); debugfile->Write(); }
        tIVCDPropertyItemsPtr pItems = m_pGrabber->getAvailableVCDProperties();
        if( pItems != 0 ) {
            tIVCDPropertyItemPtr pExposureItem = pItems->findItem( VCDID_Exposure );
            tIVCDPropertyElementPtr pExposureValueElement = pExposureItem->findElement( VCDElement_Value );
            if (pExposureValueElement != 0) {
                pExposureValueElement->getInterfacePtr(m_pExposureAbs);
                if (m_pExposureAbs == 0) {
                    wxMessageBox(_("Warning - cannot directly control exposure duration - running in auto-exposure"));
                    m_pGrabber->setProperty(CameraControl_Exposure,true);
                }
                else
                    m_pExposureAbs->setValue(0.2);
            }

            tIVCDPropertyItemPtr pGainItem = pItems->findItem( VCDID_Gain );
            tIVCDPropertyElementPtr pGainValueElement = pGainItem->findElement( VCDElement_Value );
            if (pGainValueElement != 0) {
                pGainValueElement->getInterfacePtr(m_pGain);
                if (m_pGain == 0) {
                    wxMessageBox(_T("Warning - cannot directly control gain - running in auto-gain"));
    //              m_pGrabber->setProperty(CameraControl_Exposure,true);
                }
                else {
                    GainMax = m_pGain->getRangeMax();
                    long lval = (long) GuideCameraGain * GainMax / 100;
                    if (lval > m_pGain->getRangeMax())
                        lval = m_pGain->getRangeMax();
                    else if (lval < m_pGain->getRangeMin())
                        lval = m_pGain->getRangeMin();

                    m_pGain->setValue(lval);
                }
            }
        }
    } // try
    catch (...) {
        wxMessageBox(wxString::Format("Fatal error at step %d connecting to TIS camera",debugstep));
        if (debug) {  debugfile->AddLine(wxString::Format("Failed at %d",debugstep)); debugfile->Write(); debugfile->Close(); }
        return true;
    }
    if (debug) {debugfile->Write(); debugfile->Close(); delete debugfile; }
    Connected=true;
    return false;
}
Ejemplo n.º 2
0
unsigned long ICCam::PrintCameraInformation()
{
    if (!isOpen())
    {
        return (RET_FAILED | RET_CAMERA_NOT_OPEN);
    }
    // Print device name
    std::cout << "Device name: " << m_grabber->getDev().toString() << std::endl;
    std::cout << "Current video format: " << m_grabber->getVideoFormat().toString() << ".\n";
    std::cout << "Current frame rate: " << m_grabber->getFrameRate() << std::endl;

    // Print available video formats
    if( m_grabber->isVideoNormAvailableWithCurDev() )
    {
        std::cout << "\nSupported Video Norms and Formats: \n";
        Grabber::tVidNrmListPtr pVidNrmList = m_grabber->getAvailableVideoNorms();
        // List the available video formats for each video norm (PAL, NTSC,...)
        unsigned int nrmCounter = 0;
        for( Grabber::tVidNrmList::iterator itNrm = pVidNrmList->begin();
                itNrm != pVidNrmList->end();
                ++itNrm )
        {
            std::cout << "Video Norm [" << nrmCounter++ << "]: " << itNrm->toString() << std::endl;
            m_grabber->setVideoNorm( pVidNrmList->at( nrmCounter ) );

            // Now retrieve the video formats.
            Grabber::tVidFmtListPtr pVidFmtList = m_grabber->getAvailableVideoFormats();
            if( pVidFmtList == 0 )
            {
                std::cerr << "ICCam::PrintCameraInformation: " << m_grabber->getLastError().toString() << std::endl;
                return RET_FAILED;
            }

            unsigned int fmtCounter = 0;
            // List the available video formats.
            for( Grabber::tVidFmtList::iterator itFmt = pVidFmtList->begin();
                    itFmt != pVidFmtList->end();
                    ++itFmt )
            {
                std::cout << "\t[" << fmtCounter++ << "] " << itFmt->toString() << std::endl;
            }

            std::cout << std::endl << std::endl;

        }

    }
    else
    {
        // If the current video capture device does not support video norms,
        // the available video formats can be retrieved immediately.
        std::cout << "\nSupported Video Formats: \n";

        Grabber::tVidFmtListPtr pVidFmtList = m_grabber->getAvailableVideoFormats();

        if( pVidFmtList == 0 ) // No video formats available?
        {
            std::cerr << "ICCam::PrintCameraInformation: " << m_grabber->getLastError().toString() << std::endl;
            return RET_FAILED;
        }
        else
        {
            unsigned int fmtCounter = 0;
            // List the available video formats.
            for( Grabber::tVidFmtList::iterator itFmd = pVidFmtList->begin();
                    itFmd != pVidFmtList->end();
                    ++itFmd )
            {
                std::cout << "\t[" << fmtCounter++ << "] " << itFmd->toString() << std::endl;
            }
        }
    }


    return RET_OK;
}