Canon SDK Live View

Okay, recently we needed to make a Canon EOS40D capture a Live Stream (keep in mind this is a Still Camera not a video camera) and pull the Live Stream back to a Windows Forms application and perform interactions with another system via Serial Commands.  The biggest struggle throughout the project was the Live Stream.  Taking pictures and the rest of the project was a snap (bad pun)….

There was absolutely NO documentation provided in a managed code such as .NET (which is what we were writing it in), Canon does not support the SDK, and documentation on the internet is nearly nothing.

Below is a sample of the LiveView loop and how to call it in a separate thread to make it run.

***************** UPDATE ********************************************

Due to the enormous popularity of this post we have made access to our SVN Repository for this project open.

You can download the sample from http://svn.hostoverride.com:81/svn/Kodak – the access is read only using the following credentials:

Username:  public
Password:  hostoverride2012

Let me know if you have any questions

93 Comments for this entry

  • roberto, April 8th, 2010 on 11:52 pm

    It seems to be great !
    I received EDSDK from Canon But I am a beginner in VB.Net and I wonder how to declare the EDSDK.dll in VB code to be able to use it ?
    The VB code provided by Canon don’t works (I don’t succeed in adding DLL to the project and there is non help from Canon for VB, only for C++ or C#)
    Can you send a example of the coding necessary for these declarations ?
    Many thanks.
    Roberto

  • justin, April 9th, 2010 on 12:06 am

    Sure Roberto, we will send something over to help you get started.

  • Kenny, April 14th, 2010 on 7:41 am

    Justin.

    I have been trying to get the sdk working in C# 2008 on windows 7 but for some reason I don’t get any exceptions or errors. It just executes but nothing happens. I tried to execute the VB samples but nothing. Due to the lack of documentation it is kind of hard to make any progress. I would be grateful if you could send my something that will get me started or at least give me a head start into the right direction.

    Tons of thanks.

    Kenny
    mulengak_at_verizon_dot_net.

  • justin, April 14th, 2010 on 7:25 pm

    Hi Kenny. Our implementation was written in C# and VB.NET on Windows Vista. We have not since revisited it to see it work on Windows 7. What camera are you using? Also, how far are you getting with it, when you initialize does it find the camera or give you a “Camera Not Found” message box? Let me know where you are lagging and we will help you get started.

  • dirk, April 18th, 2010 on 10:09 am

    HI justin,

    can you help me with this…

    # Dim buffer() As Byte = New Byte(CInt(len)) {}
    #
    # Marshal.Copy(ipData, buffer, 0, CInt(len))
    # Dim memStream As New System.IO.MemoryStream(buffer)
    #
    # ‘ get the bitmap
    #
    # Dim bitmap As Bitmap
    #
    # bitmap = bitmap.FromStream(memStream)
    #
    # PictureBox1.Image = bitmap

    first problem i encounter is with

    Marshal.Copy(ipData, buffer, 0, CInt(len))

    it seems “MARSHAL” is not recognize at all..

    If its ok can you provide me a simple vb.net program that does the live view… cause its giving me hard time figuring it out..

    ill really appreciate it…

    thanks

  • justin, April 18th, 2010 on 11:29 am

    Do you have the C++ compilers loaded in your Visual Studio instance?

  • dirk, April 19th, 2010 on 12:13 am

    Justin,

    Yes i have..

    But im not that familiar in c++.

    here’s the thing, i have the SDK sample for vb.net. and its working fine.. the only thing that i need to know is how to display the live view..

    i really appreciate your help..
    thanks.

  • justin, April 19th, 2010 on 12:54 pm

    Have you verified your camera supports Live View?

  • TAYLOR, April 23rd, 2010 on 1:44 pm

    Appreciate your article. It was well said.

  • Lars, April 29th, 2010 on 3:44 pm

    Hello Justin.
    I am using vb.net and EOS 1000D, and having trouble getting the liveview to work. The test app was made based on the sample from canon, but no luck, do you have a sample that works… :-)

  • Lars, April 29th, 2010 on 3:59 pm

    Hello Justin…. i forgot to tell i tried your sample above, but get “Unable to download Evf image – 81″ ?
    any ideas….

  • justin, April 29th, 2010 on 6:42 pm

    Ok, this post appears to have gotten very popular. I went and checked our code back out of SVN so we could pull it up. We actually don’t even have a camera anymore as it was turned back over to the client upon project completion but I am going to try to step you through it as best as I can remember. We started with a Canon-40D and ended up with a Canon-50D. I cannot speak to models of cameras other than that but assuming they work with the SDK and offer Live View then they should work.

    Note that we began with the VB.NET Windows Forms example provided by Canon and modified it to add the Live View which did not exist:

    Here are the Imports Statements:

    Imports System.Runtime.InteropServices
    Imports System.Threading
    Imports System.IO
    Imports System.Drawing
    Imports System.Drawing.Drawing2D
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.IO.Ports
    Imports System.ComponentModel
    Imports System.Collections.Generic

    Directly inside the Class on the code-behind for the Windows form we added some public constants, we have left the pertinant ones here:

    Inherits System.Windows.Forms.Form
    Implements Observer

    Public LiveViewRunning As Boolean = False
    Private LVThread As Thread
    Friend WithEvents PictureBox2 As System.Windows.Forms.PictureBox
    Friend WithEvents btnInitializePreview As System.Windows.Forms.Button
    Public PreviewInt As Boolean = False

    Public scThread As Thread

    This function here when fired will initialize the camera, set the variables necessary for live preview, and fire a test picture so you know the camera is ready. You can obviously remove the test picture once you are satisfied it works:

    First fire this:

    Public Sub BeginProcess()

    Dim err As Integer = EDS_ERR_OK
    Dim cameraList As IntPtr = Nothing
    Dim camera As IntPtr = Nothing
    Dim count As Integer = 0
    Dim isSDKLoaded As Boolean = False
    Dim propObj As New CameraProperty

    ‘ connect property id to combobox.
    m_cmbTbl.Add(kEdsPropID_AEMode, Me.AEModeCmb)
    m_cmbTbl.Add(kEdsPropID_ISOSpeed, Me.ISOSpeedCmb)
    m_cmbTbl.Add(kEdsPropID_Av, Me.AvCmb)
    m_cmbTbl.Add(kEdsPropID_Tv, Me.TvCmb)
    m_cmbTbl.Add(kEdsPropID_MeteringMode, Me.MeteringModeCmb)
    m_cmbTbl.Add(kEdsPropID_ExposureCompensation, Me.ExposureCompCmb)
    m_cmbTbl.Add(kEdsPropID_ImageQuality, Me.ImageQualityCmb)

    err = EdsInitializeSDK()

    If err = EDS_ERR_OK Then

    isSDKLoaded = True

    End If

    If err = EDS_ERR_OK Then

    err = EdsGetCameraList(cameraList)

    End If

    If err = EDS_ERR_OK Then

    err = EdsGetChildCount(cameraList, count)
    If count = 0 Then
    err = EDS_ERR_DEVICE_NOT_FOUND
    End If

    End If

    ‘// Get the first camera.
    If err = EDS_ERR_OK Then

    err = EdsGetChildAtIndex(cameraList, 0, camera)

    End If

    Dim deviceInfo As EdsDeviceInfo = Nothing

    If err = EDS_ERR_OK Then

    err = EdsGetDeviceInfo(camera, deviceInfo)

    If err = EDS_ERR_OK And IsNothing(camera) = True Then
    err = EDS_ERR_DEVICE_NOT_FOUND
    End If

    End If

    If IsNothing(cameraList) = False Then

    EdsRelease(cameraList)

    End If

    ‘// Create the camera model
    If err = EDS_ERR_OK Then

    model = cameraModelFactory(camera, deviceInfo)

    If IsNothing(model) = True Then
    err = EDS_ERR_DEVICE_NOT_FOUND

    End If
    End If

    If err <> EDS_ERR_OK Then

    MessageBox.Show(“Cannot detect camera”)

    End If

    If err = EDS_ERR_OK Then

    ‘// Create a controller
    controller = New CameraController

    ‘// Set the model to this controller.
    controller.setCameraModel(model)

    ‘// Make notify the model updating to the view.
    model.addObserver(Me)

    ‘ ————————————————————————
    ‘ ————————————————————————
    ‘ You should create class instance of delegates of event handlers
    ‘ with ‘new’ expressly if its attribute is Shared,
    ‘ because System sometimes do garbage-collect these delegates.


    ‘ This error occurs.

    ‘ CallbackOnCollectedDelegate is detected.
    ‘ Message: Callback was called with
    ‘ garbage-collected delegate of
    ‘ Type() ‘VBSample3!VBSample3.EDSDKTypes+EdsPropertyEventHandler::Invoke’

    ‘ It will be able to make data loss or application clash.
    ‘ You should stock delegates when you want to send delegate to unmanaged code.

    ‘ ————————————————————————
    If err = EDS_ERR_OK Then

    err = EdsSetPropertyEventHandler(camera, kEdsPropertyEvent_All, _
    inPropertyEventHandler, IntPtr.Zero)
    End If

    ‘// Set ObjectEventHandler
    If err = EDS_ERR_OK Then
    err = EdsSetObjectEventHandler(camera, kEdsObjectEvent_All, _
    inObjectEventHandler, IntPtr.Zero)

    End If

    ‘// Set StateEventHandler
    If err = EDS_ERR_OK Then
    err = EdsSetCameraStateEventHandler(camera, kEdsStateEvent_All, _
    inStateEventHandler, IntPtr.Zero)
    End If

    End If

    If err <> EDS_ERR_OK Then

    If IsNothing(camera) = False Then
    EdsRelease(camera)
    camera = Nothing
    End If

    If (isSDKLoaded) Then
    EdsTerminateSDK()
    End If

    If IsNothing(model) = False Then
    model = Nothing
    End If

    If IsNothing(controller) = False Then
    controller = Nothing
    End If

    End

    End If

    ‘//Execute the controller.
    controller.run()

    End Sub

    Then this:

    Public Sub Initialize()

    Dim camera As IntPtr

    camera = model.getCameraObject()

    Dim err As Integer = EDS_ERR_OK

    Dim prop As Integer = EdsEvfOutputDevice.kEdsEvfOutputDevice_PC ‘ Change this to _PC
    Dim proptype As Integer = EDSDKTypes.kEdsPropID_Evf_OutputDevice

    Dim saveVal As Integer = kEdsPropID_SaveTo
    Dim saveType As Integer = EdsSaveTo.kEdsSaveTo_Both

    Dim cmIntPtr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(saveType))
    Marshal.StructureToPtr(saveType, cmIntPtr, False)

    ‘ Stock the property.
    Dim wkIntPtr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(prop))
    Marshal.StructureToPtr(prop, wkIntPtr, False)
    ‘ send property/command to the camera
    err = EdsSetPropertyData(camera, proptype, 0, Marshal.SizeOf(prop), prop)
    err = EdsSetPropertyData(camera, saveVal, 0, Marshal.SizeOf(saveType), saveType)

    PreviewInt = True

    Thread.Sleep(2000)

    controller.actionPerformed(“takepicture”)

    End Sub

    Now you should be able to call the functions from above in the original tutorial and get Preview to work. Let me know how it works out!

  • Dirk, April 30th, 2010 on 1:33 am

    Hi Justin,

    thanks for the help.. there is one thing i cannot understand here.. Im trying to know where will the live view be displayed? in the FORM? or in a PICTURE BOX? what part of the code point to where the live view be displayed? Unfortunately I need to wait till sunday to test this because I dont have the Cannon camera right now here. thats why im just trying to learn the code by reading it.. But like what I said THANK YOU VERY MUCH..

    DIRK

  • Lars, April 30th, 2010 on 3:41 am

    Hello Jason.
    Thanks for the quick respond, i added your code to the canon tutorial and marked out the code in the load event on the form and added yours:
    BeginProcess()
    Initialize()

    The camera fires up and takes a picture on startup as mentioned and i get the settings from the camera, but no preveiw, no error… hmmm, i can see you delcare:
    Friend WithEvents PictureBox2 As System.Windows.Forms.PictureBox
    I suppose it’s for the preview, but i can’t see in your code where you point to it.. any ideas., The camera support liveview, by the way.

  • justin, April 30th, 2010 on 6:11 am

    It runs to the PictureBox on the Form. It runs in a separate thread and continually updates the PictureBox.

  • Dirk, April 30th, 2010 on 6:32 am

    Thank you very much Justin..

  • Lars, April 30th, 2010 on 12:09 pm

    Hello Justin.
    Thanks again, it make sense with the thread, but i can’t find the code for it, can you post it.

  • Lars, April 30th, 2010 on 12:33 pm

    I may need to add, that i added your code from the top of this page and made a button that run the “PreviewOn” sub, which i expected to give a liveview, on the picturebox1 that i added to the form. :-( , but still no liveview, and no error. any ideas…..

  • Lars, April 30th, 2010 on 12:39 pm

    Hello justin.
    I tried to enable the line:
    MessageBox.Show(“Unable to download Evf image – ” & Hex(result))
    and gets the result…. “Unable to download Evf image – 81″ have you run into this.
    many thanks

  • justin, May 2nd, 2010 on 5:43 am

    Yes, that line we used for debugging. It relates to a Hex error code which is referenced in the C++ library which the .NET wrapper utilizes. I forget how we actually found the error detail but referencing the CanonSDK documentation (all 100+ pages of it) is where we found it. I will try to take a look later and get back to you.

  • James, May 9th, 2010 on 2:46 am

    HI Justin

    Thank you sharing your code with us, I am truly grateful.

    I have also entered the code as suggested and reach the same conclusion as Lars. I would benefit me greatly if I could get a grip of the preview feature and therefore would ask you to please see what you can do for us in providing the “missing” code.

    Best regards
    James, South Africa

  • James, May 9th, 2010 on 5:29 am

    HI Justin

    Please ignore my last post, I see you provided the code in the first entry…silly me.

    Thank you once again
    Best regards
    James

  • justin, May 9th, 2010 on 11:37 am

    Thanks James. Let me know if you need any further help.

  • dirk, May 10th, 2010 on 1:44 am

    Justin,

    Thank you very much… Problem solved… Thanks for the help..

  • justin, May 10th, 2010 on 10:49 am

    You are very welcome Dirk. Glad we were able to help. What kind of app are you building out of curiosity?

  • Jaxon Grant, June 1st, 2010 on 7:24 am

    This blog was recommended to me by a friend. I have to say, browsing this was of value. I will inform more friends about it as well.

  • cengiz, June 3rd, 2010 on 9:21 am

    Thank u for the code buddy..!
    I modified it a little bit..and now it works just purrrrfect !

  • Paul Sorauer, September 29th, 2010 on 2:15 pm

    Hi Justin

    I know this post is a little old now but just like the first reply above, I need a little help understanding how to use the header files in Visual Studio.NET c#/vb.

    Could you possibly forward to me the information you sent to Roberto back in April?

    Thanks.

  • Herndon, October 8th, 2010 on 8:57 am

    good post, added you to my RSS reader. Hope it works with my FeedBurner

  • Sodah, October 13th, 2010 on 8:53 am

    Hello,

    nice work. Hmmm…. but this will not work. I use the latest SDK 2.8 and the Canon 1000D.
    I get this error:
    Unable to download Evf image – 81

    Have you a complete VB Project for me?

  • justin, October 13th, 2010 on 9:42 am

    I am very familiar with that error. Just to let you know we built this over a year ago and were working with the Canon 50D. I will package up the code and post it for everyone and post a link back here later today. Thanks.

  • Randy Hogan, October 17th, 2010 on 5:19 pm

    It amazes me the wonders that can be recorded with one of these cameras. Remarkable editorial, interesting insight you supplied.

  • SerWal, October 28th, 2010 on 12:32 pm

    Hi,

    is there anything new about SDK 2.8 and LiveView Mode with Canon EOS 1000D? The function EdsCreateEvfImageRef() always returns EDS_ERR_FILE_FORMAT_UNRECOGNIZED error.

  • Serwal, October 29th, 2010 on 9:59 am

    Sorry, I have used the EdsCreateImageRef() instead of EdsCreateEvfImageRef(). With the right function it works well. I have tried .Net Framework 4.0 but there was “PInvokeStackImbalance” exception after calling the EdsCreateEvfImageRef() function. The solution was to use .NET Framework 2.0

    Thanks

  • omer, December 2nd, 2010 on 1:50 am

    Hi. I’m currently trying to get liveview from VC++, but it’s really hard for me as I am a total beginner in VC++. However I do have some experience with C#, but the EDSDK.dll appears to be unmanaged and thus I cannot add a reference to it in C#, so I have to use DllImport in C# to access the dll functions. Are you aware of any C# wrapper for the unmanaged code? Thanks

  • justin, December 2nd, 2010 on 8:11 am

    Hi Omer:

    This whole thread is showing you how to do Live View in VB.NET which you could easily convert to C# even using an online converter; however, here is also a thread on C# Live View. Let me know if there are any more questions:

    http://www.overridepro.com/liveviewcsharp

  • Paul, February 9th, 2011 on 7:03 am

    Hi Justin,

    How the is your project with the 50D? I have a similar type, will the above codes work with this model?

  • Donald Chapman, February 17th, 2011 on 4:31 pm

    Thank you very much for all of the above code. I have Live View implemented. I want to be able to have the camera in Video mode, which I do and I would like my operator to press the Record button on the camera to start video recording, then when finished, press it again to stop – this action should fire the New Item to download aevent which I can react to.

    My problem is that pressing the record button does nothing – even though I have called the Unlock command.

    Do you have any idea on how I can get the camera to respond to pressing the record button?

    Thanks

  • justin, February 17th, 2011 on 4:46 pm

    Paul — Our final implementation of this used a 50D actually as the 40D had been replaced and our implementation was in Disney’s EPCOT and the 40D had burned out.

    Donald — You need to do a EDS.Release() I believe. It has been a LONG time since I worked with this but I believe that is what it is. Basically you need to drop control of the camera and then pick it back up. Personally if I were you I would give the user an interface to run the camera without touching it. The project we used this forth was for a theme park attraction in Disney EPCOT. The attraction used a show control system which issued Serial Commands so we wrote a Serial listener, the operator had a handheld control that they pressed buttons on. One button started the LivePreview mode, then next button turned live preview off, took a photo, passed it to the server transformed it and showed it on a series of monitors. It sounds similar to what you are wanting to do so that is the route I would go. Whatever you can do that is easiest whether it be attached by USB, Network Cable, serial, wireless, bluetooth, whatever but I would just have the camera be separate. I believe Canon actually sells something that attaches to the camera but I am not sure.

  • helana, February 19th, 2011 on 12:09 am

    Hi Justin,

    I use SDK 2.8 and the Canon 1000D.
    I get this error:
    EDSDK.EdsDownloadEvfImage(cameraDev, EvfImageRef);
    Unable to download Evf image – 81

    I really appreciate your help.
    Many thanks.

  • Paul, February 21st, 2011 on 4:21 pm

    Hi Justin,

    I am having a very hard time getting the camera to acknowledging the vb program. I can get to the point to where the message comes up and states “cannot detect camera”. How can I get the vb program to display/test the image from the camera?

    I am also new to this camera/vb coding, so please be patience. Thank you for your help.

  • Jakov, February 22nd, 2011 on 8:11 am

    I did everything written above, now the program takes picture (in Initialize()) and then shows me just one frame from the live view!
    I made the button with
    LVThread = New Thread(AddressOf LiveView)
    LiveViewRunning = True
    LVThread.Start()
    but when clicked it shows me only 1-2 seconds of stream, so I have to click it again and again.
    Why does that happen?

  • Arik, March 14th, 2011 on 6:29 pm

    A couple of years ago I built a great VB tool to snap pictures from a webcam and upload them to our website. Unfortunately the webcam photo quality stinks so I bought a Rebel and got the SDK so I could capture higher quality photos. I’ve been banging my head against the wall for a couple of days trying to get this code to work and to replace the webcam in my app with the canon Rebel.

    I’m looking for someone to build me a VB.net application that will connect to my camera, display a real-time view of what the camera sees, let me click a “Take Picture” button that saves the file to my hard drive and sets a text box with the file name so that I can integrate it into the rest of my application. If anyone has built or can build this for me, please let me know as I’ll PayPal you a check for $1000. No joke.

    Thanks,
    Arik.

  • Alphie, May 10th, 2011 on 9:54 am

    Hi Justin,

    I was really glad to find this posting because there’s not much else of help elsewhere. Unfortunately I’ve hit a brick wall getting live view out of a EOS1000D which I’m hoping someone can help with.

    Every time I call EdsDownloadEvfImage I get a EDS_ERR_OBJECT_NOTREADY error returned. The kEdsPropID_Evf_OutputDevice is being set to kEdsEvfOutputDevice_PC OK and kEdsPropID_SaveTo is being set to kEdsSaveTo_Host with no errors. I know live view is supported somehow because I’ve seen it work in other applications.

    Thanks

  • BT, June 30th, 2011 on 7:02 am

    Hello Justin,

    I’m having the same issue as Roberto posted at top of this thread. I’m having difficulty registering the Canon dlls in my visual Studio 2010 project. Any help would be appreciated.

    BT

  • justin, July 6th, 2011 on 8:31 am

    Hi All–

    So sorry I haven’t seen these comments lately. I apologize. I am going to reply to each post one by one so they are segmented out for you.

  • justin, July 6th, 2011 on 8:32 am

    To Paul – if the program will not recognize the camera then you are probably using the wrong version of the SDK.

  • justin, July 6th, 2011 on 8:34 am

    To Jakov – it sounds like you are not multi-threading correctly or the LiveViewRunning variable is being set back somehow in side your loop. Have you run the program in debug mode and stepped through watching for that variable?

  • justin, July 6th, 2011 on 8:45 am

    Arik– If you are serious I have a program that is 90% of the way there right now and can gladly make the modifications for the $1,000.00. Send me a message through the Contact Form on this site and we can work out the details.

  • justin, July 6th, 2011 on 8:55 am

    @Alphie – I am not sure about the EOS1000D although a quick Google search does indicate that it is Live View compatible. I would think that perhaps you are firing the function too quick and that the Code has not gained a lock on the camera yet. Have you tried slowing it down, perhaps a Thread.Sleep(5000) command in there somewhere to make it sleep for 5 seconds?

    This code was written for a 40D/50D but should be compatible with any Live View supported Camera that uses the same SDK.

    It has not been upgraded. It was written for an attraction at Disney’s EPCOT in late 2009 at that time there was NOTHING on the Internet about it.

Leave a Reply




*