Loading
 
Support Support Home » APIs » XiAPINET » NET Sample code

NET Sample code

Look up table (LUT) example


using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Drawing;
using xiApi.NET;
namespace xiApi.NET_example
{
class Program
{
  static void Main(string[] args)
  {
    xiCam myCam = new xiCam();

    try
    {
      // Initialize first camera
      myCam.OpenDevice(0);

      // Set device exposure to 2 milliseconds
      int exposure_us = 2000;
      myCam.SetParam(PRM.EXPOSURE, exposure_us);

      // Set device gain to 5 decibels
      float gain_db = 5;
      myCam.SetParam(PRM.GAIN, gain_db);

      // Set image output format to monochrome 8 bit
      myCam.SetParam(PRM.IMAGE_DATA_FORMAT, IMG_FORMAT.MONO8);

      // Setting LUT parameters - inverse example, 
      // which inverts pixel values (min -> max, max -> min).
      int maxIndex, minIndex;
      int maxValue, minValue;
      maxIndex = myCam.GetParamInt(PRM.LUT_INDEX_MAX);
      minIndex = myCam.GetParamInt(PRM.LUT_INDEX_MIN);
      maxValue = myCam.GetParamInt(PRM.LUT_VALUE_MAX);
      minValue = myCam.GetParamInt(PRM.LUT_VALUE_MIN);

      Console.WriteLine("max index {0} \n", maxIndex);
      Console.WriteLine("max value {0} \n", maxValue);

      for (int i = 0; i < maxIndex; i++)
      {
          myCam.SetParam(PRM.LUT_INDEX, i);
          myCam.SetParam(PRM.LUT_VALUE, maxValue - i);

      }

      myCam.SetParam(PRM.LUT_EN, 1);

      //Start acquisition
      myCam.StartAcquisition();

      // Capture images
      Bitmap myImage;
      int timeout = 1000;
      for (int i = 0; i < 10; i++)
      {
        myCam.GetImage(out myImage, timeout );
        string fName = string.Format("image{0}.bmp", i);
        myImage.Save(fName);
      }
      // Stop acquisition
      myCam.StopAcquisition();
    }

    catch (System.ApplicationException appExc)
    {
      // Show handled error
      Console.WriteLine(appExc.Message);
      System.Console.ReadLine();
      myCam.CloseDevice();
    }

    finally
    {
      myCam.CloseDevice();
    }
  }
} // end of Program
} // end of namespace xiApi.NET_example

Offline processing example

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Drawing;
using System.Windows.Media.Imaging;
using System.IO;
using System.Threading;
using xiApi.NET;
using xiApi;

namespace xiProc_NET_Example
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("TestOfflineProcessing_UsingAPI_WithCamera V3\n");

            const int max_images = 10;
            xiCam myCam = new xiCam();
            xiProc myProc = new xiProc();
            XI_IMG[] images = new XI_IMG[max_images];

            string image_path = "images";
            // create directory for images
            Directory.CreateDirectory(image_path);

            try
            {
                // Get number of connected devicess
                int numDevices = 0;
                myCam.GetNumberDevices(out numDevices);

                if (0 == numDevices)
                {
                    Console.WriteLine("No devices found");
                    Thread.Sleep(3000);
                    return;
                }

                // Initialize the device and return the device handle.
                myCam.OpenDevice(0);
                myCam.SetParam(PRM.IMAGE_DATA_FORMAT, IMG_FORMAT.RAW8);
                myCam.SetParam(PRM.BUFFER_POLICY, BUFF_POLICY.SAFE);                
                // set the same parameters like when camera processing is running online
                // these parameters will be part of API context list
                myCam.SetParam(PRM.EXPOSURE, 10000);
                myCam.SetParam(PRM.WB_KR, 1.05F);
                myCam.SetParam(PRM.WB_KG, 1.0F);
                myCam.SetParam(PRM.WB_KB, 2.10F);
                myCam.SetParam(PRM.GAMMAY, 1.0F);
                myCam.SetParam(PRM.SHARPNESS, 1.0F);

                // capture images
                myCam.StartAcquisition();
                for (int i = 0; i < max_images; i++)
                {                    
                    images[i] = myCam.GetXI_IMG(1000);                    
                }

                // offline processing
                Console.WriteLine("Offline processing started");
                myProc.ProcOpen();

                string cam_context = string.Empty;
                cam_context = myCam.GetParamString(PRM.API_CONTEXT_LIST);                
                myProc.ProcSetParam(PRM.API_CONTEXT_LIST, cam_context);
                myProc.ProcSetParamInt(PRM.IMAGE_DATA_FORMAT, IMG_FORMAT.RGB24);

                for(int i=0; i < max_images; i++)
                {
                    Bitmap processed_image;
                    myProc.xiProcPushImage(images[i]);
                    myProc.xiProcPullImage(out processed_image, 1000);
                    string fName = string.Format(image_path + "/RGBAimage{0}.png", i);
                    Console.WriteLine("Got image: {0}, size {1}x{2} saving to {3}", i, 
                    processed_image.Width, processed_image.Height, fName);
                    processed_image.Save(fName);
                }

                // clear acquisition buffers and close camear
                myCam.StopAcquisition();
                myCam.CloseDevice();
            }
            catch (System.ApplicationException appExc)
            {
                Console.WriteLine(appExc.Message);
                Thread.Sleep(3000);
                myCam.CloseDevice();
            }

        }
    }
}