IC Imaging Control ActiveX

Saving Codec Properties

Saving Codec Properties

This chapter shows you how to handle codecs and how to save and load the properties of a codec.

Introduction

The source code for this example is contained in the samples\VB6\Saving Codec Properties sample source directory.

A codec compresses and decompresses video streams. Sometimes, a codec is also referred to as a "compressor" or an "avicompressor".

The properties of some (but not all) codecs can be retrieved and assigned with IC Imaging Control. For example, the quality of a video stream may be set before it is saved.

Codec properties are supplied as a binary data stream. It is very important to know the length of the data stream, as later when an application loads the data stream, it is required to allocate memory for it.

The saving file format must be binary, because the data stream is also binary.

You should not alter the binary data stream from a codec. Doing so would make the data no longer fit the codec, thus causing unpredictable errors. Furthermore, the codec properties of one codec will not match each other. IC Imaging Control prevents this situation from happening at all - it causes an error message.

A codec is an external DLL file that is handled by an AviCompressor object. If an AviCompressor object becomes invalid, the DLL will be removed from memory. In this case, most codecs will loose their previously set properties. You should, therefore, declare a global AviCompressor object in your form in order to avoid loosing properties.

You can retrieve data from or assign data to codecs where the property PropertyPageAvailable is True. It is therefore necessary to check this property before performing an operation.

Setting up the Project

Create a new project and add IC Imaging Control to the form. Now add 3 buttons to the form and label them Save Data, Load Data and Show Property Page. Name the buttons cmdSaveData, cmdLoadData and cmdShowPropertyPage respectively. Add at least one combo box to the form and name it cboVideoCodec. This combo box will contain the names of the codecs.

image

Setting Up the Codec

First of all, an application needs a global AviCompressor object that contains the data of the codec. This must be global, because the AviCompressor 's properties will be lost when it is removed from memory. So add the following line to the beginning of the form:

Dim Codec As AviCompressor

Add a Form_Load event procedure to the form and enter the following code to fill the combo box with the available codecs:

Private Sub Form_Load()

    ' Show available video codecs in the combo box
    For Each Codec In ICImagingControl1.AviCompressors
        cboVideoCodec.AddItem Codec.Name
    Next
    cboVideoCodec.ListIndex = 0

    ' Get the first available codec from ICImagingControl.
    Set Codec = ICImagingControl1.AviCompressors.Item(cboVideoCodec.ListIndex + 1)

    ' Enable or disable the buttons.
    cmdShowPropertyPage.Enabled = Codec.PropertyPageAvailable
    cmdLoadData.Enabled = Codec.PropertyPageAvailable
    cmdSaveData.Enabled = Codec.PropertyPageAvailable
End Sub

This code uses the AviCompressors property of IC Imaging Control to generate a list of all available codecs. As you can see, the global variable Codec is used as item in the For Each .. In .. statement. The first available codec will be assigned to the global AviCompressor object Codec. The three buttons will be enabled or disabled according to the property PropertyPageAvailable in the AviCompressor object. This avoids performing operations that are not supported by the codec.

Now add the Click event procedure of the combo box cboVideoCodec to your form:

Private Sub cboVideoCodec_Click()

    ' Get the codec object from ICImagingControl that is
    ' selected in the combobox.
    Set Codec = ICImagingControl1.AviCompressors.Item _
                         (cboVideoCodec.ListIndex + 1)

    ' Enable or disable the buttons.
    cmdShowPropertyPage.Enabled = Codec.PropertyPageAvailable
    cmdLoadData.Enabled = Codec.PropertyPageAvailable
    cmdSaveData.Enabled = Codec.PropertyPageAvailable
End Sub

The selection index of the combo box refers to an item in the AviCompressors collection. This item is assigned to the global AviCompressor object Codec.

The AviCompressor 's property PropertyPageAvailable is used to disable or enable the buttons. This prevents, for example, the user from pressing the cmdShowPropertyPage button when no property dialog is available for the codec.

Now, add the Click event procedure of the cmdShowPropertyPage button to your form:

Private Sub cmdShowPropertyPage_Click()

    ' Show the property dialog of the codec.
    Codec.ShowPropertyPage
End Sub

This shows the property dialog of the codec. The cmdShowPropertyPage button was enabled or disabled in the Form_Load and cboVideoCodec Click event procedure, thus you do not need to check PropertyPageAvailable again.

This shows the property dialog of the codec. The cmdShowPropertyPage button was enabled or disabled in the Form_Load and SelectedValueChanged event procedure, thus you do not need to check PropertyPageAvailable again.

Saving the Codec Properties

As described above, the codec properties are supplied as a binary data stream. A binary data stream is simply a list of bytes. It is recommended to save the byte count in the data stream as the first element in the file. Using this first element, when the data is loaded from file, a dynamic array can be sized or resized to the matching byte count in order to accommodate the data.

Add the Click event procedure of cmdSaveData to your form:

Private Sub cmdSaveData_Click()
    Dim Data() As Byte ' Dynamic array for the data stream.

    Open "test.bin" For Binary As #1 ' Must be opened binary!

    ' Save the length of the codec's name.
    Put #1, , Len(Codec.Name)

    ' Save the name of the codec.
    Put #1, , Codec.Name

    ' Save the byte count of the data stream. It is used
    ' when data is loaded from file to resize the dynamic array.
    Put #1, , Codec.CompressorDataSize

    ' Get the data from the codec.
    Data = Codec.CompressorData

    ' Save the data to the file.
    Put #1, , Data
    Close #1
End Sub

The codec properties will be saved in the file test.bin. Remember that the cmdSaveData button is only enabled, if AviCompressor.PropertyPageAvailable returns True. Alternatively, you can implement the following lines of code:

If Codec.PropertyPageAvailable Then
    ' Do something like saving etc.
Else
    MsgBox "This codec does not support properties!"
End If

Loading the Codec Properties

Add the Click event procedure of cmdLoadData to your form:

Private Sub cmdLoadData_Click()
    Dim Data() As Byte      ' Dynamic array for the data stream.
    Dim Length As Long      ' Length of the data stream.
    Dim NameLength As Long  ' Length of the codec's name.
    Dim CodecName As String ' Name of the codec.
    Dim i As Integer

    Open "test.bin" For Binary As #1 ' Must be opened binary!

    ' Read the length of the codec's name.
    Get #1, , NameLength

    ' Allocate memory for the name.
    CodecName = String(NameLength, " ")

    ' Read the name of the codec.
    Get #1, , CodecName

    ' Compare the names.
    If CodecName = Codec.Name Then
        ' Read the length of the follwing data.
        Get #1, , Length

        ' Resize the data array to Length.
        ReDim Data(Length)

        ' Read the codec data from the file.
        Get #1, , Data

        ' Assign the read data to the codec object.
        Codec.CompressorData = Data
    Else
        MsgBox "The saved data does not match to the used codec." + Chr(13) + _
               "saved: " + CodecName + Chr(13) + _
               "used: " + Codec.Name
    End If
    Close #1
End Sub

As described above at the cmdSaveData button, the cmdLoadData button is only enabled if AviCompressor.PropertyPageAvailable returns True. Before assigning data to a codec, you should check whether it supports properties:

If Codec.PropertyPageAvailable Then
    ' Do something like loading etc.
Else
    MsgBox "This codec does not support properties!"
End If

Using the Codec for Capture

For the sake of completeness, we have included the following statement that shows how to use the codec to capture an AVI file:

ImagingControl.AviStartCapture "test.avi", Codec.Name

This is not included in the example.

<< Programmer's Guide

Get in touch with us


About The Imaging Source

Established in 1990, The Imaging Source is one of the leading manufacturers of industrial cameras, frame grabbers and video converters for production automation, quality assurance, logistics, medicine, science and security.

Our comprehensive range of cameras with USB 3.1, USB 3.0, USB 2.0, GigE interfaces and other innovative machine vision products are renowned for their high quality and ability to meet the performance requirements of demanding applications.

Automated Imaging Association ISO 9001:2015 certified

Contact us