basler 相機拍照簡單類綜合Emgu.CV---得到圖檔--原創


 

在網上找了半天都是下載要錢,自己試做了,經測試能ok,一起分享吧。給初學的人一點鼓勵。

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Basler.Pylon;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using Emgu.CV;
using Emgu.CV.Util;
using Emgu.CV.Structure;
namespace paiz
{
class basaler_tu :IDisposable
{
public Camera camera;
public Bitmap bittu;

public basaler_tu() //構造函數
{

camera = new Camera();  
camera.CameraOpened += Configuration.SoftwareTrigger;

//Open the connection to the camera device.
camera.Open();

// Set a handler for processing the images.
camera.StreamGrabber.ImageGrabbed += OnImageGrabbed;     
camera.StreamGrabber.Start(GrabStrategy.OneByOne, GrabLoop.ProvidedByStreamGrabber);
}
private void OnImageGrabbed(Object sender, ImageGrabbedEventArgs e)
{
// The grab result is automatically disposed when the event call back returns.
// The grab result can be cloned using IGrabResult.Clone if you want to keep a copy of it (not shown in this sample).
IGrabResult grabResult = e.GrabResult;
// Image grabbed successfully?
if (grabResult.GrabSucceeded)
{
// Access the image data.
byte[] buffer = grabResult.PixelData as byte[];

ImagePersistence.Save(ImageFileFormat.Bmp, "test.bmp", grabResult);  //保存圖片

}
else
{
string a=string.Format("Error: {0} {1}", grabResult.ErrorCode, grabResult.ErrorDescription);
MessageBox.Show(a);
}
}


public void paizhao()  //觸發拍照
{
Char key;
int num = 0;
do
{
      ///可以增加按鍵或其他IO開關

key = 't';

if ((key == 't' || key == 'T'))
{
// Execute the software trigger. Wait up to 100 ms until the camera is ready for trigger.
if (camera.WaitForFrameTriggerReady(100, TimeoutHandling.ThrowException))
{
camera.ExecuteSoftwareTrigger();
}
num++;
}
if (num == 3)     //一定要等待三次才能拍出來
{
key = 'e';
}
}
while ((key != 'e') && (key != 'E'));

 

}

public void stop()
{

camera.StreamGrabber.Stop();

// Close the connection to the camera device.
camera.Close();

// camera.Dispose();
}
public void Dispose()
{
camera.Dispose();

}
public Emgu.CV.Image<Bgr, byte> retun_tu()  //  這個直接調用前這個函數     在實際調用中,只要構造對象 后直接調用此函數就可以得到圖片去應用。
{
paizhao();
stop();
Dispose();
Emgu.CV.Image<Bgr, byte> tu1 = new Emgu.CV.Image<Bgr, byte>("test.bmp");

//如果要使用MAT類可以更好得到圖片

if (File.Exists("test.bmp"))
{
File.Delete("test.bmp");       //刪掉檔圖片
}
return tu1;
}
}
}

實際拍圖應用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Basler.Pylon;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.IO;
using Emgu.CV;
using Emgu.CV.Util;
using Emgu.CV.Structure;
using PylonLiveView;
namespace paiz
{
public partial class Form1 : Form
{


public Form1()
{
InitializeComponent();

}


private void Form1_Load(object sender, EventArgs e)
{


}

private void button1_Click(object sender, EventArgs e)
{


basaler_tu tu = new basaler_tu();
pictureBox1.Image= tu.retun_tu().ToBitmap();

}

private void button2_Click(object sender, EventArgs e)
{
using( Form_pylon nue = new Form_pylon())
{


nue.ShowDialog();
}

}

}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM