修改程式0118
This commit is contained in:
parent
f170514531
commit
ed1681a2e4
@ -1,173 +1,292 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
|
||||||
using System.Drawing.Imaging;
|
using System.Drawing.Imaging;
|
||||||
|
using System.Drawing;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Basler.Pylon;
|
using Basler.Pylon;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Threading;
|
||||||
|
using static System.Windows.Forms.DataFormats;
|
||||||
|
|
||||||
namespace Camera_connect
|
namespace Camera_connect
|
||||||
{
|
{
|
||||||
public class BaslerCamera(Main form)
|
public class BaslerCamera
|
||||||
|
{
|
||||||
|
private readonly Main form;
|
||||||
|
public BaslerCamera(Main form)
|
||||||
{
|
{
|
||||||
private readonly Main form = form;
|
|
||||||
private readonly object lockObject = new();
|
|
||||||
public int CameraNumber => CameraFinder.Enumerate().Count;
|
|
||||||
public event Action<Bitmap> CameraImageEvent;
|
|
||||||
|
|
||||||
private readonly List<Camera> allCameras = [];
|
this.form = form;
|
||||||
private readonly PixelDataConverter pxConvert = new();
|
|
||||||
private bool isGrabbing;
|
|
||||||
private bool isCameraOpen = false;
|
|
||||||
|
|
||||||
|
}
|
||||||
|
//連接相機各數
|
||||||
|
public int CameraNumber = CameraFinder.Enumerate().Count;
|
||||||
|
// 共享的鎖
|
||||||
|
private readonly object lockObject = new object();
|
||||||
|
//委託+事件=回調函數,用於傳遞相機抓取的圖像
|
||||||
|
public delegate void CameraImage(Bitmap bmp, int cameraIndex);
|
||||||
|
public event CameraImage CameraImageEvent;
|
||||||
|
|
||||||
|
//委託+事件=回調函數,用於傳遞相機抓取的圖像
|
||||||
|
//public delegate void CameraImage1(Bitmap bmp1);
|
||||||
|
//public event CameraImage1 CameraImageEvent1;
|
||||||
|
|
||||||
|
|
||||||
|
// 創建一個列表來儲存所有的相機
|
||||||
|
List<Camera> allCameras = new List<Camera>();
|
||||||
|
//Basler裡將相機採集到的圖像轉換成位圖
|
||||||
|
PixelDataConverter pxConvert = new PixelDataConverter();
|
||||||
|
|
||||||
|
//控制相機採集圖片的過程!
|
||||||
|
public bool Graber;
|
||||||
|
public bool freed;
|
||||||
|
public bool trigger;
|
||||||
|
//第一步初始化相機 所用的cameras里的事件然后绑定自己寫好的事件
|
||||||
public void CameraInit()
|
public void CameraInit()
|
||||||
{
|
{
|
||||||
var allCameraInfos = CameraFinder.Enumerate();
|
// 獲取所有可用相機的列表
|
||||||
allCameras.Clear();
|
List<ICameraInfo> allCameraInfos = CameraFinder.Enumerate();
|
||||||
if (isCameraOpen == true) return;
|
// 創建一個字典來儲存每個相機的識別碼和相機物件
|
||||||
|
Dictionary<string, Camera> cameras = new Dictionary<string, Camera>();
|
||||||
foreach (var cameraInfo in allCameraInfos)
|
foreach (ICameraInfo cameraInfo in allCameraInfos)
|
||||||
{
|
{
|
||||||
var camera = new Camera(cameraInfo);
|
// 創建一個新的相機物件
|
||||||
camera.CameraOpened += Configuration.AcquireContinuous;
|
Camera camera0 = new Camera(cameraInfo);
|
||||||
camera.StreamGrabber.GrabStarted += StreamGrabber_GrabStarted;
|
|
||||||
camera.StreamGrabber.ImageGrabbed += StreamGrabber_ImageGrabbed;
|
// 獲取相機的識別碼
|
||||||
camera.Open();
|
string id = camera0.CameraInfo[CameraInfoKey.SerialNumber];
|
||||||
allCameras.Add(camera);
|
Console.WriteLine(id);
|
||||||
isCameraOpen = true; ;
|
// 將相機物件儲存到字典中
|
||||||
|
cameras[id] = camera0;
|
||||||
|
}
|
||||||
|
Graber = true;
|
||||||
|
freed = true;
|
||||||
|
//相當於初始化了一下相機的動態鏈庫 可以調用Camera裡面的東西
|
||||||
|
// 清空 allCameras 列表
|
||||||
|
allCameras.Clear();
|
||||||
|
// 將字典中的所有相機添加到列表中
|
||||||
|
foreach (string id in cameras.Keys)
|
||||||
|
{
|
||||||
|
allCameras.Add(cameras[id]);
|
||||||
|
}
|
||||||
|
// 為每個相機設定事件處理程序
|
||||||
|
for (int i = 0; i < allCameras.Count; i++)
|
||||||
|
{
|
||||||
|
//自由連接模式
|
||||||
|
//當連接到像機自動適應此配置
|
||||||
|
//打开後,添加到Basler.Pylon.ICamera中。
|
||||||
|
allCameras[i].CameraOpened += Configuration.AcquireContinuous;
|
||||||
|
//抓取开始事件
|
||||||
|
allCameras[i].StreamGrabber.GrabStarted += StreamGrabber_GrabStarted;
|
||||||
|
//抓取圖片事件
|
||||||
|
//allCameras[i].StreamGrabber.ImageGrabbed += (sender, e) => StreamGrabber_ImageGrabbed(sender, e, i);
|
||||||
|
//打开相機
|
||||||
|
allCameras[i].Open();
|
||||||
}
|
}
|
||||||
|
|
||||||
isGrabbing = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 斷開連接
|
||||||
|
/// </summary>
|
||||||
public void Close()
|
public void Close()
|
||||||
{
|
{
|
||||||
foreach (var camera in allCameras)
|
for (int i = 0; i < allCameras.Count; i++)
|
||||||
{
|
{
|
||||||
camera.StreamGrabber.Stop();
|
//停止連接
|
||||||
camera.Close();
|
allCameras[i].StreamGrabber.Stop();
|
||||||
camera.Dispose();
|
}
|
||||||
|
//釋放相機資源
|
||||||
|
DestroyCamera();
|
||||||
}
|
}
|
||||||
|
|
||||||
allCameras.Clear();
|
/// <summary>
|
||||||
isCameraOpen = false;
|
/// 擷取圖片
|
||||||
isGrabbing = false;
|
/// </summary>
|
||||||
MessageBox.Show("相機已斷開連接");
|
/// <param name="sender"></param>
|
||||||
}
|
/// <param name="e"></param>
|
||||||
|
// 創建一個Bitmap類型的陣列來存儲影像
|
||||||
public void OneShot()
|
private readonly Bitmap[] images;
|
||||||
{
|
// 定義一個可以存放 IGrabResult 物件的陣列
|
||||||
if (isGrabbing && allCameras.Count > 0)
|
private readonly IGrabResult[] grabResults;
|
||||||
{
|
|
||||||
var camera = allCameras[0];
|
|
||||||
camera.Parameters[PLCamera.AcquisitionMode].SetValue(PLCamera.AcquisitionMode.SingleFrame);
|
|
||||||
camera.StreamGrabber.Start(1, GrabStrategy.OneByOne, GrabLoop.ProvidedByStreamGrabber);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
MessageBox.Show("請確保相機已連接!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void KeepShot()
|
|
||||||
{
|
|
||||||
if (!isGrabbing || allCameras.Count == 0)
|
|
||||||
{
|
|
||||||
MessageBox.Show("請確保相機已連接!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var camera = allCameras[0];
|
|
||||||
camera.Parameters[PLCamera.AcquisitionMode].SetValue(PLCamera.AcquisitionMode.Continuous);
|
|
||||||
camera.StreamGrabber.Start(GrabStrategy.OneByOne, GrabLoop.ProvidedByStreamGrabber);
|
|
||||||
|
|
||||||
camera.StreamGrabber.ImageGrabbed += (sender, e) =>
|
|
||||||
{
|
|
||||||
var grabResult = e.GrabResult;
|
|
||||||
if (grabResult.IsValid)
|
|
||||||
{
|
|
||||||
Bitmap bmp = GrabResultToBitmap(grabResult);
|
|
||||||
UpdatePictureBox(bmp);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// 將 IGrabResult 轉換為 Bitmap
|
|
||||||
private Bitmap GrabResultToBitmap(IGrabResult grabResult)
|
|
||||||
{
|
|
||||||
Bitmap bmp = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppArgb);
|
|
||||||
BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);
|
|
||||||
|
|
||||||
pxConvert.OutputPixelFormat = PixelType.BGRA8packed;
|
|
||||||
pxConvert.Convert(bitmapData.Scan0, bitmapData.Stride * bmp.Height, grabResult);
|
|
||||||
|
|
||||||
bmp.UnlockBits(bitmapData);
|
|
||||||
return bmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新 PictureBox 的顯示
|
|
||||||
private void UpdatePictureBox(Bitmap bmp)
|
|
||||||
{
|
|
||||||
form.Invoke((MethodInvoker)delegate
|
|
||||||
{
|
|
||||||
Bitmap resizedImage = new Bitmap(form.pictureBox1.Width, form.pictureBox1.Height);
|
|
||||||
using (Graphics g = Graphics.FromImage(resizedImage))
|
|
||||||
{
|
|
||||||
g.DrawImage(bmp, 0, 0, form.pictureBox1.Width, form.pictureBox1.Height);
|
|
||||||
}
|
|
||||||
|
|
||||||
Bitmap? oldImage = form.pictureBox1.Image as Bitmap;
|
|
||||||
form.pictureBox1.Image = resizedImage;
|
|
||||||
oldImage?.Dispose();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void Stop()
|
private void StreamGrabber_ImageGrabbed(object sender, ImageGrabbedEventArgs e, int cameraIndex)
|
||||||
{
|
{
|
||||||
if (allCameras.Count > 0)
|
|
||||||
{
|
|
||||||
var camera = allCameras[0];
|
|
||||||
camera.StreamGrabber.Stop(); // 停止抓取
|
|
||||||
allCameras[0].StreamGrabber.Stop();
|
|
||||||
CameraImageEvent = null; // 解除訂閱事件,防止影像更新
|
|
||||||
isGrabbing = true;
|
|
||||||
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
MessageBox.Show("無可用的相機,請先連接 Basler 相機!", "停止失敗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void StreamGrabber_ImageGrabbed(object sender, ImageGrabbedEventArgs e)
|
|
||||||
{
|
|
||||||
lock (lockObject)
|
lock (lockObject)
|
||||||
{
|
{
|
||||||
var grabResult = e.GrabResult;
|
IGrabResult grabResult = e.GrabResult;
|
||||||
if (grabResult.IsValid)
|
if (grabResult.IsValid && Graber)
|
||||||
{
|
{
|
||||||
var bitmap = GrabResult2Bmp(grabResult);
|
//委托就用到了!
|
||||||
CameraImageEvent?.Invoke(bitmap);
|
CameraImageEvent(GrabResult2Bmp(grabResult), cameraIndex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//private void StreamGrabber_ImageGrabbed1(object sender, ImageGrabbedEventArgs e)
|
||||||
|
//{
|
||||||
|
// lock (lockObject)
|
||||||
|
// {
|
||||||
|
// IGrabResult grabResult1 = e.GrabResult;
|
||||||
|
// if (grabResult1.IsValid && Graber)
|
||||||
|
// {
|
||||||
|
// // 將 IGrabResult 物件存入陣列
|
||||||
|
// grabResults[1] = grabResult1;
|
||||||
|
// // 在需要時轉換為 Bitmap 物件
|
||||||
|
// images[1] = GrabResult2Bmp(grabResults[1]);
|
||||||
|
// //委托就用到了!
|
||||||
|
// CameraImageEvent1(images[1]);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 開始擷取
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
/// <param name="e"></param>
|
||||||
private void StreamGrabber_GrabStarted(object sender, EventArgs e)
|
private void StreamGrabber_GrabStarted(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
isGrabbing = true;
|
Graber = true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Bitmap GrabResult2Bmp(IGrabResult grabResult)
|
/// <summary>
|
||||||
|
/// 釋放相機
|
||||||
|
/// </summary>
|
||||||
|
public void DestroyCamera()
|
||||||
{
|
{
|
||||||
var bitmap = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppArgb);
|
if (freed == true)
|
||||||
var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
|
{
|
||||||
|
for (int i = 0; i < allCameras.Count; i++)
|
||||||
|
{
|
||||||
|
allCameras[i].Close();
|
||||||
|
allCameras[i].Dispose();
|
||||||
|
}
|
||||||
|
freed = false;
|
||||||
|
Graber = false;
|
||||||
|
trigger = false;
|
||||||
|
MessageBox.Show("相機已斷開連接");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 將相機圖相轉成Bitmap
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="grabResult"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
|
||||||
|
Bitmap GrabResult2Bmp(IGrabResult grabResult)
|
||||||
|
{
|
||||||
|
// PixelFormat format:新的像素格式 System.Drawing.Bitmap。 这必须指定一个值,开头 Format。(参1:图宽,参2:图高,参3:像素格式)
|
||||||
|
Bitmap b = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppArgb);
|
||||||
|
//BitmapData:指定位图像的属性
|
||||||
|
BitmapData bitmapData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, b.PixelFormat);
|
||||||
|
//PixelType.BGRA8packed:抓取结果返回像素类型,由图像处理支持类使用
|
||||||
pxConvert.OutputPixelFormat = PixelType.BGRA8packed;
|
pxConvert.OutputPixelFormat = PixelType.BGRA8packed;
|
||||||
pxConvert.Convert(bitmapData.Scan0, bitmapData.Stride * bitmap.Height, grabResult);
|
IntPtr bmpIntpr = bitmapData.Scan0;
|
||||||
|
pxConvert.Convert(bmpIntpr, bitmapData.Stride * b.Height, grabResult);
|
||||||
|
b.UnlockBits(bitmapData);
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
public void judge(int i)
|
||||||
|
{
|
||||||
|
if (trigger == false)
|
||||||
|
{
|
||||||
|
trigger = true;
|
||||||
|
allCameras[i].StreamGrabber.ImageGrabbed += (sender, e) => StreamGrabber_ImageGrabbed(sender, e, i);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bitmap.UnlockBits(bitmapData);
|
|
||||||
return bitmap;
|
public void OneShot(int i)
|
||||||
|
{
|
||||||
|
|
||||||
|
judge(i);
|
||||||
|
|
||||||
|
if (Graber == true)
|
||||||
|
{
|
||||||
|
if (i < CameraNumber)
|
||||||
|
{
|
||||||
|
//PLCamera:所有可用于basler摄像机设备dByStre的参数名称列表 SingleFrame:启用单帧采集模式。
|
||||||
|
allCameras[i].Parameters[PLCamera.AcquisitionMode].SetValue(PLCamera.AcquisitionMode.SingleFrame);
|
||||||
|
allCameras[i].StreamGrabber.Start(1, GrabStrategy.OneByOne, GrabLoop.ProvidedByStreamGrabber);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MessageBox.Show("請確保連接相機數量!!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MessageBox.Show("請連接相機!!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void KeepShot(int i)
|
||||||
|
{
|
||||||
|
|
||||||
|
judge(i);
|
||||||
|
if (Graber == true)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (i < CameraNumber)
|
||||||
|
{
|
||||||
|
//Continuous:啟用連續採集模式。
|
||||||
|
allCameras[i].Parameters[PLCamera.AcquisitionMode].SetValue(PLCamera.AcquisitionMode.Continuous);
|
||||||
|
//camera.StreamGrabber.Start(GrabStrategy.OneByOne, GrabLoop.ProvidedByStreamGrabber);
|
||||||
|
lock (lockObject)
|
||||||
|
{
|
||||||
|
//GrabStrategy.OneByOne:默認抓取策略
|
||||||
|
//GrabLoop.ProvidedByStreamGrabber:定義額外線呈的使用
|
||||||
|
allCameras[i].StreamGrabber.Start(GrabStrategy.OneByOne, GrabLoop.ProvidedByStreamGrabber);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MessageBox.Show("請確保連接相機數量!!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MessageBox.Show("請連接相機!!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void Stop(int i)
|
||||||
|
{
|
||||||
|
if (allCameras != null)
|
||||||
|
{
|
||||||
|
if (i < CameraNumber)
|
||||||
|
{
|
||||||
|
//擷取停止
|
||||||
|
allCameras[i].StreamGrabber.Stop();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MessageBox.Show("請確保連接相機數量!!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
MessageBox.Show("該相機已停止擷取");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
1
Camera_connect/Main.Designer.cs
generated
1
Camera_connect/Main.Designer.cs
generated
@ -87,6 +87,7 @@
|
|||||||
pictureBox1.Location = new Point(99, 328);
|
pictureBox1.Location = new Point(99, 328);
|
||||||
pictureBox1.Name = "pictureBox1";
|
pictureBox1.Name = "pictureBox1";
|
||||||
pictureBox1.Size = new Size(1219, 940);
|
pictureBox1.Size = new Size(1219, 940);
|
||||||
|
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
|
||||||
pictureBox1.TabIndex = 4;
|
pictureBox1.TabIndex = 4;
|
||||||
pictureBox1.TabStop = false;
|
pictureBox1.TabStop = false;
|
||||||
//
|
//
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -17,17 +17,24 @@ namespace Camera_connect
|
|||||||
{
|
{
|
||||||
public partial class Main : Form
|
public partial class Main : Form
|
||||||
{
|
{
|
||||||
private BaslerCamera Basler;
|
private readonly BaslerCamera Basler;
|
||||||
public IDSCamera IDS_camera = new IDSCamera();
|
public IDSCamera IDS_camera = new IDSCamera();
|
||||||
private bool isKeepShotting = false; // 用於控制連續拍攝
|
private bool isKeepShotting = false; // 用於控制連續拍攝
|
||||||
private Task keepShotTask; // 用於執行拍攝任務
|
private Task keepShotTask; // 用於執行拍攝任務
|
||||||
private CancellationTokenSource cts; // 用於取消拍攝任務
|
private CancellationTokenSource cts; // 用於取消拍攝任務
|
||||||
public PictureBox switchbox; //轉換相機圖像用
|
public PictureBox switchbox; //轉換相機圖像用
|
||||||
|
public List<PictureBox> pictureBoxes;
|
||||||
public Main()
|
public Main()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
Basler = new BaslerCamera(this);
|
||||||
|
Basler.CameraImageEvent += Camera_CameraImageEvent;
|
||||||
|
pictureBoxes = new List<PictureBox> { pictureBox1 /*, 其他 PictureBox 控件 */ };
|
||||||
|
Bt_Stop.Enabled = false;
|
||||||
|
Bt_OneShot.Enabled = false;
|
||||||
|
Bt_KeepShot.Enabled = false;
|
||||||
}
|
}
|
||||||
//IDS測試相機序號 4103372214
|
//IDS測試相機序號 4103372214
|
||||||
|
|
||||||
private async void Bt_connect_Click(object sender, EventArgs e)
|
private async void Bt_connect_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
@ -35,64 +42,65 @@ namespace Camera_connect
|
|||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(selectedCamera))
|
if (string.IsNullOrWhiteSpace(selectedCamera))
|
||||||
{
|
{
|
||||||
Label_status.Text = "請選擇相機類型";
|
Label_status.Text = "請選擇相機類型";
|
||||||
MessageBox.Show("請先選擇相機類型!", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
MessageBox.Show("請先選擇相機類型!", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (selectedCamera == "IDS")
|
if (selectedCamera == "IDS")
|
||||||
{
|
{
|
||||||
// 顯示連線狀態
|
// 顯示連線狀態
|
||||||
Label_status.Text = "IDS連線中...";
|
Label_status.Text = "IDS連線中...";
|
||||||
|
|
||||||
// 讀取用戶輸入
|
// 讀取用戶輸入
|
||||||
string IDS_serialnumber = camera_serialnumber.Text;
|
string IDS_serialnumber = camera_serialnumber.Text;
|
||||||
|
|
||||||
// 檢查序列號是否為空
|
// 檢查序列號是否為空
|
||||||
if (string.IsNullOrWhiteSpace(IDS_serialnumber))
|
if (string.IsNullOrWhiteSpace(IDS_serialnumber))
|
||||||
{
|
{
|
||||||
Label_status.Text = "請輸入相機序號";
|
Label_status.Text = "請輸入相機序號";
|
||||||
MessageBox.Show("相機序號不能為空!", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
MessageBox.Show("相機序號不能為空!", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// 啟動連線(異步處理以保持界面響應)
|
// 啟動連線(異步處理以保持界面響應)
|
||||||
bool connectionResult = await Task.Run(() => IDS_camera.CameraInit(IDS_serialnumber));
|
bool connectionResult = await Task.Run(() => IDS_camera.CameraInit(IDS_serialnumber));
|
||||||
|
|
||||||
if (connectionResult)
|
if (connectionResult)
|
||||||
{
|
{
|
||||||
// 設置曝光時間
|
// 設置曝光時間
|
||||||
IDS_camera.SetExposureTime(10000);
|
IDS_camera.SetExposureTime(10000);
|
||||||
Label_status.Text = "IDS連線成功";
|
Label_status.Text = "IDS連線成功";
|
||||||
|
Bt_OneShot.Enabled = true;
|
||||||
|
Bt_KeepShot.Enabled = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Label_status.Text = "IDS連線失敗";
|
Label_status.Text = "IDS連線失敗";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Label_status.Text = "連線失敗";
|
Label_status.Text = "連線失敗";
|
||||||
MessageBox.Show($"連線時發生錯誤:{ex.Message}", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
MessageBox.Show($"連線時發生錯誤:{ex.Message}", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (selectedCamera == "Basler")
|
else if (selectedCamera == "Basler")
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// 顯示連線狀態
|
// 顯示連線狀態
|
||||||
Label_status.Text = "Basler連線中...";
|
Label_status.Text = "Basler連線中...";
|
||||||
|
|
||||||
// 初始化 Basler 相機
|
// 初始化 Basler 相機
|
||||||
Basler = new BaslerCamera(this);
|
//Basler = new BaslerCamera(this);
|
||||||
|
|
||||||
bool connectionResult = await Task.Run(() =>
|
bool connectionResult = await Task.Run(() =>
|
||||||
{
|
{
|
||||||
if (Basler.CameraNumber > 0)
|
if (Basler.CameraNumber > 0)
|
||||||
{
|
{
|
||||||
Basler.CameraImageEvent += Camera_CameraImageEvent; // 訂閱事件
|
|
||||||
Basler.CameraInit();
|
Basler.CameraInit();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -104,24 +112,26 @@ namespace Camera_connect
|
|||||||
|
|
||||||
if (connectionResult)
|
if (connectionResult)
|
||||||
{
|
{
|
||||||
Label_status.Text = "Basler連線成功";
|
Label_status.Text = "Basler連線成功";
|
||||||
|
Bt_OneShot.Enabled = true;
|
||||||
|
Bt_KeepShot.Enabled = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Label_status.Text = "未檢測到 Basler 相機";
|
Label_status.Text = "未檢測到 Basler 相機";
|
||||||
MessageBox.Show("未檢測到 Basler 相機,請確認相機連接是否正常!", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
MessageBox.Show("未檢測到 Basler 相機,請確認相機連接是否正常!", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Label_status.Text = "Basler連線失敗";
|
Label_status.Text = "Basler連線失敗";
|
||||||
MessageBox.Show($"連線 Basler 相機時發生錯誤:{ex.Message}", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
MessageBox.Show($"連線 Basler 相機時發生錯誤:{ex.Message}", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Label_status.Text = "請選擇有效的相機類型";
|
Label_status.Text = "請選擇有效的相機類型";
|
||||||
MessageBox.Show("請選擇有效的相機類型!", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
MessageBox.Show("請選擇有效的相機類型!", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,7 +149,7 @@ namespace Camera_connect
|
|||||||
if (image != null)
|
if (image != null)
|
||||||
{
|
{
|
||||||
pictureBox1.Image = image;
|
pictureBox1.Image = image;
|
||||||
Label_status.Text = "IDS單張擷取";
|
Label_status.Text = "IDS單張擷取";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -148,18 +158,18 @@ namespace Camera_connect
|
|||||||
if (isKeepShotting == true) return;
|
if (isKeepShotting == true) return;
|
||||||
if (Basler != null)
|
if (Basler != null)
|
||||||
{
|
{
|
||||||
Basler.OneShot(); // 單次拍攝
|
Basler.OneShot(0); // 單次拍攝
|
||||||
Label_status.Text = "Basler單張擷取";
|
Label_status.Text = "Basler單張擷取";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
MessageBox.Show("請先連線 Basler 相機!", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
MessageBox.Show("請先連線 Basler 相機!", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
MessageBox.Show($"拍攝影像時發生錯誤:{ex.Message}", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
MessageBox.Show($"拍攝影像時發生錯誤:{ex.Message}", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,24 +179,27 @@ namespace Camera_connect
|
|||||||
if (isKeepShotting == true) return;
|
if (isKeepShotting == true) return;
|
||||||
if (selectedCamera == "IDS")
|
if (selectedCamera == "IDS")
|
||||||
{
|
{
|
||||||
// IDS 相機的連續拍攝邏輯
|
// IDS 相機的連續拍攝邏輯
|
||||||
StartKeepShot(() => IDS_camera.GetPicture());
|
StartKeepShot(() => IDS_camera.GetPicture());
|
||||||
Label_status.Text = "IDS連續取像";
|
Label_status.Text = "IDS連續取像";
|
||||||
}
|
}
|
||||||
else if (selectedCamera == "Basler")
|
else if (selectedCamera == "Basler")
|
||||||
{
|
{
|
||||||
// Basler 相機的連續拍攝邏輯
|
// Basler 相機的連續拍攝邏輯
|
||||||
if (Basler != null)
|
if (Basler != null)
|
||||||
{
|
{
|
||||||
Basler.KeepShot();
|
Basler.KeepShot(0);
|
||||||
isKeepShotting = true;
|
isKeepShotting = true;
|
||||||
Label_status.Text = "Basler連續取像";
|
Label_status.Text = "Basler連續取像";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
MessageBox.Show("請先連線 Basler 相機!", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
MessageBox.Show("請先連線 Basler 相機!", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Bt_OneShot.Enabled = false;
|
||||||
|
Bt_KeepShot.Enabled = false;
|
||||||
|
Bt_Stop.Enabled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Bt_Stop_Click(object sender, EventArgs e)
|
private void Bt_Stop_Click(object sender, EventArgs e)
|
||||||
@ -200,20 +213,23 @@ namespace Camera_connect
|
|||||||
|
|
||||||
if (selectedCamera == "IDS")
|
if (selectedCamera == "IDS")
|
||||||
{
|
{
|
||||||
cts.Cancel(); // 停止拍攝
|
cts.Cancel(); // 停止拍攝
|
||||||
isKeepShotting = false;
|
isKeepShotting = false;
|
||||||
Label_status.Text = "IDS結束連續取像";
|
Label_status.Text = "IDS結束連續取像";
|
||||||
}
|
}
|
||||||
else if (selectedCamera == "Basler")
|
else if (selectedCamera == "Basler")
|
||||||
{
|
{
|
||||||
|
|
||||||
if (Basler != null)
|
if (Basler != null)
|
||||||
{
|
{
|
||||||
Basler?.Stop();
|
Basler?.Stop(0);
|
||||||
isKeepShotting = false;
|
isKeepShotting = false;
|
||||||
Label_status.Text = "Basler結束連續取像";
|
Label_status.Text = "Basler結束連續取像";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Bt_Stop.Enabled = false;
|
||||||
|
Bt_OneShot.Enabled = true;
|
||||||
|
Bt_KeepShot.Enabled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void StartKeepShot(Func<Bitmap> captureMethod)
|
private void StartKeepShot(Func<Bitmap> captureMethod)
|
||||||
@ -233,44 +249,49 @@ namespace Camera_connect
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// 調用傳入的捕獲方法
|
// 調用傳入的捕獲方法
|
||||||
Bitmap image = captureMethod();
|
Bitmap image = captureMethod();
|
||||||
if (image != null)
|
if (image != null)
|
||||||
{
|
{
|
||||||
// 更新 PictureBox 圖像
|
// 更新 PictureBox 圖像
|
||||||
pictureBox1.Invoke((MethodInvoker)delegate
|
pictureBox1.Invoke((MethodInvoker)delegate
|
||||||
{
|
{
|
||||||
Bitmap? oldImage = pictureBox1.Image as Bitmap;
|
Bitmap? oldImage = pictureBox1.Image as Bitmap;
|
||||||
pictureBox1.Image = image;
|
pictureBox1.Image = image;
|
||||||
oldImage?.Dispose(); // 確保釋放舊的 Bitmap 資源
|
oldImage?.Dispose(); // 確保釋放舊的 Bitmap 資源
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
// 可以選擇記錄日誌或顯示錯誤訊息
|
// 可以選擇記錄日誌或顯示錯誤訊息
|
||||||
Console.WriteLine($"捕獲影像時發生錯誤:{ex.Message}");
|
Console.WriteLine($"捕獲影像時發生錯誤:{ex.Message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, token);
|
}, token);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Camera_CameraImageEvent(Bitmap bmp)
|
private void Camera_CameraImageEvent(Bitmap bmp, int cameraIndex)
|
||||||
{
|
{
|
||||||
pictureBox1.Invoke((MethodInvoker)delegate
|
// 確保 cameraIndex 不超出範圍
|
||||||
|
if (cameraIndex >= 0 && cameraIndex < pictureBoxes.Count)
|
||||||
{
|
{
|
||||||
// 縮放影像以適應 PictureBox
|
// 根據 cameraIndex 決定要更新哪個 pictureBox
|
||||||
Bitmap resizedImage = new Bitmap(pictureBox1.Width, pictureBox1.Height);
|
PictureBox pictureBoxToUpdate = pictureBoxes[cameraIndex];
|
||||||
using (Graphics g = Graphics.FromImage(resizedImage))
|
|
||||||
{
|
|
||||||
g.DrawImage(bmp, 0, 0, pictureBox1.Width, pictureBox1.Height);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新 PictureBox
|
//使用委托进行跨线程交互 防止线程卡死状态在Invoke前面加一个Begin
|
||||||
Bitmap? old = pictureBox1.Image as Bitmap;
|
pictureBoxToUpdate.BeginInvoke(new MethodInvoker(delegate
|
||||||
pictureBox1.Image = resizedImage;
|
{
|
||||||
old?.Dispose();
|
Bitmap? old = pictureBoxToUpdate.Image as Bitmap;
|
||||||
});
|
pictureBoxToUpdate.Image = bmp;
|
||||||
|
if (old != null)
|
||||||
|
old.Dispose();
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine($"無效的相機索引:{cameraIndex}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Bt_disconnect_Click(object sender, EventArgs e)
|
private void Bt_disconnect_Click(object sender, EventArgs e)
|
||||||
@ -278,12 +299,12 @@ namespace Camera_connect
|
|||||||
string? selectedCamera = comboBox1.SelectedItem?.ToString();
|
string? selectedCamera = comboBox1.SelectedItem?.ToString();
|
||||||
if (selectedCamera == "IDS")
|
if (selectedCamera == "IDS")
|
||||||
{
|
{
|
||||||
Label_status.Text = "IDS相機中斷連線";
|
Label_status.Text = "IDS相機中斷連線";
|
||||||
}
|
}
|
||||||
else if(selectedCamera == "Basler")
|
else if(selectedCamera == "Basler")
|
||||||
{
|
{
|
||||||
Basler.Close();
|
Basler.Close();
|
||||||
Label_status.Text = "Basler相機中斷連線";
|
Label_status.Text = "Basler相機中斷連線";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user