0

我编写了 C++ 代码来在监视器上显示实时取景图像。我提到了stackoverflow上的一些代码。最后,我完成了我的代码,但是有一些问题。

我想使用 opencv 显示实时取景图像,但我不知道如何获取实时取景图像的宽度和高度。(也许它是由 EDSDK 函数检索到的..)

请为我回答。

(我附上了我的代码,我想为我的代码提供合适的答案)

(看“//libjpegTurbo...//,有手动的_width&height。我想用函数检索)


// Functions_body
bool LiveViewStart() 
{
    EdsError err = EDS_ERR_OK;
    EdsCameraListRef cameraList = NULL;
    EdsCameraRef camera = NULL;
    EdsUInt32    count = 0; 
    bool         isSDKLoaded = false;

    // Initialize SDK
    err = EdsInitializeSDK(); 

    if(err == EDS_ERR_OK) 
    { 
        isSDKLoaded = true; 
    }

    // Acquisition of camera list
    if(err == EDS_ERR_OK)
    {
        err = EdsGetCameraList(&cameraList);
    }

    // Acquisition of number of Cameras
    if(err == EDS_ERR_OK)
    {
        err = EdsGetChildCount(cameraList, &count);
        if(count == 0)
        {
            err = EDS_ERR_DEVICE_NOT_FOUND;
        }
    }

    // Acquisition of camera at the head of the list
    if(err == EDS_ERR_OK)
    {   
        err = EdsGetChildAtIndex(cameraList , 0 , &camera); 
    }

    // Acquisition of camera information
    EdsDeviceInfo deviceInfo;
    if(err == EDS_ERR_OK)
    {   
        err = EdsGetDeviceInfo(camera , &deviceInfo);   
        if(err == EDS_ERR_OK && camera == NULL)
        {
            err = EDS_ERR_DEVICE_NOT_FOUND;
        }
    }

    // Release camera list
    if(cameraList != NULL)
    {
        EdsRelease(cameraList);
    }

    if(err != EDS_ERR_OK)
    {
        ::MessageBox(NULL,"Cannot detect camera",NULL,MB_OK);
        exit(1);
    }

    // Open session with camera 
    if(err == EDS_ERR_OK) 
    { 
        err = EdsOpenSession(camera); 
    } 

    // Start Live view  
    // Get the output device for the live view image
    EdsUInt32 device;
    err = EdsGetPropertyData(camera, kEdsPropID_Evf_OutputDevice, 0, sizeof(device), &device);

    // PC live view starts by setting the PC as the output device for the live view image. 
    if(err == EDS_ERR_OK) 
    { 
        device |= kEdsEvfOutputDevice_PC; 
        err = EdsSetPropertyData(camera, kEdsPropID_Evf_OutputDevice, 0 , sizeof(device), &device);
    } 

    Sleep(2000);

    // Download EvfData
    EdsEvfImageRef evfImage = NULL;
    EdsStreamRef stream = NULL;
    unsigned char* data = NULL;
    unsigned long size = 0;

    // Create memory stream
    err = EdsCreateMemoryStream(0, &stream);

    if (err != EDS_ERR_OK) {
        cout << "Download Live View Image Error in Function EdsCreateMemoryStream: " << err << "\n";
        return false;
    }

    // Create EvfImageRef.
    err = EdsCreateEvfImageRef(stream, &evfImage);

    if (err != EDS_ERR_OK) {
        cout << "Download Live View Image Error in Function EdsCreateEvfImageRef: " << err << "\n";
        return false;

    }

    // Download live view image data.
    err = EdsDownloadEvfImage(camera, evfImage);

    if (err != EDS_ERR_OK) {
        cout << "Download Live View Image Error in Function EdsDownloadEvfImage: " << err << "\n";
        return false;
    }

    //Sleep(1000);

    // Get Pointer of evfStream
    err = EdsGetPointer(stream, (EdsVoid**)& data);

    if (err != EDS_ERR_OK) {
        cout << "Download Live View Image Error in Function EdsGetPointer: " << err << "\n";
        return false;
    }

    // Get Length of evfStream
    err = EdsGetLength(stream, &size);

    if (err != EDS_ERR_OK) {
        cout << "Download Live View Image Error in Function EdsGetLength: " << err << "\n";
        return false;
    }



    // libjpegTurbo(data, size);
    int JPEG_QUALITY = 75;
    int COLOR_COMPONENTS = 3;
    int _width = 1920;
    int _height = 1080;
    long unsigned int _jpegSize = 0;
    unsigned char *_compressedImage = NULL;
    unsigned char *buffer = new unsigned char [_width * _height * COLOR_COMPONENTS];

    tjhandle _jpegCompressor = tjInitCompress();

    tjCompress2(_jpegCompressor, buffer, _width, 0, _height, TJPF_RGB, &_compressedImage, &_jpegSize, TJSAMP_444, JPEG_QUALITY, TJFLAG_FASTDCT);

    tjDestroy(_jpegCompressor);

    // display RGB image in opencv

    // Release stream
    if (stream != NULL) {
        EdsRelease(stream);
        stream = NULL;
    }

    // Release evfImage
    if (evfImage != NULL) {            
        EdsRelease(evfImage);
        evfImage = NULL;
    }

    data = NULL;

    // End Live view.
    // Get the output device for the live view image
    err = EdsGetPropertyData(camera, kEdsPropID_Evf_OutputDevice, 0, sizeof(device), &device);

    // PC live view ends if the PC is disconneccted from the live view output device.
    if(err == EDS_ERR_OK)
    {
        device &= ~kEdsEvfOutputDevice_PC;
        err = EdsSetPropertyData(camera, kEdsPropID_Evf_OutputDevice, 0, sizeof(device), &device);
    }

    // Close session with camera 
    if(err == EDS_ERR_OK) 
    {
        err = EdsCloseSession(camera); 
    } 

    // Release camera 
    if(camera != NULL) 
    { 
        EdsRelease(camera); 
    } 
    // Terminate SDK 
    if(isSDKLoaded) 
    { 
        EdsTerminateSDK(); 
    } 
    return true;
} 
4

2 回答 2

1

如果你有一个 evfImage,你可以使用 EdsGetPropertyData 来请求它的 kEdsPropID_Evf_CoordinateSystem(类型 EdsSize)。此值包含最后捕获的帧的实时视图的宽度和高度。

于 2014-06-14T14:19:32.003 回答
1

佳能 SDK 不提供任何函数来获取实时取景图像的大小。您在代码中面临的问题是您尝试压缩图像但需要解压缩它。我建议您阅读 libjpg turbo 的用法。他们提供了非常有据可查的例子:http ://www.libjpeg-turbo.org/Documentation/Documentation

旁注:这是您的真实代码还是只是为了展示?因为实时取景图片下载必须循环调用才能重复调用,否则只会下载一张图片并关闭所有内容。

亲切的问候

于 2014-04-14T13:23:30.267 回答