加入basler程式
This commit is contained in:
parent
c3c5cf3e4a
commit
4bcbd8e920
132
Camera_connect/BaslerCamera.cs
Normal file
132
Camera_connect/BaslerCamera.cs
Normal file
@ -0,0 +1,132 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Drawing;
|
||||||
|
using System.Drawing.Imaging;
|
||||||
|
using Basler.Pylon;
|
||||||
|
using System.Windows.Forms;
|
||||||
|
|
||||||
|
namespace Camera_connect
|
||||||
|
{
|
||||||
|
public class BaslerCamera
|
||||||
|
{
|
||||||
|
private readonly Main form;
|
||||||
|
private readonly object lockObject = new object();
|
||||||
|
public int CameraNumber => CameraFinder.Enumerate().Count;
|
||||||
|
public event Action<Bitmap> CameraImageEvent;
|
||||||
|
|
||||||
|
private readonly List<Camera> allCameras = new List<Camera>();
|
||||||
|
private readonly PixelDataConverter pxConvert = new PixelDataConverter();
|
||||||
|
private bool isGrabbing;
|
||||||
|
|
||||||
|
public BaslerCamera(Main form)
|
||||||
|
{
|
||||||
|
this.form = form;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CameraInit()
|
||||||
|
{
|
||||||
|
var allCameraInfos = CameraFinder.Enumerate();
|
||||||
|
allCameras.Clear();
|
||||||
|
|
||||||
|
foreach (var cameraInfo in allCameraInfos)
|
||||||
|
{
|
||||||
|
var camera = new Camera(cameraInfo);
|
||||||
|
camera.CameraOpened += Configuration.AcquireContinuous;
|
||||||
|
camera.StreamGrabber.GrabStarted += StreamGrabber_GrabStarted;
|
||||||
|
camera.StreamGrabber.ImageGrabbed += StreamGrabber_ImageGrabbed;
|
||||||
|
camera.Open();
|
||||||
|
allCameras.Add(camera);
|
||||||
|
}
|
||||||
|
|
||||||
|
isGrabbing = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Close()
|
||||||
|
{
|
||||||
|
foreach (var camera in allCameras)
|
||||||
|
{
|
||||||
|
camera.StreamGrabber.Stop();
|
||||||
|
camera.Close();
|
||||||
|
camera.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
allCameras.Clear();
|
||||||
|
isGrabbing = false;
|
||||||
|
MessageBox.Show("相機已斷開連接");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OneShot()
|
||||||
|
{
|
||||||
|
if (isGrabbing && allCameras.Count > 0)
|
||||||
|
{
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
var camera = allCameras[0];
|
||||||
|
camera.Parameters[PLCamera.AcquisitionMode].SetValue(PLCamera.AcquisitionMode.Continuous);
|
||||||
|
camera.StreamGrabber.Start(GrabStrategy.OneByOne, GrabLoop.ProvidedByStreamGrabber);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MessageBox.Show("請確保相機已連接!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Stop()
|
||||||
|
{
|
||||||
|
if (allCameras.Count > 0)
|
||||||
|
{
|
||||||
|
var camera = allCameras[0];
|
||||||
|
camera.StreamGrabber.Stop(); // 停止抓取
|
||||||
|
allCameras[0].StreamGrabber.Stop();
|
||||||
|
CameraImageEvent = null; // 解除訂閱事件,防止影像更新
|
||||||
|
isGrabbing = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MessageBox.Show("無可用的相機,請先連接 Basler 相機!", "停止失敗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void StreamGrabber_ImageGrabbed(object sender, ImageGrabbedEventArgs e)
|
||||||
|
{
|
||||||
|
lock (lockObject)
|
||||||
|
{
|
||||||
|
var grabResult = e.GrabResult;
|
||||||
|
if (grabResult.IsValid)
|
||||||
|
{
|
||||||
|
var bitmap = GrabResult2Bmp(grabResult);
|
||||||
|
CameraImageEvent?.Invoke(bitmap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void StreamGrabber_GrabStarted(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
isGrabbing = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Bitmap GrabResult2Bmp(IGrabResult grabResult)
|
||||||
|
{
|
||||||
|
var bitmap = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppArgb);
|
||||||
|
var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
|
||||||
|
|
||||||
|
pxConvert.OutputPixelFormat = PixelType.BGRA8packed;
|
||||||
|
pxConvert.Convert(bitmapData.Scan0, bitmapData.Stride * bitmap.Height, grabResult);
|
||||||
|
|
||||||
|
bitmap.UnlockBits(bitmapData);
|
||||||
|
return bitmap;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -9,11 +9,20 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Emgu.CV" Version="4.10.0.5680" />
|
||||||
|
<PackageReference Include="Emgu.CV.Bitmap" Version="4.10.0.5680" />
|
||||||
|
<PackageReference Include="Emgu.CV.UI" Version="4.10.0.5680" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Reference Include="Basler.Pylon">
|
||||||
|
<HintPath>libs\Basler.Pylon.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
<Reference Include="ids_peak_dotnet">
|
<Reference Include="ids_peak_dotnet">
|
||||||
<HintPath>C:\Program Files\IDS\ids_peak\generic_sdk\samples\bin\x86_64\ids_peak_dotnet.dll</HintPath>
|
<HintPath>libs\ids_peak_dotnet.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="ids_peak_ipl_dotnet">
|
<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>
|
<HintPath>libs\ids_peak_ipl_dotnet.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
@ -10,57 +10,118 @@ using System.IO;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Reflection.Emit;
|
using System.Reflection.Emit;
|
||||||
|
using Basler.Pylon;
|
||||||
|
using Emgu.CV;
|
||||||
|
|
||||||
namespace Camera_connect
|
namespace Camera_connect
|
||||||
{
|
{
|
||||||
public partial class Main : Form
|
public partial class Main : Form
|
||||||
{
|
{
|
||||||
|
private 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 Main()
|
public Main()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
}
|
}
|
||||||
//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();
|
||||||
Label_status.Text = "IDS連線中...";
|
|
||||||
|
|
||||||
// 讀取用戶輸入
|
if (string.IsNullOrWhiteSpace(selectedCamera))
|
||||||
string IDS_serialnumber = camera_serialnumber.Text;
|
|
||||||
|
|
||||||
// 檢查序列號是否為空
|
|
||||||
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
|
if (selectedCamera == "IDS")
|
||||||
{
|
{
|
||||||
// 啟動連線(異步處理以保持界面響應)
|
// 顯示連線狀態
|
||||||
bool connectionResult = await Task.Run(() => IDS_camera.CameraInit(IDS_serialnumber));
|
Label_status.Text = "IDS連線中...";
|
||||||
|
|
||||||
if (connectionResult)
|
// 讀取用戶輸入
|
||||||
|
string IDS_serialnumber = camera_serialnumber.Text;
|
||||||
|
|
||||||
|
// 檢查序列號是否為空
|
||||||
|
if (string.IsNullOrWhiteSpace(IDS_serialnumber))
|
||||||
{
|
{
|
||||||
// 設置曝光時間
|
Label_status.Text = "請輸入相機序號";
|
||||||
IDS_camera.SetExposureTime(10000);
|
MessageBox.Show("相機序號不能為空!", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||||
Label_status.Text = "IDS連線成功";
|
return;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
Label_status.Text = "IDS連線失敗";
|
// 啟動連線(異步處理以保持界面響應)
|
||||||
|
bool connectionResult = await Task.Run(() => IDS_camera.CameraInit(IDS_serialnumber));
|
||||||
|
|
||||||
|
if (connectionResult)
|
||||||
|
{
|
||||||
|
// 設置曝光時間
|
||||||
|
IDS_camera.SetExposureTime(10000);
|
||||||
|
Label_status.Text = "IDS連線成功";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Label_status.Text = "IDS連線失敗";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Label_status.Text = "連線失敗";
|
||||||
|
MessageBox.Show($"連線時發生錯誤:{ex.Message}", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
else if (selectedCamera == "Basler")
|
||||||
{
|
{
|
||||||
Label_status.Text = "連線失敗";
|
try
|
||||||
MessageBox.Show($"連線時發生錯誤:{ex.Message}", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
{
|
||||||
|
// 顯示連線狀態
|
||||||
|
Label_status.Text = "Basler連線中...";
|
||||||
|
|
||||||
|
// 初始化 Basler 相機
|
||||||
|
Basler = new BaslerCamera(this);
|
||||||
|
|
||||||
|
bool connectionResult = await Task.Run(() =>
|
||||||
|
{
|
||||||
|
if (Basler.CameraNumber > 0)
|
||||||
|
{
|
||||||
|
Basler.CameraImageEvent += Camera_CameraImageEvent; // 訂閱事件
|
||||||
|
Basler.CameraInit();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (connectionResult)
|
||||||
|
{
|
||||||
|
Label_status.Text = "Basler連線成功";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Label_status.Text = "未檢測到 Basler 相機";
|
||||||
|
MessageBox.Show("未檢測到 Basler 相機,請確認相機連接是否正常!", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Label_status.Text = "Basler連線失敗";
|
||||||
|
MessageBox.Show($"連線 Basler 相機時發生錯誤:{ex.Message}", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Label_status.Text = "請選擇有效的相機類型";
|
||||||
|
MessageBox.Show("請選擇有效的相機類型!", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,24 +130,88 @@ namespace Camera_connect
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (IDS_camera != null)
|
string? selectedCamera = comboBox1.SelectedItem?.ToString();
|
||||||
|
|
||||||
|
if (selectedCamera == "IDS")
|
||||||
{
|
{
|
||||||
Bitmap image = IDS_camera.GetPicture();
|
if (IDS_camera != null)
|
||||||
if (image != null)
|
|
||||||
{
|
{
|
||||||
pictureBox1.Image = image;
|
Bitmap image = IDS_camera.GetPicture();
|
||||||
|
if (image != null)
|
||||||
|
{
|
||||||
|
pictureBox1.Image = image;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (selectedCamera == "Basler")
|
||||||
|
{
|
||||||
|
if (Basler != null)
|
||||||
|
{
|
||||||
|
Basler.OneShot(); // 單次拍攝
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MessageBox.Show("請先連線 Basler 相機!", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
MessageBox.Show($"拍攝影像時發生錯誤:{ex.Message}", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void bt_KeepShot_Click(object sender, EventArgs e)
|
private void bt_KeepShot_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
string? selectedCamera = comboBox1.SelectedItem?.ToString();
|
||||||
|
|
||||||
|
if (selectedCamera == "IDS")
|
||||||
|
{
|
||||||
|
// IDS 相機的連續拍攝邏輯
|
||||||
|
StartKeepShot(() => IDS_camera.GetPicture());
|
||||||
|
}
|
||||||
|
else if (selectedCamera == "Basler")
|
||||||
|
{
|
||||||
|
// Basler 相機的連續拍攝邏輯
|
||||||
|
if (Basler != null)
|
||||||
|
{
|
||||||
|
if (Basler != null)
|
||||||
|
{
|
||||||
|
Basler.KeepShot(); // 啟動連續拍攝
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MessageBox.Show("請先連線 Basler 相機!", "連線資訊", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void bt_Stop_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
if (!isKeepShotting)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string? selectedCamera = comboBox1.SelectedItem?.ToString();
|
||||||
|
|
||||||
|
if (selectedCamera == "IDS")
|
||||||
|
{
|
||||||
|
cts.Cancel();
|
||||||
|
isKeepShotting = false;
|
||||||
|
}
|
||||||
|
else if (selectedCamera == "Basler")
|
||||||
|
{
|
||||||
|
if (Basler != null)
|
||||||
|
{
|
||||||
|
Basler.Stop();
|
||||||
|
Basler.CameraImageEvent -= Camera_CameraImageEvent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void StartKeepShot(Func<Bitmap> captureMethod)
|
||||||
{
|
{
|
||||||
if (isKeepShotting)
|
if (isKeepShotting)
|
||||||
{
|
{
|
||||||
@ -103,36 +228,35 @@ namespace Camera_connect
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (IDS_camera != null)
|
// 調用傳入的捕獲方法
|
||||||
|
Bitmap image = captureMethod();
|
||||||
|
if (image != null)
|
||||||
{
|
{
|
||||||
Bitmap image = IDS_camera.GetPicture();
|
// 更新 PictureBox 圖像
|
||||||
if (image != null)
|
pictureBox1.Invoke((MethodInvoker)delegate
|
||||||
{
|
{
|
||||||
pictureBox1.Invoke((MethodInvoker)delegate
|
pictureBox1.Image = image;
|
||||||
{
|
});
|
||||||
pictureBox1.Image = image;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
// 可以選擇記錄日誌或顯示錯誤訊息
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, token);
|
}, token);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void bt_Stop_Click(object sender, EventArgs e)
|
private void Camera_CameraImageEvent(Bitmap bmp)
|
||||||
{
|
{
|
||||||
if (!isKeepShotting)
|
// 直接更新單一 PictureBox1
|
||||||
|
pictureBox1.BeginInvoke(new MethodInvoker(delegate
|
||||||
{
|
{
|
||||||
|
Bitmap old = pictureBox1.Image as Bitmap;
|
||||||
return;
|
pictureBox1.Image = bmp;
|
||||||
}
|
if (old != null)
|
||||||
|
old.Dispose();
|
||||||
cts.Cancel();
|
}));
|
||||||
isKeepShotting = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
BIN
Camera_connect/libs/Basler.Pylon.dll
Normal file
BIN
Camera_connect/libs/Basler.Pylon.dll
Normal file
Binary file not shown.
BIN
Camera_connect/libs/ids_peak_dotnet.dll
Normal file
BIN
Camera_connect/libs/ids_peak_dotnet.dll
Normal file
Binary file not shown.
BIN
Camera_connect/libs/ids_peak_ipl_dotnet.dll
Normal file
BIN
Camera_connect/libs/ids_peak_ipl_dotnet.dll
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user