Menu

IAMCameraControl NullReferenceException Error

kevinlin
2011-06-07
2012-10-29
  • kevinlin

    kevinlin - 2011-06-07

    Hello,erveryone, I want to write a program to get the exposure time of a
    webcam and I was using IAMCameraControl. But there was an error in my program
    and I couldn't find out what the problem is.

    Private CamControl As IAMCameraControl
    
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Dim data As Integer
            Dim iFlags As CameraControlFlags
            Dim result As Integer = CamControl.Get(CameraControlProperty.Exposure, data, iFlags)
            TextBox1.Text = String.Concat(data.ToString)
    
        End Sub
    

    When the program excuted the get function, it showed an NullReferenceException
    Error. Can anyone help me point out what the error is?

     
  • kevinlin

    kevinlin - 2011-06-07

    BTW,the error is System.NullReferenceException – Object reference not set to
    an instance of an object.

     
  • snarfle

    snarfle - 2011-06-07

    Where do you assign a value to CamControl?

     
  • kevinlin

    kevinlin - 2011-06-07

    Assign a value to CamControl?Why?I did't do that.

     
  • kevinlin

    kevinlin - 2011-06-07

    What value should I assigned to?

     
  • kevinlin

    kevinlin - 2011-06-07

    Hi,snarfle, I get another question, If I have more than one webcam used in my
    program, how can I specify which camera is using?The belowing is my whole
    program. Now my program have no error but can not get the expected value. All
    the three values of exposure,focus,zoom are 0. I guess it's because of the
    selected video device. Is it?Can you give me some guidline on how to solve it?
    Thank you so much for your kind help.

    Private capDevices As DsDevice()
        Private CamDevice As DsDevice
        Private CamFilter As IBaseFilter = Nothing
        Private CamControl As IAMCameraControl
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Enumerate video Input filters and add them to comboBox1
            capDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice)
            For Each ds As DsDevice In capDevices
                ComboBox1.Items.Add(ds.Name)
            Next
    
        End Sub
    
     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    
            ' Setup()
            Dim expo, focus, zoom As Integer
    
            GetControlProperties(CameraControlProperty.Exposure, expo)
            TextBox1.Text = String.Concat(expo.ToString)
    
            GetControlProperties(CameraControlProperty.Focus, focus)
            TextBox2.Text = String.Concat(focus.ToString)
    
            GetControlProperties(CameraControlProperty.Zoom, zoom)
            TextBox3.Text = String.Concat(zoom.ToString)
    
        End Sub
    
        Private Sub Setup()
            CamControl = Nothing
            'Get the IAMCameraControl
            Dim devs As DsDevice() = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice)
            'Get the graphbuilder object
            Dim graphBuilder As IFilterGraph2 = TryCast(New FilterGraph(), IFilterGraph2)
            'add the video input device
            Dim capFilter As IBaseFilter = Nothing
            Dim hr As Integer = graphBuilder.AddSourceFilterForMoniker(devs(0).Mon, Nothing, devs(0).Name, capFilter)
            DsError.ThrowExceptionForHR(hr)
            CamControl = TryCast(capFilter, IAMCameraControl)
            Debug.Assert(CamControl IsNot Nothing)
    
        End Sub
    
        Private Sub GetControlProperties(ByVal CtrlProp As DirectShowLib.CameraControlProperty, ByRef value As Integer)
            Dim hr As Integer
            'Dim iValue As Integer
            Dim iFlags As CameraControlFlags
            hr = CamControl.Get(CtrlProp, value, iFlags)
            'Dim Result As Integer = CamControl.Get(CtrlProp, value, DirectShowLib.CameraControlFlags.Manual)
        End Sub
    
    
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            If ComboBox1.SelectedIndex <> -1 Then
                CamDevice = capDevices(ComboBox1.SelectedIndex)
                Dim graphBuilder As IFilterGraph2 = CType(New FilterGraph, IFilterGraph2)
                Dim hr As Integer = graphBuilder.AddSourceFilterForMoniker(CamDevice.Mon, Nothing, CamDevice.Name, CamFilter)
                CamControl = CType(CamFilter, IAMCameraControl)
            End If
        End Sub
    
        Private Function CreateFilter(ByVal category As Guid, ByVal friendlyname As String) As IBaseFilter
            Dim source As Object = Nothing
            Dim iid As Guid = GetType(IBaseFilter).GUID
            For Each device As DsDevice In DsDevice.GetDevicesOfCat(category)
                If device.Name.CompareTo(friendlyname) = 0 Then
                    device.Mon.BindToObject(Nothing, Nothing, iid, source)
                    Exit For
                End If
            Next
    
            Return DirectCast(source, IBaseFilter)
        End Function
    
     

Log in to post a comment.

Auth0 Logo