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();
}

}

}
}

 

posted on 2018-03-01 21:55  努力的毛毛虫  阅读(2305)  评论(2编辑  收藏  举报

导航