博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
basler 相机拍照简单类综合Emgu.CV---得到图档--原创
阅读量:4479 次
发布时间:2019-06-08

本文共 3178 字,大约阅读时间需要 10 分钟。

 

在网上找了半天都是下载要钱,自己试做了,经测试能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();
}
}
}
}

 

转载于:https://www.cnblogs.com/nulidemaomaochong/p/8490739.html

你可能感兴趣的文章
poj 2387 Dijkstra 模板
查看>>
求教关于ActionBar 样式问题
查看>>
hdu 5833 Zhu and 772002 ccpc网络赛 高斯消元法
查看>>
12.Linux中外接显示器分辨率不匹配的问题
查看>>
微信开发----JS-SDK接口
查看>>
微软嵌入式WEC2013产品研讨会(深圳站---2013.10.16)
查看>>
216. Combination Sum III java solutions
查看>>
sqlserver2008用sa登陆
查看>>
总是差和自由时差
查看>>
1196: [HNOI2006]公路修建问题 - BZOJ
查看>>
java 单例模式详解
查看>>
RHEL内核管理
查看>>
洛谷 3784(bzoj 4913) [SDOI2017]遗忘的集合——多项式求ln+MTT
查看>>
jQuery validation学习(1)验证只输入空格通过验证
查看>>
Sybase数据库异常紧急恢复
查看>>
抓包工具(查看协议)
查看>>
Nodejs - child_process - execFile(), exec(), spawn(), fork
查看>>
中介者模式小记【原创】
查看>>
转:Sublime Text 2 实用快捷键[Mac OS X]
查看>>
转: 技术牛人博客
查看>>