2011-07-31 34 views
4

幾天來,我試圖從相機檢索拍攝的照片。今天我發現用EDSDK拍攝照片並立即檢索

EdsSetPropertyData(cRef, PropID_SaveTo, 0, 4, 2); 

它不保存圖片到相機。但我的問題是目前我無法從相機檢索圖像。我的ObjectEventHandler永遠不會被調用(有時在初始化時會調用以前的圖片)。 這裏是我的來電顯示(主功能):

Camera camera = new Camera(); 
Thread thread = new Thread(new ThreadStart(camera.DoThings)); 
thread.Start(); 

這裏是我的CC級(攝像頭控制)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using EDSDKLib; 

namespace CCCBSv2 
{ 
class CC : EDSDK 
{ 
    IntPtr cRef; 

    // Initilizes, takes picture and uninitilizes 
    public void RunSeq() 
    { 
     uint err; 
     err = Initialize(); 
     if (err != EDS_ERR_OK) 
     { 
      return; 
     } 
     err = TakePicture(); 
     if (err != EDS_ERR_OK) 
     { 
      return; 
     } 
     Uninitilize(); 
    } 

    public uint Initialize() 
    { 
     // err init 
     uint err; 
     // Initilize SDK. 
     // SDK initilizion is needed to use EDSDK functions 
     err = EdsInitializeSDK(); 

     // In case of error pass it on... 
     if (err != EDS_ERR_OK) 
     { 
      return err; 
     } 

     // get first camera connected to computer 
     err = GetFirstCamera(); 

     if (err != EDS_ERR_OK) 
     { 
      return err; 
     } 

     // Open session to camera 
     EdsOpenSession(cRef); 

     // Set object event handler, We need this event handler to retrieve pictures from camera 
     err = EdsSetObjectEventHandler(cRef, ObjectEvent_All, ObjectEventHandler, IntPtr.Zero); 

     if (err != EDS_ERR_OK) 
     { 
      return err; 
     } 

     // We don't want to save pictures to the camera so lets set save target to host 
     err = EdsSetPropertyData(cRef, PropID_SaveTo, 0, 4, 2); 

     return err; 
    } 

    // uninitilizes edsdk 
    public uint Uninitilize() 
    { 
     uint err; 
     err = EdsCloseSession(cRef); 
     if (err != EDS_ERR_OK) 
     { 
      return err; 
     } 
     err = EdsTerminateSDK(); 

     return err; 
    } 

    // gets the first camera to cRef 
    private uint GetFirstCamera() 
    { 
     // Variable initilazions 
     IntPtr cameraList; 
     uint err; 
     int count; 

     // Get list of cameras; 
     err = EdsGetCameraList(out cameraList); 

     if (err != EDS_ERR_OK) 
     { 
      return err; 
     } 

     // Get count of cameras in list 
     err = EdsGetChildCount(cameraList, out count); 

     if (err != EDS_ERR_OK) 
     { 
      return err; 
     } 
     if (count == 0) 
     { 
      return EDS_ERR_DEVICE_NOT_FOUND; 
     } 

     // Get first camera to public cRef 
     err = EdsGetChildAtIndex(cameraList, 0, out cRef); 

     if (err != EDS_ERR_OK) 
     { 
      return err; 
     } 

     // release camera list 
     if (cameraList != null) 
     { 
      EdsRelease(cameraList); 
     } 

     // everything went just fine 
     return EDS_ERR_OK; 
    } 

    public uint ObjectEventHandler(uint inEvent, IntPtr inRef, IntPtr inContext){ 
     switch (inEvent) 
     { 
      // in case of a download request 
      case ObjectEvent_DirItemRequestTransfer: 
       DownloadImage(inRef); 
       break; 
      default: 
       break; 
     } 
     return EDS_ERR_OK; 
    } 

    // Downloads the picture from camera's buffer to the computer 
    private uint DownloadImage(IntPtr dItem) 
    { 

     EdsDirectoryItemInfo dirInfo; 
     uint err; 
     IntPtr stream = new IntPtr(); 
     err = EdsGetDirectoryItemInfo(dItem, out dirInfo); 

     if (err == EDS_ERR_OK) 
     { 
      err = EdsCreateFileStream(dirInfo.szFileName, EdsFileCreateDisposition.CreateAlways, EdsAccess.ReadWrite, out stream); 
     } 
     if (err == EDS_ERR_OK) 
     { 
      err = EdsDownload(dItem, dirInfo.Size, stream); 
     } 
     if (err == EDS_ERR_OK) 
     { 
      err = EdsDownloadComplete(dItem); 
     } 
     if (stream != null) 
     { 
      EdsRelease(stream); 
     } 
     return err; 
    } 

    // Takes picture 
    public uint TakePicture() 
    { 
     return EdsSendCommand(cRef, CameraCommand_TakePicture, 0); 
    } 
} 
} 
+0

如何使用Canon.EOS.Framework設置相機屬性? – 2012-01-02 15:50:33

回答

0

我的第一個猜測是,你反初始化驅動過快,回調前有機會被觸發。

TakePicture只是告訴相機拍照,並且您立即返回或多或少地返回初始化驅動程序。但實際上拍攝照片需要移動機械部件,並且可能需要半年的時間,並且在相機宣佈已完成時,不再有聽衆接聽信息。

3

您將需要一個Windows消息循環來獲取和分派事件。

while(GetMessage(&msg, NULL, NULL, NULL)) 
{   
    TranslateMessage(&msg); 
    DispatchMessage(&msg); 
} 

還有,記得PropID_SaveTo設置爲SaveTo_Host:您添加後這main()對象的事件處理程序被調用。你可以看到我的文章here