救救我啊我救我

This commit is contained in:
JEFF 2025-01-16 20:53:21 +08:00
parent 987ef1083c
commit 3a309775a0
3 changed files with 91 additions and 76 deletions

View File

@ -54,6 +54,7 @@ namespace Camera_connect
} }
allCameras.Clear(); allCameras.Clear();
isCameraOpen = false;
isGrabbing = false; isGrabbing = false;
MessageBox.Show("相機已斷開連接"); MessageBox.Show("相機已斷開連接");
} }
@ -74,18 +75,18 @@ namespace Camera_connect
public void KeepShot() public void KeepShot()
{ {
if (isGrabbing && allCameras.Count > 0) if (!isGrabbing || allCameras.Count == 0)
{
var camera = allCameras[0];
camera.Parameters[PLCamera.AcquisitionMode].SetValue(PLCamera.AcquisitionMode.Continuous);
camera.StreamGrabber.Start(GrabStrategy.OneByOne, GrabLoop.ProvidedByStreamGrabber);
}
else
{ {
MessageBox.Show("請確保相機已連接!"); MessageBox.Show("請確保相機已連接!");
return;
} }
var camera = allCameras[0];
camera.Parameters[PLCamera.AcquisitionMode].SetValue(PLCamera.AcquisitionMode.Continuous);
camera.StreamGrabber.Start(GrabStrategy.OneByOne, GrabLoop.ProvidedByStreamGrabber);
} }
public void Stop() public void Stop()
{ {
if (allCameras.Count > 0) if (allCameras.Count > 0)
@ -94,8 +95,8 @@ namespace Camera_connect
camera.StreamGrabber.Stop(); // 停止抓取 camera.StreamGrabber.Stop(); // 停止抓取
allCameras[0].StreamGrabber.Stop(); allCameras[0].StreamGrabber.Stop();
CameraImageEvent = null; // 解除訂閱事件,防止影像更新 CameraImageEvent = null; // 解除訂閱事件,防止影像更新
isGrabbing = false; isGrabbing = true;
isCameraOpen = false;
} }
else else
{ {

View File

@ -28,58 +28,59 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
bt_connect = new Button(); Bt_connect = new Button();
bt_OneShot = new Button(); Bt_OneShot = new Button();
bt_KeepShot = new Button(); Bt_KeepShot = new Button();
bt_Stop = new Button(); Bt_Stop = new Button();
pictureBox1 = new PictureBox(); pictureBox1 = new PictureBox();
Label_status = new Label(); Label_status = new Label();
comboBox1 = new ComboBox(); comboBox1 = new ComboBox();
camera_serialnumber = new TextBox(); camera_serialnumber = new TextBox();
label2 = new Label(); label2 = new Label();
label3 = new Label(); label3 = new Label();
Bt_disconnect = new Button();
((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit();
SuspendLayout(); SuspendLayout();
// //
// bt_connect // Bt_connect
// //
bt_connect.Location = new Point(99, 75); Bt_connect.Location = new Point(99, 75);
bt_connect.Name = "bt_connect"; Bt_connect.Name = "Bt_connect";
bt_connect.Size = new Size(116, 63); Bt_connect.Size = new Size(116, 63);
bt_connect.TabIndex = 0; Bt_connect.TabIndex = 0;
bt_connect.Text = "相機連線"; Bt_connect.Text = "相機連線";
bt_connect.UseVisualStyleBackColor = true; Bt_connect.UseVisualStyleBackColor = true;
bt_connect.Click += bt_connect_Click; Bt_connect.Click += Bt_connect_Click;
// //
// bt_OneShot // Bt_OneShot
// //
bt_OneShot.Location = new Point(99, 242); Bt_OneShot.Location = new Point(99, 242);
bt_OneShot.Name = "bt_OneShot"; Bt_OneShot.Name = "Bt_OneShot";
bt_OneShot.Size = new Size(116, 63); Bt_OneShot.Size = new Size(116, 63);
bt_OneShot.TabIndex = 1; Bt_OneShot.TabIndex = 1;
bt_OneShot.Text = "單張擷取"; Bt_OneShot.Text = "單張擷取";
bt_OneShot.UseVisualStyleBackColor = true; Bt_OneShot.UseVisualStyleBackColor = true;
bt_OneShot.Click += bt_OneShot_Click; Bt_OneShot.Click += Bt_OneShot_Click;
// //
// bt_KeepShot // Bt_KeepShot
// //
bt_KeepShot.Location = new Point(243, 242); Bt_KeepShot.Location = new Point(243, 242);
bt_KeepShot.Name = "bt_KeepShot"; Bt_KeepShot.Name = "Bt_KeepShot";
bt_KeepShot.Size = new Size(116, 63); Bt_KeepShot.Size = new Size(116, 63);
bt_KeepShot.TabIndex = 2; Bt_KeepShot.TabIndex = 2;
bt_KeepShot.Text = "連續取像"; Bt_KeepShot.Text = "連續取像";
bt_KeepShot.UseVisualStyleBackColor = true; Bt_KeepShot.UseVisualStyleBackColor = true;
bt_KeepShot.Click += bt_KeepShot_Click; Bt_KeepShot.Click += Bt_KeepShot_Click;
// //
// bt_Stop // Bt_Stop
// //
bt_Stop.Location = new Point(385, 242); Bt_Stop.Location = new Point(385, 242);
bt_Stop.Name = "bt_Stop"; Bt_Stop.Name = "Bt_Stop";
bt_Stop.Size = new Size(116, 63); Bt_Stop.Size = new Size(116, 63);
bt_Stop.TabIndex = 3; Bt_Stop.TabIndex = 3;
bt_Stop.Text = "停止"; Bt_Stop.Text = "停止";
bt_Stop.UseVisualStyleBackColor = true; Bt_Stop.UseVisualStyleBackColor = true;
bt_Stop.Click += bt_Stop_Click; Bt_Stop.Click += Bt_Stop_Click;
// //
// pictureBox1 // pictureBox1
// //
@ -102,7 +103,7 @@
// //
comboBox1.FormattingEnabled = true; comboBox1.FormattingEnabled = true;
comboBox1.Items.AddRange(new object[] { "Basler", "IDS" }); comboBox1.Items.AddRange(new object[] { "Basler", "IDS" });
comboBox1.Location = new Point(300, 75); comboBox1.Location = new Point(424, 74);
comboBox1.Name = "comboBox1"; comboBox1.Name = "comboBox1";
comboBox1.Size = new Size(182, 31); comboBox1.Size = new Size(182, 31);
comboBox1.TabIndex = 6; comboBox1.TabIndex = 6;
@ -132,21 +133,32 @@
label3.TabIndex = 11; label3.TabIndex = 11;
label3.Text = "相機序號"; label3.Text = "相機序號";
// //
// Bt_disconnect
//
Bt_disconnect.Location = new Point(263, 75);
Bt_disconnect.Name = "Bt_disconnect";
Bt_disconnect.Size = new Size(116, 63);
Bt_disconnect.TabIndex = 12;
Bt_disconnect.Text = "中斷連線";
Bt_disconnect.UseVisualStyleBackColor = true;
Bt_disconnect.Click += Bt_disconnect_Click;
//
// Main // Main
// //
AutoScaleDimensions = new SizeF(11F, 23F); AutoScaleDimensions = new SizeF(11F, 23F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1746, 1294); ClientSize = new Size(1746, 1294);
Controls.Add(Bt_disconnect);
Controls.Add(label3); Controls.Add(label3);
Controls.Add(label2); Controls.Add(label2);
Controls.Add(camera_serialnumber); Controls.Add(camera_serialnumber);
Controls.Add(comboBox1); Controls.Add(comboBox1);
Controls.Add(Label_status); Controls.Add(Label_status);
Controls.Add(pictureBox1); Controls.Add(pictureBox1);
Controls.Add(bt_Stop); Controls.Add(Bt_Stop);
Controls.Add(bt_KeepShot); Controls.Add(Bt_KeepShot);
Controls.Add(bt_OneShot); Controls.Add(Bt_OneShot);
Controls.Add(bt_connect); Controls.Add(Bt_connect);
Name = "Main"; Name = "Main";
Text = "Form1"; Text = "Form1";
((System.ComponentModel.ISupportInitialize)pictureBox1).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBox1).EndInit();
@ -156,15 +168,16 @@
#endregion #endregion
private Button bt_connect; private Button Bt_connect;
private Button bt_OneShot; private Button Bt_OneShot;
private Button bt_KeepShot; private Button Bt_KeepShot;
private Button bt_Stop; private Button Bt_Stop;
private PictureBox pictureBox1; private PictureBox pictureBox1;
private Label Label_status; private Label Label_status;
private ComboBox comboBox1; private ComboBox comboBox1;
private TextBox camera_serialnumber; private TextBox camera_serialnumber;
private Label label2; private Label label2;
private Label label3; private Label label3;
private Button Bt_disconnect;
} }
} }

View File

@ -29,7 +29,7 @@ namespace Camera_connect
} }
//IDS測試相機序號 4103372214 //IDS測試相機序號 4103372214
private async void bt_connect_Click(object sender, EventArgs e) private async void Bt_connect_Click(object sender, EventArgs e)
{ {
string? selectedCamera = comboBox1.SelectedItem?.ToString(); string? selectedCamera = comboBox1.SelectedItem?.ToString();
@ -125,7 +125,7 @@ namespace Camera_connect
} }
} }
private void bt_OneShot_Click(object sender, EventArgs e) private void Bt_OneShot_Click(object sender, EventArgs e)
{ {
try try
{ {
@ -161,7 +161,7 @@ namespace Camera_connect
} }
} }
private void bt_KeepShot_Click(object sender, EventArgs e) private void Bt_KeepShot_Click(object sender, EventArgs e)
{ {
string? selectedCamera = comboBox1.SelectedItem?.ToString(); string? selectedCamera = comboBox1.SelectedItem?.ToString();
if (isKeepShotting == true) return; if (isKeepShotting == true) return;
@ -175,20 +175,17 @@ namespace Camera_connect
// Basler 相機的連續拍攝邏輯 // Basler 相機的連續拍攝邏輯
if (Basler != null) if (Basler != null)
{ {
if (Basler != null) Basler.KeepShot();
{ isKeepShotting = true;
Basler.KeepShot(); // 啟動連續拍攝 }
isKeepShotting = true; else
} {
else MessageBox.Show("請先連線 Basler 相機!", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Warning);
{
MessageBox.Show("請先連線 Basler 相機!", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
} }
} }
} }
private void bt_Stop_Click(object sender, EventArgs e) private void Bt_Stop_Click(object sender, EventArgs e)
{ {
if (!isKeepShotting) if (!isKeepShotting)
{ {
@ -208,9 +205,9 @@ namespace Camera_connect
if (Basler != null) if (Basler != null)
{ {
Basler.Stop(); Basler?.Stop();
Basler.CameraImageEvent -= Camera_CameraImageEvent; isKeepShotting = false;
Label_status.Text = "basler結束連續取像"; Label_status.Text = "Basler結束連續取像";
} }
} }
} }
@ -256,14 +253,18 @@ namespace Camera_connect
private void Camera_CameraImageEvent(Bitmap bmp) private void Camera_CameraImageEvent(Bitmap bmp)
{ {
// 直接更新單一 PictureBox1 pictureBox1.Invoke((MethodInvoker)delegate
pictureBox1.BeginInvoke(new MethodInvoker(delegate
{ {
Bitmap old = pictureBox1.Image as Bitmap; Bitmap? old = pictureBox1.Image as Bitmap;
pictureBox1.Image = bmp; pictureBox1.Image = bmp;
if (old != null) old?.Dispose();
old.Dispose(); });
})); }
private void Bt_disconnect_Click(object sender, EventArgs e)
{
Basler.Close();
Label_status.Text = "相機中斷連線";
} }
} }
} }