Created
August 1, 2017 05:23
-
-
Save immengineer/9e797af65cc7e6ddfcd0aa67aa14ab9c to your computer and use it in GitHub Desktop.
JAISDK Transport layer名取得サンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Text; | |
using System.Windows.Forms; | |
using Jai_FactoryDotNET; | |
namespace SimpleImageDisplaySample | |
{ | |
public partial class Form1 : Form | |
{ | |
// Main factory object | |
CFactory myFactory = new CFactory(); | |
// Opened camera object | |
CCamera myCamera; | |
// GenICam nodes | |
CNode myWidthNode; | |
CNode myHeightNode; | |
CNode myGainNode; | |
public Form1() | |
{ | |
InitializeComponent(); | |
Jai_FactoryWrapper.EFactoryError error = Jai_FactoryWrapper.EFactoryError.Success; | |
// Open the factory with the default Registry database | |
error = myFactory.Open(""); | |
// Search for cameras and update all controls | |
SearchButton_Click(null, null); | |
} | |
private void WidthNumericUpDown_ValueChanged(object sender, EventArgs e) | |
{ | |
if (myWidthNode != null) | |
{ | |
myWidthNode.Value = int.Parse(WidthNumericUpDown.Value.ToString()); | |
SetFramegrabberValue("Width", (Int64)myWidthNode.Value); | |
} | |
} | |
private void HeightNumericUpDown_ValueChanged(object sender, EventArgs e) | |
{ | |
if (myHeightNode != null) | |
{ | |
myHeightNode.Value = int.Parse(HeightNumericUpDown.Value.ToString()); | |
SetFramegrabberValue("Height", (Int64)myHeightNode.Value); | |
} | |
} | |
private void GainTrackBar_Scroll(object sender, EventArgs e) | |
{ | |
if (myGainNode != null) | |
myGainNode.Value = int.Parse(GainTrackBar.Value.ToString()); | |
GainLabel.Text = myGainNode.Value.ToString(); | |
} | |
private void StartButton_Click(object sender, EventArgs e) | |
{ | |
if (myCamera != null) | |
myCamera.StartImageAcquisition(true, 5); | |
} | |
private void StopButton_Click(object sender, EventArgs e) | |
{ | |
if (myCamera != null) | |
myCamera.StopImageAcquisition(); | |
} | |
private void SearchButton_Click(object sender, EventArgs e) | |
{ | |
if (null != myCamera) | |
{ | |
if (myCamera.IsOpen) | |
{ | |
myCamera.Close(); | |
} | |
myCamera = null; | |
} | |
// Discover GigE and/or generic GenTL devices using myFactory.UpdateCameraList(in this case specifying Filter Driver for GigE cameras). | |
myFactory.UpdateCameraList(Jai_FactoryDotNET.CFactory.EDriverType.FilterDriver); | |
// Open the camera - first check for GigE devices | |
for (int i = 0; i < myFactory.CameraList.Count; i++) | |
{ | |
myCamera = myFactory.CameraList[i]; | |
if (Jai_FactoryWrapper.EFactoryError.Success == myCamera.Open()) | |
{ | |
break; | |
} | |
} | |
if (null != myCamera && myCamera.IsOpen) | |
{ | |
CameraIDTextBox.Text = myCamera.CameraID; | |
if (myCamera.NumOfDataStreams > 0) | |
{ | |
StartButton.Enabled = true; | |
StopButton.Enabled = true; | |
} | |
else | |
{ | |
StartButton.Enabled = false; | |
StopButton.Enabled = false; | |
} | |
int currentValue = 0; | |
// Get the Width GenICam Node | |
myWidthNode = myCamera.GetNode("Width"); | |
if (myWidthNode != null) | |
{ | |
currentValue = int.Parse(myWidthNode.Value.ToString()); | |
// Update range for the Numeric Up/Down control | |
// Convert from integer to Decimal type | |
WidthNumericUpDown.Maximum = decimal.Parse(myWidthNode.Max.ToString()); | |
WidthNumericUpDown.Minimum = decimal.Parse(myWidthNode.Min.ToString()); | |
WidthNumericUpDown.Value = decimal.Parse(currentValue.ToString()); | |
WidthNumericUpDown.Enabled = true; | |
} | |
else | |
WidthNumericUpDown.Enabled = false; | |
SetFramegrabberValue("Width", (Int64)myWidthNode.Value); | |
// Get the Height GenICam Node | |
myHeightNode = myCamera.GetNode("Height"); | |
if (myHeightNode != null) | |
{ | |
currentValue = int.Parse(myHeightNode.Value.ToString()); | |
// Update range for the Numeric Up/Down control | |
// Convert from integer to Decimal type | |
HeightNumericUpDown.Maximum = decimal.Parse(myHeightNode.Max.ToString()); | |
HeightNumericUpDown.Minimum = decimal.Parse(myHeightNode.Min.ToString()); | |
HeightNumericUpDown.Value = decimal.Parse(currentValue.ToString()); | |
HeightNumericUpDown.Enabled = true; | |
} | |
else | |
HeightNumericUpDown.Enabled = false; | |
SetFramegrabberValue("Height", (Int64)myHeightNode.Value); | |
SetFramegrabberPixelFormat(); | |
// Get the GainRaw GenICam Node | |
myGainNode = myCamera.GetNode("GainRaw"); | |
if (myGainNode != null) | |
{ | |
currentValue = int.Parse(myGainNode.Value.ToString()); | |
// Update range for the TrackBar Controls | |
GainTrackBar.Maximum = int.Parse(myGainNode.Max.ToString()); | |
GainTrackBar.Minimum = int.Parse(myGainNode.Min.ToString()); | |
GainTrackBar.Value = currentValue; | |
GainLabel.Text = myGainNode.Value.ToString(); | |
GainLabel.Enabled = true; | |
GainTrackBar.Enabled = true; | |
} | |
else | |
{ | |
GainLabel.Enabled = false; | |
GainTrackBar.Enabled = false; | |
} | |
// Get the Transport Layer Name | |
uint iSize = 512; | |
StringBuilder sbTransportLayerName = new StringBuilder(512); | |
Jai_FactoryWrapper.EFactoryError error = Jai_FactoryWrapper.EFactoryError.Success; | |
error = Jai_FactoryWrapper.J_Camera_GetTransportLayerName(myCamera.ItemHandle, sbTransportLayerName, ref iSize); | |
if (Jai_FactoryWrapper.EFactoryError.Success != error) | |
{ | |
MessageBox.Show(this, "Error getting transport layer name for " + myCamera.CameraID, "Camera Transport Name Error", MessageBoxButtons.OK, MessageBoxIcon.Stop); | |
return; | |
} | |
else | |
textBox1.Text = sbTransportLayerName.ToString(); | |
} | |
else | |
{ | |
StartButton.Enabled = true; | |
StopButton.Enabled = true; | |
WidthNumericUpDown.Enabled = false; | |
HeightNumericUpDown.Enabled = true; | |
GainLabel.Enabled = false; | |
GainTrackBar.Enabled = false; | |
MessageBox.Show("No Cameras Found!"); | |
} | |
} | |
private void Form1_FormClosing(object sender, FormClosingEventArgs e) | |
{ | |
if (myCamera != null) | |
{ | |
StopButton_Click(null, null); | |
myCamera.Close(); | |
} | |
} | |
private void SetFramegrabberValue(String nodeName, Int64 int64Val) | |
{ | |
if (null == myCamera) | |
{ | |
return; | |
} | |
IntPtr hDevice = IntPtr.Zero; | |
Jai_FactoryWrapper.EFactoryError error = Jai_FactoryWrapper.J_Camera_GetLocalDeviceHandle(myCamera.CameraHandle, ref hDevice); | |
if (Jai_FactoryWrapper.EFactoryError.Success != error) | |
{ | |
return; | |
} | |
if (IntPtr.Zero == hDevice) | |
{ | |
return; | |
} | |
IntPtr hNode; | |
error = Jai_FactoryWrapper.J_Camera_GetNodeByName(hDevice, nodeName, out hNode); | |
if (Jai_FactoryWrapper.EFactoryError.Success != error) | |
{ | |
return; | |
} | |
if (IntPtr.Zero == hNode) | |
{ | |
return; | |
} | |
error = Jai_FactoryWrapper.J_Node_SetValueInt64(hNode, false, int64Val); | |
if (Jai_FactoryWrapper.EFactoryError.Success != error) | |
{ | |
return; | |
} | |
//Special handling for Active Silicon CXP boards, which also has nodes prefixed | |
//with "Incoming": | |
if ("Width" == nodeName || "Height" == nodeName) | |
{ | |
string strIncoming = "Incoming" + nodeName; | |
IntPtr hNodeIncoming; | |
error = Jai_FactoryWrapper.J_Camera_GetNodeByName(hDevice, strIncoming, out hNodeIncoming); | |
if (Jai_FactoryWrapper.EFactoryError.Success != error) | |
{ | |
return; | |
} | |
if (IntPtr.Zero == hNodeIncoming) | |
{ | |
return; | |
} | |
error = Jai_FactoryWrapper.J_Node_SetValueInt64(hNodeIncoming, false, int64Val); | |
} | |
} | |
private void SetFramegrabberPixelFormat() | |
{ | |
String nodeName = "PixelFormat"; | |
if (null == myCamera) | |
{ | |
return; | |
} | |
IntPtr hDevice = IntPtr.Zero; | |
Jai_FactoryWrapper.EFactoryError error = Jai_FactoryWrapper.J_Camera_GetLocalDeviceHandle(myCamera.CameraHandle, ref hDevice); | |
if (Jai_FactoryWrapper.EFactoryError.Success != error) | |
{ | |
return; | |
} | |
if (IntPtr.Zero == hDevice) | |
{ | |
return; | |
} | |
long pf = 0; | |
error = Jai_FactoryWrapper.J_Camera_GetValueInt64(myCamera.CameraHandle, nodeName, ref pf); | |
if (Jai_FactoryWrapper.EFactoryError.Success != error) | |
{ | |
return; | |
} | |
UInt64 pixelFormat = (UInt64)pf; | |
UInt64 jaiPixelFormat = 0; | |
error = Jai_FactoryWrapper.J_Image_Get_PixelFormat(myCamera.CameraHandle, pixelFormat, ref jaiPixelFormat); | |
if (Jai_FactoryWrapper.EFactoryError.Success != error) | |
{ | |
return; | |
} | |
StringBuilder sbJaiPixelFormatName = new StringBuilder(512); | |
uint iSize = (uint)sbJaiPixelFormatName.Capacity; | |
error = Jai_FactoryWrapper.J_Image_Get_PixelFormatName(myCamera.CameraHandle, jaiPixelFormat, sbJaiPixelFormatName, iSize); | |
if (Jai_FactoryWrapper.EFactoryError.Success != error) | |
{ | |
return; | |
} | |
IntPtr hNode; | |
error = Jai_FactoryWrapper.J_Camera_GetNodeByName(hDevice, nodeName, out hNode); | |
if (Jai_FactoryWrapper.EFactoryError.Success != error) | |
{ | |
return; | |
} | |
if (IntPtr.Zero == hNode) | |
{ | |
return; | |
} | |
error = Jai_FactoryWrapper.J_Node_SetValueString(hNode, false, sbJaiPixelFormatName.ToString()); | |
if (Jai_FactoryWrapper.EFactoryError.Success != error) | |
{ | |
return; | |
} | |
//Special handling for Active Silicon CXP boards, which also has nodes prefixed | |
//with "Incoming": | |
string strIncoming = "Incoming" + nodeName; | |
IntPtr hNodeIncoming; | |
error = Jai_FactoryWrapper.J_Camera_GetNodeByName(hDevice, strIncoming, out hNodeIncoming); | |
if (Jai_FactoryWrapper.EFactoryError.Success != error) | |
{ | |
return; | |
} | |
if (IntPtr.Zero == hNodeIncoming) | |
{ | |
return; | |
} | |
error = Jai_FactoryWrapper.J_Node_SetValueString(hNodeIncoming, false, sbJaiPixelFormatName.ToString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment