IC Imaging Control C++

Properties of a Codec

Properties of a Codec

This example demonstrates how to get and set the properties of a codec.

The program can be found in the %TOPLEVEL%\Samples\VC71\CodecProperty directory. A VC6 version can be found in the directory %TOPLEVEL%\Samples\VC6. In order to run the program, open the solution file CodecProperty.sln in this directory and select Build -> Build CodecProperty in the menu. Then the program can be executed by selecting Debug -> Start.

The program generates a list of all available video compressors and asks the user for the name of the compressor it should use and a filename. If the selected codec has a property dialog, this dialog will now be displayed. After the dialog has been closed, the program retrieves the properties set in the property dialog and saves them to the file "codec.xml". Then it loads the properties that had been previously saved from the file and puts them in the codec. It then waits for a key press to start the capturing of the image stream. It will then write the image stream into the AVI file until enter is pressed again.

Not every codec has a property dialog that allows parameters to be set externally. The following codecs have a property dialog:

  • Cinepak2
  • MJPEG
  • DV Video Encoder
  • Indeo video 5.10 compression filter
  • Lead MCMP/MJPEG Codec (VfW)
  • DivX412
  • DivX500

Saving Codec Settings

To save the data of a codec, use Codec::getSettings. This method returns an XML formatted string, containing the codec data. You can write this string to a file or the registry to save your program's codec settings:

std::string codec_settings = codec.getSettings();
if( !codec_settings.empty() )
{
    std::ofstream file( filename.c_str() );
    if( file.good() )
    {
        file << codec_settings;
        bSucceded = true;
    }
    else
    {
        std::cerr << "Error : Could not open file " << filename << std::endl;
    }
}
else
{
    std::cerr << "Error : No data available from the codec" << std::endl;
}

Loading Codec Settings

To restore codec settings, use Codec::setSettings:

std::ifstream file( filename.c_str() );
if( file.good() )
{
    std::string codec_settings;
    while( file.good() )    // read the file char by char
    {
        codec_settings += file.get();
    }
    if( !codec_settings.empty() )    // no data in the file check
    {
        if( codec.setSettings( codec_settings ) )    // set the data
        {
            bSucceded = true;
        }
        else
        {
            std::cerr << "Error : Data does not fit codec" << std::endl;
        }
    }
    else
    {
        std::cerr << "Error : No data found in the supplied file" << std::endl;
    }
}
else
{
    std::cerr << "Error : Could not open file " << filename << std::endl;
}

See also

Writing Image Streams to an AVI file

<< 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