123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- using PaintDotNet.Setting;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Runtime.InteropServices;
- using System.Threading;
- using TUCAMAPI;
- using TUCamera;
- namespace PaintDotNet.ImageCollect.CameraManager
- {
- internal class MIChrome5Pro : CameraPublic
- {
- public bool m_isWaiting;
- public bool m_isRecording = false;
- public Thread m_waitingThread;
- public TUCAM_FRAME m_frame;
- public TUCAM_TRIGGER_ATTR m_triggerAttr = new TUCAM_TRIGGER_ATTR();
- public SetValueDel setValueDel;
- public CameraPreviewDialog cameraPreviewDialog;
- public CameraSettingDialog cameraSettingDialog;
- byte[] pBuf = null;
- object lockObj = new object();
- private TUCAM_OPEN m_handler;
- public TUCamera.TUCamera m_currentCamera;
- public CameraPreviewDialog CameraPreviewDialog
- {
- set
- {
- this.cameraPreviewDialog = value;
- }
- }
- public CameraSettingDialog CameraSettingDialog
- {
- set
- {
- this.cameraSettingDialog = value;
- }
- }
- public SetValueDel SetValueDel
- {
- set
- {
- this.setValueDel = value;
- }
- }
- public MIChrome5Pro()
- {
- m_currentCamera = TUCameraManager.GetInstance().GetCurrentCamera();
- m_handler = m_currentCamera.GetHandler();
- }
- /// <summary>
- /// 初始化
- /// </summary>
- public override void Init()
- {
- this.m_isRecording = false;
- this.m_triggerAttr.nTgrMode = (int)TUCAM_CAPTURE_MODES.TUCCM_SEQUENCE;
- this.m_triggerAttr.nFrames = 1;
- this.m_triggerAttr.nDelayTm = 0;
- this.m_triggerAttr.nExpMode = (int)TUCAM_TRIGGER_EXP.TUCTE_EXPTM;
- this.m_triggerAttr.nEdgeMode = (int)TUCAM_TRIGGER_EDGE.TUCTD_RISING;
-
- }
- /// <summary>
- /// 释放
- /// </summary>
- public override void Uninit()
- {
- //关闭所有打开的
- m_currentCamera.Close();
- }
- /// <summary>
- /// 开始预览
- /// </summary>
- ///
- public override void StartPreview()
- {
- this.StartWaitForFrame(this.m_handler);
- }
- /// <summary>
- /// 停止预览
- /// </summary>
- public override void StopPreview()
- {
- if (this.m_isWaiting)
- this.StopWaitForFrame(this.m_handler);
- }
- /// <summary>
- /// 获取当前分辨率
- /// </summary>
- /// <returns></returns>
- public unsafe override Size GetCurrentResolution()
- {
- /*
- Size size = new Size();
- TUCAM_VALUE_INFO valueInfo = new TUCAM_VALUE_INFO();
- valueInfo.nID = (int)TUCAM_IDINFO.TUIDI_CURRENT_WIDTH;
- TUCamAPI.TUCAM_Dev_GetInfo(this.m_opCamList[this.m_indexCam].hIdxTUCam, ref valueInfo);
- size.Width = valueInfo.nValue;
-
- valueInfo.nID = (int)TUCAM_IDINFO.TUIDI_CURRENT_HEIGHT;
- TUCamAPI.TUCAM_Dev_GetInfo(this.m_opCamList[this.m_indexCam].hIdxTUCam, ref valueInfo);
- size.Height = valueInfo.nValue;
- return size;
- */
- return m_currentCamera.GetResolution();
-
- }
- public void StopWaitForFrame(TUCAM_OPEN opCam)
- {
- if (!m_isWaiting)
- {
- return;
- }
- if (m_currentCamera.IsOpen())
- {
- m_isWaiting = false;
- TUCamAPI.TUCAM_Buf_AbortWait(opCam.hIdxTUCam);
- //m_waitingThread.Join();//存疑
- m_waitingThread.Abort();
- TUCamAPI.TUCAM_Cap_Stop(opCam.hIdxTUCam); // Stop capture
- TUCamAPI.TUCAM_Buf_Release(opCam.hIdxTUCam); // Release alloc buffer after stop capture and quit drawing thread
- }
- }
- public void StartWaitForFrame(TUCAM_OPEN opCam)
- {
- if (m_isWaiting)
- {
- return;
- }
- if (m_currentCamera.IsOpen())
- {
- m_isWaiting = true;
- m_waitingThread = new Thread(new ThreadStart(WaitForFrameThreadEntity));
- m_frame.pBuffer = IntPtr.Zero;
- m_frame.ucFormatGet = (byte)TUFRM_FORMATS.TUFRM_FMT_USUAl;
- m_frame.uiRsdSize = 1;
- TUCamAPI.TUCAM_Buf_Alloc(opCam.hIdxTUCam, ref m_frame); // Alloc buffer after set resolution or set ROI attribute
- TUCamAPI.TUCAM_Cap_Start(opCam.hIdxTUCam, (uint)m_triggerAttr.nTgrMode); // Start capture
- m_waitingThread.Start();
- }
- }
- /// <summary>
- /// 获取图像帧
- /// </summary>
- public void WaitForFrameThreadEntity()
- {
- IntPtr hIdxTUCam = this.m_handler.hIdxTUCam;
- while (m_isWaiting)
- {
- m_frame.ucFormatGet = (byte)TUFRM_FORMATS.TUFRM_FMT_USUAl;
- if (TUCAMRET.TUCAMRET_SUCCESS == TUCamAPI.TUCAM_Buf_WaitForFrame(hIdxTUCam, ref m_frame))
- {
- if (IntPtr.Zero != m_frame.pBuffer)
- {
- int nSize = (int)(m_frame.uiImgSize + m_frame.usHeader);
- if(pBuf==null) pBuf = new byte[nSize];
- Marshal.Copy(m_frame.pBuffer, pBuf, 0, nSize);
- Buffer.BlockCopy(pBuf, (int)(m_frame.usHeader), pBuf, 0, (int)(m_frame.uiImgSize));
- if(cameraPreviewDialog!=null && !cameraPreviewDialog.IsDisposed)
- {
- this.cameraPreviewDialog.Invoke((EventHandler)(delegate
- {
- setValueDel(pBuf, lockObj);
- }));
- }
- /* if (cameraSettingDialog != null && !cameraSettingDialog.IsDisposed)
- {
- this.cameraSettingDialog.Invoke((EventHandler)(delegate
- {
- setValueDel(pBuf, lockObj);
- }));
- }*/
- }
- }
- }
- }
- public void ManualRelease()
- {
- if (pBuf != null)
- {
- pBuf = null;
- }
- if (m_waitingThread != null)
- {
- if (m_waitingThread.ThreadState == ThreadState.Stopped)
- {
- m_waitingThread = null;
- }
- }
- this.Dispose();
- this.Close();
- GC.Collect();
- }
- /// <summary>
- /// 判断颜色灰度值 如果是true 则设置为灰色
- /// </summary>
- /// <param name="isGray"></param>
- public void SetColorModel(bool isGray) {
-
- if (m_currentCamera.IsOpen())
- {
- TUCamAPI.TUCAM_Capa_SetValue(this.m_handler.hIdxTUCam, (int)TUCAM_IDCAPA.TUIDC_MONOCHROME, isGray ? 1 : 0);
- }
- }
- }
- }
|