Advertisement
Guest User

Untitled

a guest
Feb 27th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Drawing;
  8. using System.Drawing.Imaging;
  9. using Emgu.CV;
  10. using Emgu.CV.Structure;
  11. using Emgu.CV.CvEnum;
  12. using System.Timers;
  13.  
  14. namespace HikvisionGrabber
  15. {
  16.     class Program
  17.     {
  18.         #region importCpp
  19.         [DllImport("hik_grabber_cpp.dll")]
  20.         public static extern IntPtr display_feed(IntPtr instance);
  21.         [DllImport("hik_grabber_cpp.dll")]
  22.         public static extern void wiper(IntPtr instance);
  23.         [DllImport("hik_grabber_cpp.dll")]
  24.         public static extern void pump(IntPtr instance);
  25.         [DllImport("hik_grabber_cpp.dll")]
  26.         public static extern void stop(IntPtr instance);
  27.         [DllImport("hik_grabber_cpp.dll")]
  28.         public static extern IntPtr hikCam_new(int connectTime, int reconnectTime, string ipAddress, int port, string userName, string password, bool hasWiper);
  29.         [DllImport("hik_grabber_cpp.dll")]
  30.         public static extern void test(IntPtr instance);
  31.         [DllImport("hik_grabber_cpp.dll")]
  32.         public static extern void getRes(IntPtr instance, ref int width, ref int height);
  33.         #endregion
  34.  
  35.  
  36.         class camera
  37.         {
  38.  
  39.  
  40.             public int width;
  41.             public int height;
  42.             public int size;
  43.             public int linesize;
  44.             public byte[] buffer;
  45.             public camera(string name = "cam", int connectTime = 2000, int reconnectTime = 10000, string ipAddress = "192.168.1.64", int port = 8000, string userName = "admin", string password = "12345", bool hasWiper = false)
  46.             {
  47.                 this.name = name;
  48.                 hikCam = hikCam_new(connectTime, reconnectTime, ipAddress, port, userName, password, hasWiper);
  49.                 getRes(hikCam, ref width, ref height);
  50.                 this.size = (int)height * width * 5;
  51.                 this.linesize = width * 4;
  52.                 this.buffer = new byte[size];
  53.             }
  54.             public string name;
  55.             public IntPtr hikCam;
  56.             public async Task grabImage()
  57.             {
  58.                 IntPtr data = display_feed(this.hikCam);
  59.                 IntPtr data_ptr = data + this.linesize * (this.height - 1);
  60.                 Mat image = new Mat(this.height, this.width, DepthType.Cv8U, 4, data_ptr + 54, -this.linesize);
  61.                 CvInvoke.Resize(image, image, new Size(0, 0), 0.3, 0.3);
  62.                 CvInvoke.Imshow(this.name, image);
  63.                 CvInvoke.WaitKey(1);  //Wait for the key pressing event      
  64.                 image.Dispose();
  65.             }
  66.             public async Task Pump()
  67.             {
  68.                 pump(this.hikCam);
  69.             }
  70.             public async Task Wiper()
  71.             {
  72.                 wiper(this.hikCam);
  73.             }
  74.             public async Task Test()
  75.             {
  76.                 test(this.hikCam);
  77.             }
  78.  
  79.  
  80.             // ***************** TIMER FOR WATERPUMP **********************************
  81.             public Timer timer1;
  82.             public int timerCounter;
  83.             public bool limitReached = false;
  84.  
  85.             public void timerStart()
  86.             {
  87.                 timer1 = new Timer();
  88.                 timer1.Elapsed += new ElapsedEventHandler(timer1_Tick);
  89.                 timer1.Interval = 1000; // Interval is at 1 second
  90.                 timer1.Start();
  91.  
  92.             }
  93.  
  94.             public void timer1_Tick(object sender, EventArgs e)
  95.             {
  96.                
  97.                 if (timerCounter >= 600) // 600 is our max value for how often the pump can run
  98.                 {
  99.                     limitReached = true;
  100.                 }
  101.  
  102.                 if(limitReached && timerCounter == 0)
  103.                 {
  104.                     limitReached = false;
  105.                     timerStart();
  106.                 }
  107.  
  108.                 if(timerCounter == 0)
  109.                 {
  110.                     timer1.Stop();
  111.                 }
  112.  
  113.                 else
  114.                 {
  115.                     timerCounter--;
  116.                     Console.WriteLine("The timer is at: " + timerCounter + " seconds");
  117.                 }
  118.  
  119.             }
  120.  
  121.             // ********************* END OF TIMER ********************************************
  122.  
  123.  
  124.             // Just a dummy function instead of starting the pump again and again.
  125.             public void virtualPump()
  126.             {
  127.                 Console.WriteLine("The pump har been activated!");  
  128.             }
  129.  
  130.  
  131.         }
  132.  
  133.         static void Main(string[] args)
  134.         {
  135.  
  136.  
  137.             List<camera> cameras = new List<camera>();
  138.             cameras.Add(new camera(name: "cam1", ipAddress: "192.168.1.164", password: "Strix126", hasWiper: true));
  139.             //cameras.Add(new camera(name: "cam2", ipAddress: "192.168.1.164", password: "Strix126"));
  140.  
  141.  
  142.  
  143.             foreach (camera cam in cameras)
  144.             {
  145.                 CvInvoke.NamedWindow(cam.name);
  146.             }
  147.  
  148.             while (!Console.KeyAvailable)
  149.             {
  150.                 foreach (camera cam in cameras)
  151.                 {
  152.                     cam.grabImage();
  153.                 };
  154.             }
  155.  
  156.             // Reads the first key input from keyboard
  157.             // This code will only run once, and the the program will terminate.
  158.  
  159.             Console.ReadKey();
  160.             foreach (camera cam in cameras)
  161.             {
  162.  
  163.  
  164.                 if (cam.timerCounter >= 600 || cam.limitReached)
  165.                 {
  166.                     Console.WriteLine("You have to wait " + (cam.timerCounter) + "Seconds to run the pump again");
  167.  
  168.                 }
  169.  
  170.                 if (cam.timerCounter == 0 && !cam.limitReached)
  171.                 {
  172.                     cam.virtualPump(); // Just a dummy function, only writes out a string that says the waterpump has started
  173.  
  174.                     cam.timerStart();
  175.                     cam.timerCounter += 75;
  176.  
  177.                     // ******* Functions integrated in camera ************
  178.                     //cam.Wiper();
  179.                     //cam.Test();
  180.                     //cam.Pump();
  181.                     // ***************************************************
  182.  
  183.                 }
  184.                
  185.  
  186.                 // Helt ærlig så vet jeg ikke hvorfor jeg har inkludert det som står under her da det er helt likt som if-setningen           
  187.                 // ovenfor. Det var sent på dagen. Det kan sikkret bare fjernes.
  188.  
  189.                 if (cam.timerCounter == 0 && !cam.limitReached)
  190.                 {
  191.  
  192.                     cam.virtualPump();  // Just a dummy function, only writes out a string that says the waterpump has started
  193.  
  194.                     cam.timerStart();
  195.                     cam.timerCounter += 75;
  196.                     Console.WriteLine(cam.timerCounter);
  197.  
  198.                     // ******* Functions integrated in camera ************
  199.                     //cam.Wiper();
  200.                     //cam.Test();
  201.                     //cam.Pump();
  202.                     // ***************************************************
  203.  
  204.  
  205.                 }
  206.             }
  207.         }
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement