加入專案檔案。

This commit is contained in:
el124vip124 2025-01-16 14:32:53 +08:00
parent 38e62f7266
commit a2988b17ee
7 changed files with 556 additions and 0 deletions

22
Camera.sln Normal file
View File

@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35527.113 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Camera_connect", "Camera_connect\Camera_connect.csproj", "{AC07B92D-98AE-45ED-92C7-A4F93F4FFD7E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AC07B92D-98AE-45ED-92C7-A4F93F4FFD7E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AC07B92D-98AE-45ED-92C7-A4F93F4FFD7E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AC07B92D-98AE-45ED-92C7-A4F93F4FFD7E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AC07B92D-98AE-45ED-92C7-A4F93F4FFD7E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<Reference Include="ids_peak_dotnet">
<HintPath>C:\Program Files\IDS\ids_peak\generic_sdk\samples\bin\x86_64\ids_peak_dotnet.dll</HintPath>
</Reference>
<Reference Include="ids_peak_ipl_dotnet">
<HintPath>C:\Program Files\IDS\ids_peak\generic_sdk\samples\bin\x86_64\ids_peak_ipl_dotnet.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

142
Camera_connect/IDSCamera.cs Normal file
View File

@ -0,0 +1,142 @@
using peak;
using peak.core;
using peak.core.nodes;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Camera_connect
{
public class IDSCamera
{
Device device1;
NodeMap nodeMapRemoteDevice1;
DataStream dataStream1;
Bitmap cam1Image;
public bool CameraInit(string camera1SerialNumber)
{
try
{
Library.Initialize();
DeviceManager deviceManager = DeviceManager.Instance();
deviceManager.Update();
if (deviceManager.Devices()[0] == null)
{
return false;
}
foreach (var deviceDescriptor in deviceManager.Devices())
{
if (deviceDescriptor.SerialNumber() == camera1SerialNumber)
{
device1 = deviceDescriptor.OpenDevice(DeviceAccessType.Control);
nodeMapRemoteDevice1 = device1.RemoteDevice().NodeMaps().First();
//nodeMapRemoteDevice1.FindNode<IntegerNode>("Width").SetValue(5472);
//nodeMapRemoteDevice1.FindNode<IntegerNode>("Height").SetValue(3648);
nodeMapRemoteDevice1.FindNode<IntegerNode>("Width").SetValue(1920);
nodeMapRemoteDevice1.FindNode<IntegerNode>("Height").SetValue(1200);
nodeMapRemoteDevice1.FindNode<IntegerNode>("OffsetX").SetValue(0);
nodeMapRemoteDevice1.FindNode<IntegerNode>("OffsetY").SetValue(0);
//nodeMapRemoteDevice1.FindNode<EnumerationNode>("ExposureAuto").SetCurrentEntry("Off");
nodeMapRemoteDevice1.FindNode<EnumerationNode>("AcquisitionMode").SetCurrentEntry("SingleFrame");
dataStream1 = device1.DataStreams()[0].OpenDataStream();
// Get the payload size for correct buffer allocation
UInt32 payloadSize = Convert.ToUInt32(nodeMapRemoteDevice1.FindNode<IntegerNode>("PayloadSize").Value());
// Get the minimum number of buffers that must be announced
var bufferCountMax = dataStream1.NumBuffersAnnouncedMinRequired();
// Allocate and announce image buffers and queue them
for (var bufferCount = 0; bufferCount < bufferCountMax; ++bufferCount)
{
var buffer = dataStream1.AllocAndAnnounceBuffer(payloadSize, IntPtr.Zero);
dataStream1.QueueBuffer(buffer);
}
}
}
}
catch
{
return false;
}
return true;
}
private void Capture()
{
try
{
// Lock critical features to prevent them from changing during acquisition
nodeMapRemoteDevice1.FindNode<IntegerNode>("TLParamsLocked").SetValue(1);
dataStream1.StartAcquisition();
nodeMapRemoteDevice1.FindNode<CommandNode>("AcquisitionStart").Execute();
nodeMapRemoteDevice1.FindNode<CommandNode>("AcquisitionStart").WaitUntilDone();
// Get buffer from device's datastream
var buffer = dataStream1.WaitForFinishedBuffer(5000);
// Create IDS peak IPL
var iplImg = new peak.ipl.Image((peak.ipl.PixelFormatName)buffer.PixelFormat(), buffer.BasePtr(), buffer.Size(), buffer.Width(), buffer.Height());
// Debayering and convert IDS peak IPL to RGB8 format
iplImg = iplImg.ConvertTo(peak.ipl.PixelFormatName.BGR8);
var width = Convert.ToInt32(iplImg.Width());
var height = Convert.ToInt32(iplImg.Height());
var stride = Convert.ToInt32(iplImg.PixelFormat().CalculateStorageSizeOfPixels(iplImg.Width()));
// Queue buffer so that it can be used again
dataStream1.QueueBuffer(buffer);
var image = new Bitmap(width, height, stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, iplImg.Data());
cam1Image = image;
// The other images are not needed anymore.
buffer.Dispose();
dataStream1.StopAcquisition();
// Lock critical features to prevent them from changing during acquisition
nodeMapRemoteDevice1.FindNode<IntegerNode>("TLParamsLocked").SetValue(0);
}
catch
{
}
}
public void SetExposureTime(double time)
{
try
{
nodeMapRemoteDevice1.FindNode<FloatNode>("ExposureTime").SetValue(time);
}
catch
{
}
}
public double GetExposureTime()
{
try
{
return nodeMapRemoteDevice1.FindNode<FloatNode>("ExposureTime").Value();
}
catch
{
return 0;
}
}
public Bitmap GetPicture()
{
Capture();
return cam1Image;
}
public void Destroy()
{
if (device1 == null)
return;
device1.Dispose();
}
}
}

124
Camera_connect/Main.Designer.cs generated Normal file
View File

@ -0,0 +1,124 @@
namespace Camera_connect
{
partial class Main
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
bt_connect = new Button();
bt_OneShot = new Button();
bt_KeepShot = new Button();
bt_Stop = new Button();
pictureBox1 = new PictureBox();
label1 = new Label();
((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit();
SuspendLayout();
//
// bt_connect
//
bt_connect.Location = new Point(94, 75);
bt_connect.Name = "bt_connect";
bt_connect.Size = new Size(116, 63);
bt_connect.TabIndex = 0;
bt_connect.Text = "相機連線";
bt_connect.UseVisualStyleBackColor = true;
bt_connect.Click += bt_connect_Click;
//
// bt_OneShot
//
bt_OneShot.Location = new Point(99, 242);
bt_OneShot.Name = "bt_OneShot";
bt_OneShot.Size = new Size(116, 63);
bt_OneShot.TabIndex = 1;
bt_OneShot.Text = "單張擷取";
bt_OneShot.UseVisualStyleBackColor = true;
bt_OneShot.Click += bt_OneShot_Click;
//
// bt_KeepShot
//
bt_KeepShot.Location = new Point(243, 242);
bt_KeepShot.Name = "bt_KeepShot";
bt_KeepShot.Size = new Size(116, 63);
bt_KeepShot.TabIndex = 2;
bt_KeepShot.Text = "連續取像";
bt_KeepShot.UseVisualStyleBackColor = true;
bt_KeepShot.Click += bt_KeepShot_Click;
//
// bt_Stop
//
bt_Stop.Location = new Point(385, 242);
bt_Stop.Name = "bt_Stop";
bt_Stop.Size = new Size(116, 63);
bt_Stop.TabIndex = 3;
bt_Stop.Text = "停止";
bt_Stop.UseVisualStyleBackColor = true;
bt_Stop.Click += bt_Stop_Click;
//
// pictureBox1
//
pictureBox1.Location = new Point(99, 342);
pictureBox1.Name = "pictureBox1";
pictureBox1.Size = new Size(772, 637);
pictureBox1.TabIndex = 4;
pictureBox1.TabStop = false;
//
// label1
//
label1.AutoSize = true;
label1.Location = new Point(99, 171);
label1.Name = "label1";
label1.Size = new Size(61, 23);
label1.TabIndex = 5;
label1.Text = "label1";
//
// Main
//
AutoScaleDimensions = new SizeF(11F, 23F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1512, 1078);
Controls.Add(label1);
Controls.Add(pictureBox1);
Controls.Add(bt_Stop);
Controls.Add(bt_KeepShot);
Controls.Add(bt_OneShot);
Controls.Add(bt_connect);
Name = "Main";
Text = "Form1";
((System.ComponentModel.ISupportInitialize)pictureBox1).EndInit();
ResumeLayout(false);
PerformLayout();
}
#endregion
private Button bt_connect;
private Button bt_OneShot;
private Button bt_KeepShot;
private Button bt_Stop;
private PictureBox pictureBox1;
private Label label1;
}
}

111
Camera_connect/Main.cs Normal file
View File

@ -0,0 +1,111 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
using System.Reflection.Emit;
namespace Camera_connect
{
public partial class Main : Form
{
public IDSCamera IDS_camera = new IDSCamera();
private bool isKeepShotting = false; // 用於控制連續拍攝
private Task keepShotTask; // 用於執行拍攝任務
private CancellationTokenSource cts; // 用於取消拍攝任務
public Main()
{
InitializeComponent();
}
private void bt_connect_Click(object sender, EventArgs e)
{
if (IDS_camera.CameraInit("4103372214"))
{
IDS_camera.SetExposureTime(10000);
label1.Text = "IDS連線成功";
}
else
{
label1.Text = "IDS連線失敗";
}
}
private void bt_OneShot_Click(object sender, EventArgs e)
{
try
{
if (IDS_camera != null)
{
Bitmap image = IDS_camera.GetPicture();
if (image != null)
{
pictureBox1.Image = image;
}
}
}
catch (Exception ex)
{
}
}
private void bt_KeepShot_Click(object sender, EventArgs e)
{
if (isKeepShotting)
{
return;
}
isKeepShotting = true;
cts = new CancellationTokenSource();
CancellationToken token = cts.Token;
keepShotTask = Task.Run(() =>
{
while (!token.IsCancellationRequested)
{
try
{
if (IDS_camera != null)
{
Bitmap image = IDS_camera.GetPicture();
if (image != null)
{
pictureBox1.Invoke((MethodInvoker)delegate
{
pictureBox1.Image = image;
});
}
}
}
catch (Exception ex)
{
}
}
}, token);
}
private void bt_Stop_Click(object sender, EventArgs e)
{
if (!isKeepShotting)
{
return;
}
cts.Cancel();
isKeepShotting = false;
}
}
}

120
Camera_connect/Main.resx Normal file
View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

17
Camera_connect/Program.cs Normal file
View File

@ -0,0 +1,17 @@
namespace Camera_connect
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new Main());
}
}
}