Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- namespace TH.Parking.Wrapper.HBCamera
- {
- public class HB18TCamera
- {
- private IntPtr m_channelHandle;
- private string m_imagestoreFolderPath;
- private string m_videostoreFolderPath;
- private FileStream m_fStream;
- private string m_recordFilePath = string.Empty;
- private int m_maxFileSize = 0;
- private ulong m_CallBackChannelHandle = 100;
- private IntPtr[] m_cameraHandle;
- public IntPtr[] CameraHandleChannels
- {
- get { return m_cameraHandle; }
- set { m_cameraHandle = value; }
- }
- public ulong CallBackChannelHandle
- {
- get { return m_CallBackChannelHandle; }
- set { m_CallBackChannelHandle = value; }
- }
- public int MaxRecordFileSize
- {
- get { return m_maxFileSize; }
- set { m_maxFileSize = value; }
- }
- public static string ImageFolderPath = "";
- public HB18TCamera(IntPtr handleChannel, string imageFolderPath, string videoFolderPath, int maxRecordFileSize)
- {
- m_channelHandle = handleChannel;
- m_imagestoreFolderPath = imageFolderPath;
- m_videostoreFolderPath = videoFolderPath;
- m_maxFileSize = maxRecordFileSize;
- if (!Directory.Exists(this.m_imagestoreFolderPath))
- Directory.CreateDirectory(this.m_imagestoreFolderPath);
- if (!Directory.Exists(this.m_imagestoreFolderPath))
- Directory.CreateDirectory(this.m_imagestoreFolderPath);
- }
- public static void AddWatermark(string watermarkText, string inputFilePath, string directionText = "")
- {
- FileStream fs = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read);
- System.Drawing.Image bitmap = (System.Drawing.Image)Bitmap.FromStream(fs);
- fs.Close();
- fs.Dispose();
- Font font = new Font("Courier New", 15, FontStyle.Bold, GraphicsUnit.Pixel);
- Point atpoint = new Point(120, 20);
- SolidBrush brush = new SolidBrush(Color.White);
- Graphics graphics = Graphics.FromImage(bitmap);
- StringFormat sf = new StringFormat();
- sf.Alignment = StringAlignment.Center;
- sf.LineAlignment = StringAlignment.Center;
- graphics.DrawString(watermarkText, font, brush, atpoint, sf);
- atpoint = new Point(30, 330);
- if (string.IsNullOrEmpty(directionText))
- {
- directionText = "Vào";
- }
- else
- {
- directionText = "Ra";
- }
- graphics.DrawString(directionText, font, brush, atpoint, sf);
- graphics.Dispose();
- MemoryStream m = new MemoryStream();
- bitmap.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
- FileStream fileStr = File.OpenWrite(inputFilePath);
- m.WriteTo(fileStr);
- fileStr.Flush();
- fileStr.Close();
- }
- public IntPtr ChannelHandle
- {
- get { return m_channelHandle; }
- set { m_channelHandle = value; }
- }
- public string ImageStoreFolderPath
- {
- get { return m_imagestoreFolderPath; }
- set { m_imagestoreFolderPath = value; }
- }
- public string VideoStoreFolderPath
- {
- get { return m_videostoreFolderPath; }
- set { m_videostoreFolderPath = value; }
- }
- public static string GetUniqueFilePath(string filePath)
- {
- int tmp = 1;
- while (File.Exists(filePath))
- {
- tmp++;
- filePath = filePath.Replace("M.", string.Format("M({0}).", tmp.ToString()));
- }
- return filePath;
- }
- //[System.Runtime.InteropServices.DllImport("Kernal32")]
- [DllImport("kernel32", SetLastError = true)]
- private extern static Boolean CloseHandle(IntPtr handle);
- public string CaptureImage()
- {
- string nowString = DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss_tt");
- string timeString = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss tt");
- string fileName = nowString + ".bmp";
- string fullPath = GetUniqueFilePath(this.m_imagestoreFolderPath + "\\" + fileName);
- IntPtr imgBuff = IntPtr.Zero;
- try
- {
- imgBuff = new IntPtr(704 * 576 * 2);
- ulong size = (704 * 576 * 2);
- int tmp = T1800.T18_GetOriginalImage(this.m_channelHandle, imgBuff, ref size, 1);
- int bRet = -1;
- if (size == 352 * 288 * 2)
- bRet = T1800.T18_SaveYUVToBmpFile(fullPath, imgBuff, 352, 288, 1);
- else if (size == 704 * 288 * 2)
- bRet = T1800.T18_SaveYUVToBmpFile(fullPath, imgBuff, 704, 288, 1);
- else if (size == 704 * 576 * 2)
- bRet = T1800.T18_SaveYUVToBmpFile(fullPath, imgBuff, 704, 576, 1);
- else if (size == 352 * 240 * 2)
- bRet = T1800.T18_SaveYUVToBmpFile(fullPath, imgBuff, 352, 240, 1);
- else if ((size == 704 * 240 * 2))
- bRet = T1800.T18_SaveYUVToBmpFile(fullPath, imgBuff, 704, 240, 1);
- else if (size == 704 * 480 * 2)
- bRet = T1800.T18_SaveYUVToBmpFile(fullPath, imgBuff, 704, 480, 1);
- if (bRet > -1 && !string.IsNullOrEmpty(fullPath))
- {
- AddWatermark(timeString, fullPath);
- }
- }
- catch
- {
- fullPath = string.Empty;
- }
- finally
- {
- if (imgBuff != IntPtr.Zero)
- {
- CloseHandle(imgBuff);
- imgBuff = IntPtr.Zero;
- }
- }
- return fullPath;
- }
- public void BeginRecord(bool reqShowPreview = false)
- {
- if (!CameraUtilities.AllowRecordVideo)
- return;
- Log.Logger.Error("Begin record called");
- string fileName = DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss_tt") + ".264";
- bool bRes = true;
- try
- {
- int tmpError = -1;
- m_recordFilePath = GetUniqueFilePath(this.m_videostoreFolderPath + "\\" + fileName);
- tmpError = T1800.T18_SetupSubChannel(this.m_channelHandle, 0);
- tmpError = T1800.T18_StartVideoCapture(this.m_channelHandle);
- Log.Logger.Error(tmpError);
- tmpError = T1800.T18_SetupSubChannel(this.m_channelHandle, 1);
- if (m_fStream != null) m_fStream.Close();
- if (File.Exists(m_recordFilePath)) File.Delete(m_recordFilePath);
- m_fStream = new FileStream(m_recordFilePath, FileMode.OpenOrCreate);
- if (tmpError == -1) bRes = false;
- if (reqShowPreview)
- StartVideoPreview();
- Log.Logger.Error("Begin recod");
- }
- catch (Exception ex)
- {
- Log.Logger.Error(ex);
- bRes = false;
- }
- }
- public bool EndRecord(bool bForceStopReview = false)
- {
- if (!CameraUtilities.AllowRecordVideo)
- return false;
- bool bRes = true;
- Log.Logger.Error("End record called");
- try
- {
- T1800.T18_SetupSubChannel(this.m_channelHandle, 0);
- T1800.T18_StopVideoCapture(this.m_channelHandle);
- T1800.T18_SetupSubChannel(this.m_channelHandle, 1);
- if (m_fStream != null)
- {
- Log.Logger.Error(m_fStream.Length);
- m_fStream.Flush();
- m_fStream.Close();
- m_fStream = null;
- }
- else
- {
- Log.Logger.Error("Stream null");
- }
- if (bForceStopReview)
- StopVideoPreview();
- Log.Logger.Error("Endded recod");
- }
- catch (Exception ex)
- {
- Log.Logger.Error("End record error");
- bRes = false;
- throw;
- }
- return bRes;
- }
- IntPtr _previewBoxHandle;
- Rectangle _rect;
- public bool StartVideoPreview(PictureBox picBox, bool forceRecord = true)
- {
- return StartVideoPreview(picBox.Handle, picBox.Width, picBox.Height, forceRecord);
- }
- public bool StartVideoPreview(IntPtr previewBoxHandle, int width, int height, bool forceRecord = true)
- {
- bool bRes = true;
- try
- {
- Rectangle rect = new Rectangle();
- _rect = rect;
- _previewBoxHandle = previewBoxHandle;
- rect.Size = new Size(width, height);
- int tmp = T1800.T18_StartVideoPreviewEx(m_channelHandle, previewBoxHandle, ref rect, false, 0, 30);
- if (tmp == -1) bRes = false;
- }
- catch
- {
- bRes = false;
- }
- if (forceRecord && bRes && CameraUtilities.AllowRecordVideo)
- BeginRecord();
- return bRes;
- }
- public bool StartVideoPreview(bool forceRecord = true)
- {
- bool bRes = true;
- try
- {
- int tmp = T1800.T18_StartVideoPreviewEx(m_channelHandle, _previewBoxHandle, ref _rect, false, 0, 30);
- if (tmp == -1) bRes = false;
- }
- catch
- {
- bRes = false;
- }
- if (bRes)
- BeginRecord();
- return bRes;
- }
- public bool StopVideoPreview(bool forceEndRecording = true)
- {
- if (forceEndRecording)
- EndRecord();
- bool bRes = false;
- bRes = T1800.T18_StopVideoPreview(m_channelHandle) != -1;
- return bRes;
- }
- public int SaveCallBackData()
- {
- Log.Logger.Error("SaveCallBackData");
- IntPtr m_pDataStream = IntPtr.Zero;
- Application.DoEvents();
- try
- {
- Application.DoEvents();
- GC.KeepAlive(m_fStream);
- GC.KeepAlive(m_maxFileSize);
- GC.KeepAlive(m_recordFilePath);
- GC.KeepAlive(m_imagestoreFolderPath);
- GC.KeepAlive(m_videostoreFolderPath);
- Application.DoEvents();
- byte[] bSystemHeader = new byte[200 * 1024];
- ulong nDataLength = 200 * 1024;
- m_pDataStream = new IntPtr(200 * 1024);
- int nFrameType = 0;
- Application.DoEvents();
- while (true)
- {
- Application.DoEvents();
- int nError = T1800.T18_ReadStreamData(this.m_channelHandle, bSystemHeader, ref nDataLength, ref nFrameType);
- Log.Logger.Error(nDataLength);
- if (m_fStream == null) m_fStream = new FileStream(m_recordFilePath, FileMode.OpenOrCreate);
- m_fStream.Write(bSystemHeader, 0, (int)nDataLength);
- m_fStream.Flush();
- Application.DoEvents();
- }
- }
- catch (Exception e)
- {
- Application.DoEvents();
- throw e;
- }
- finally
- {
- Application.DoEvents();
- if (m_pDataStream != IntPtr.Zero)
- {
- Application.DoEvents();
- //CloseHandle(m_pDataStream);
- m_pDataStream = IntPtr.Zero;
- }
- Application.DoEvents();
- }
- return 0;
- }
- public string GenerateBlackImage(string direction, string side)
- {
- throw new NotImplementedException();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment