Dalsa 8K彩色相机Camera link C#采图
程序员文章站
2023-11-02 16:27:34
一个采图工具,所以界面做的很简单。 private SapAcquisition m_Acquisition; private SapBuffer m_Buffers; private SapAcqToBuf m_Xfer; private SapView m_View; private SapCo ......
一个采图工具,所以界面做的很简单。
private sapacquisition m_acquisition;
private sapbuffer m_buffers;
private sapacqtobuf m_xfer;
private sapview m_view;
private sapcolorconversion m_colorconv;
private sapprocessing m_pro;
private saplocation m_serverlocation;
private string m_configfilename;
private string m_executetime;
private bool m_issignaldetected;
private bool m_online;
public bool createnewobjects()
{
m_serverlocation = new saplocation("xtium-cl_mx4_1", 2);//第一位是显卡名称,第二位是选择模式的第几项
m_configfilename = "22n_no_name_default_default.ccf";//配置文件路径
m_acquisition = new sapacquisition(m_serverlocation, m_configfilename);
if (sapbuffer.isbuffertypesupported(m_serverlocation, sapbuffer.memorytype.scattergather))
m_buffers = new sapbufferwithtrash(2, m_acquisition, sapbuffer.memorytype.scattergather);
else
m_buffers = new sapbufferwithtrash(2, m_acquisition, sapbuffer.memorytype.scattergatherphysical);
m_xfer = new sapacqtobuf(m_acquisition, m_buffers);
m_colorconv = new sapcolorconversion(m_acquisition, m_buffers);
m_xfer.pairs[0].eventtype = sapxferpair.xfereventtype.endofframe;
m_xfer.xfernotify += new sapxfernotifyhandler(xfer_xfernotify);
m_xfer.xfernotifycontext = this;
m_pro = new sapmyprocessing(m_buffers, m_colorconv, new sapprocessingdonehandler(procallback), this);
if (!createobjects())
{
destroyobjects();
disposeobjects();
return false;
}
enablesignalstatus();
return true;
}
//这项函数官方demo是用来显示帧率的,但是我这里不需要所以就没写功能,但是要加这个函数,否则连续采集的时候就只会拍两张照片。
static void procallback(object sender, sapprocessingdoneeventargs pinfo)
{
form1 demo = pinfo.context as form1;
}
private bool createobjects()
{
// create acquisition object
if (m_acquisition != null && !m_acquisition.initialized)
{
if (m_acquisition.create() == false)
{
destroyobjects();
return false;
}
}
// create buffer object
if (m_buffers != null && !m_buffers.initialized)
{
if (m_buffers.create() == false)
{
destroyobjects();
return false;
}
m_buffers.clear();
}
// create color conversion object
if (m_colorconv != null && !m_colorconv.initialized)
{
if (m_colorconv.create() == false)
{
destroyobjects();
return false;
}
}
// create view object
if (m_view != null && !m_view.initialized)
{
if (m_colorconv != null && m_colorconv.initialized)
{
// set buffer to be viewed
// when using hardware color decoder, view the acquired rgb buffer,
// otherwise, for software color conversion, view the converted rgb buffer.
sapbuffer convbuffer = m_colorconv.outputbuffer;
if (convbuffer != null && convbuffer.initialized)
m_view.buffer = convbuffer;
else
m_view.buffer = m_buffers;
}
if (m_view.create() == false)
{
destroyobjects();
return false;
}
}
// create xfer object
if (m_xfer != null && !m_xfer.initialized)
{
if (m_xfer.create() == false)
{
destroyobjects();
return false;
}
m_xfer.autoempty = false;
}
// create processing object
if (m_pro != null && !m_pro.initialized)
{
if (!m_pro.create())
{
destroyobjects();
return false;
}
m_pro.autoempty = true;
}
return true;
}
private void destroyobjects()
{
if (m_xfer != null && m_xfer.initialized)
m_xfer.destroy();
if (m_pro != null && m_pro.initialized)
m_pro.destroy();
if (m_view != null && m_view.initialized)
m_view.destroy();
if (m_colorconv != null && m_colorconv.initialized)
m_colorconv.destroy();
if (m_buffers != null && m_buffers.initialized)
m_buffers.destroy();
if (m_acquisition != null && m_acquisition.initialized)
m_acquisition.destroy();
}
private void disposeobjects()
{
if (m_xfer != null)
{ m_xfer.dispose(); m_xfer = null; }
if (m_pro != null)
{ m_pro.dispose(); m_pro = null; }
if (m_view != null)
{ m_view.dispose(); m_view = null; }
if (m_colorconv != null)
{ m_colorconv.dispose(); m_colorconv = null; }
if (m_buffers != null)
{ m_buffers.dispose(); m_buffers = null; }
if (m_acquisition != null)
{ m_acquisition.dispose(); m_acquisition = null; }
}
private void enablesignalstatus()
{
if (m_acquisition != null)
{
m_issignaldetected = (m_acquisition.signalstatus != sapacquisition.acqsignalstatus.none);
// if (m_issignaldetected == false)
// statuslabelinfo.text = "online... no camera signal detected";
// else
// statuslabelinfo.text = "online... camera signal detected";
m_acquisition.signalnotifyenable = true;
}
}
static void xfer_xfernotify(object sender, sapxfernotifyeventargs argsnotify)
{
form1 demo = argsnotify.context as form1;
// if grabbing in trash buffer, do not display the image, update the
// appropriate number of frames on the status bar instead
if (argsnotify.trash)
return;
// refresh view
else
{
demo.takeimage();
demo.m_pro.executenext();
}
}
private void button1_click(object sender, eventargs e)
{
m_xfer.grab();
}
private void button2_click(object sender, eventargs e)
{
m_xfer.freeze();
}
private void takeimage()
{
try
{
sapbuffer m_pbuffer;
int m_startframe;
// sapbuffer.fileformat[] m_standardtypes;
m_pbuffer = m_buffers;
m_startframe = m_pbuffer.index; string m_option = "-format jpeg -quality 30";
m_buffers.save(textbox2.text, m_option, m_startframe,0);
}
catch (exception ex)
{
messagebox.show(ex.message.tostring());
}
}
关于采图,上面用的是官方的sdk方式,因为我采集的图片像素是8k*8k,原图bmp格式有256mb,压缩成30%质量的jpg也还有2.3mb左右。
还有一种方式是用bitmap来获取图片地址,但是需要注意的一点。
intptr addr;
int height = m_buffers.height;
int width = m_buffers.width;
m_buffers.getaddress(out addr);
bitmap = new bitmap(width, height, width * 4, system.drawing.imaging.pixelformat.format32bpprgb, addr);
这里的宽度是*4,原因是我的bmp原图图像位深度是32位的。所以保存用bitmap的方式需要先确定原图的位深度,否则保存图片会出现重影。