using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using TUCAMAPI; namespace TUCamera { class TUCameraImageSave : TUCameraIFrameProcess { private bool m_isDeal = true; private event DoneHandler m_handler; private string m_eventId; private int m_timeLapse; private int m_count; private TimeSpan m_startTime; private int intervalTime = 0; private int m_duration = 0; private string m_savePath; private string m_imagePrefix; private List m_imgList = new List(); private TUIMG_FORMATS m_imgFormat; private IntPtr m_hIdxTUCam; /// /// 拍摄固定张数图片 /// /// 文件保存路径 /// 文件名前缀 /// 文件保存格式 /// 时间间隔 /// 拍摄次数 /// 完成委托 /// 拍摄完成的图片List public TUCameraImageSave(ref string eventId, ref DoneHandler handler, string savePath, string imagePrefix, TUIMG_FORMATS imageFormat, int timeLapse, int count, IntPtr hIdxTUCam) { eventId = System.Guid.NewGuid().ToString("N"); m_savePath = savePath; m_imagePrefix = imagePrefix; m_imgFormat = imageFormat; m_timeLapse = timeLapse; m_count = count; m_handler = handler; m_hIdxTUCam = hIdxTUCam; m_eventId = eventId; } /// /// 最大速度持续一段时间拍摄 /// /// 文件保存路径 /// 文件名前缀 /// 文件保存格式 /// 持续拍摄时间 /// 完成委托 /// 拍摄完成的图片List public TUCameraImageSave(ref string eventId, ref DoneHandler handler, string savePath, string imagePrefix, TUIMG_FORMATS imageFormat, int duration, IntPtr hIdxTUCam) { eventId = System.Guid.NewGuid().ToString("N"); m_savePath = savePath; m_imagePrefix = imagePrefix; m_imgFormat = imageFormat; m_duration = duration; m_handler = handler; m_hIdxTUCam = hIdxTUCam; m_eventId = eventId; } public void doProcess(ref TUCAM_FRAME frame, bool pause) { if (!m_isDeal || pause) { return; } if (m_startTime == new TimeSpan(0)) { m_startTime = new TimeSpan(DateTime.Now.Ticks); } // 拍摄固定张数图片 if (m_duration == 0) { TimeSpan stopTime = new TimeSpan(DateTime.Now.Ticks); intervalTime = (int)m_startTime.Subtract(stopTime).Duration().TotalMilliseconds; if (intervalTime > m_timeLapse) { m_startTime = stopTime; saveImage(ref m_hIdxTUCam, ref frame); if (m_count <= 0) { stop(); } } } else { // 持续一直拍摄 if (m_duration < 0) { saveImage(ref m_hIdxTUCam, ref frame); return; } // 持续一段时间拍摄 TimeSpan stopTime = new TimeSpan(DateTime.Now.Ticks); intervalTime = (int)m_startTime.Subtract(stopTime).Duration().TotalMilliseconds; if (intervalTime <= m_duration) { saveImage(ref m_hIdxTUCam, ref frame); } else { stop(); } } } public bool isDeal() { return m_isDeal; } private void saveImage(ref IntPtr hIdxTUCam, ref TUCAM_FRAME frame) { DateTime now = DateTime.Now; string fileName = m_savePath + "\\" + m_imagePrefix + now.Year.ToString() + now.Month.ToString("D2") + now.Day.ToString("D2") + now.Hour.ToString("D2") + now.Minute.ToString("D2") + now.Second.ToString("D2") + now.Millisecond.ToString("D3"); // 保存JPG格式 // Save other format data(TIFF/BMP/PNG/JPEG) int fileFormat = (int)(m_imgFormat); TUCAM_FILE_SAVE fileSave; fileSave.pstrSavePath = Marshal.StringToHGlobalAnsi(fileName); // struct to IntPtr fileSave.pFrame = Marshal.AllocHGlobal(Marshal.SizeOf(frame)); Marshal.StructureToPtr(frame, fileSave.pFrame, true); fileSave.nSaveFmt = fileFormat; if (TUCAMRET.TUCAMRET_SUCCESS == TUCamAPI.TUCAM_File_SaveImage(hIdxTUCam, fileSave)) { m_imgList.Add(creatImageName(fileName, m_imgFormat)); if (m_count > 0) { m_count--; } } } private string creatImageName(string fileName, TUIMG_FORMATS imageFormat) { // string fileName = fileName; switch (imageFormat) { case TUIMG_FORMATS.TUFMT_PNG: fileName = fileName + ".png"; break; case TUIMG_FORMATS.TUFMT_BMP: fileName = fileName + ".bmp"; break; case TUIMG_FORMATS.TUFMT_TIF: fileName = fileName + ".tif"; break; default: fileName = fileName + ".jpg"; break; } return fileName; } public string getEventId() { return m_eventId; } public void stop() { m_isDeal = false; m_handler(m_eventId, true, 0, "结束", m_imgList); } } }