diff --git a/Camera.sln b/Camera.sln new file mode 100644 index 0000000..0c99167 --- /dev/null +++ b/Camera.sln @@ -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 diff --git a/Camera_connect/Camera_connect.csproj b/Camera_connect/Camera_connect.csproj new file mode 100644 index 0000000..8ca0d1f --- /dev/null +++ b/Camera_connect/Camera_connect.csproj @@ -0,0 +1,20 @@ + + + + WinExe + net8.0-windows + enable + true + enable + + + + + C:\Program Files\IDS\ids_peak\generic_sdk\samples\bin\x86_64\ids_peak_dotnet.dll + + + C:\Program Files\IDS\ids_peak\generic_sdk\samples\bin\x86_64\ids_peak_ipl_dotnet.dll + + + + \ No newline at end of file diff --git a/Camera_connect/IDSCamera.cs b/Camera_connect/IDSCamera.cs new file mode 100644 index 0000000..ec4be33 --- /dev/null +++ b/Camera_connect/IDSCamera.cs @@ -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("Width").SetValue(5472); + //nodeMapRemoteDevice1.FindNode("Height").SetValue(3648); + nodeMapRemoteDevice1.FindNode("Width").SetValue(1920); + nodeMapRemoteDevice1.FindNode("Height").SetValue(1200); + nodeMapRemoteDevice1.FindNode("OffsetX").SetValue(0); + nodeMapRemoteDevice1.FindNode("OffsetY").SetValue(0); + //nodeMapRemoteDevice1.FindNode("ExposureAuto").SetCurrentEntry("Off"); + nodeMapRemoteDevice1.FindNode("AcquisitionMode").SetCurrentEntry("SingleFrame"); + dataStream1 = device1.DataStreams()[0].OpenDataStream(); + // Get the payload size for correct buffer allocation + UInt32 payloadSize = Convert.ToUInt32(nodeMapRemoteDevice1.FindNode("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("TLParamsLocked").SetValue(1); + dataStream1.StartAcquisition(); + nodeMapRemoteDevice1.FindNode("AcquisitionStart").Execute(); + nodeMapRemoteDevice1.FindNode("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("TLParamsLocked").SetValue(0); + } + catch + { + + } + } + public void SetExposureTime(double time) + { + try + { + nodeMapRemoteDevice1.FindNode("ExposureTime").SetValue(time); + } + catch + { + + } + + } + public double GetExposureTime() + { + try + { + return nodeMapRemoteDevice1.FindNode("ExposureTime").Value(); + } + catch + { + return 0; + } + + } + public Bitmap GetPicture() + { + Capture(); + return cam1Image; + } + + public void Destroy() + { + if (device1 == null) + return; + device1.Dispose(); + } + } +} diff --git a/Camera_connect/Main.Designer.cs b/Camera_connect/Main.Designer.cs new file mode 100644 index 0000000..b5b034a --- /dev/null +++ b/Camera_connect/Main.Designer.cs @@ -0,0 +1,124 @@ +namespace Camera_connect +{ + partial class Main + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + 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; + } +} diff --git a/Camera_connect/Main.cs b/Camera_connect/Main.cs new file mode 100644 index 0000000..d25820e --- /dev/null +++ b/Camera_connect/Main.cs @@ -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; // Ω󱱨s + 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 = "IDSsu\"; + } + else + { + label1.Text = "IDSsu"; + } + } + + 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; + } + + } +} diff --git a/Camera_connect/Main.resx b/Camera_connect/Main.resx new file mode 100644 index 0000000..8b2ff64 --- /dev/null +++ b/Camera_connect/Main.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Camera_connect/Program.cs b/Camera_connect/Program.cs new file mode 100644 index 0000000..030c229 --- /dev/null +++ b/Camera_connect/Program.cs @@ -0,0 +1,17 @@ +namespace Camera_connect +{ + internal static class Program + { + /// + /// The main entry point for the application. + /// + [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()); + } + } +} \ No newline at end of file