Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Drawing;
- using System.Drawing.Imaging;
- using Emgu.CV;
- using Emgu.CV.Structure;
- using Emgu.CV.CvEnum;
- using System.Timers;
- namespace HikvisionGrabber
- {
- class Program
- {
- #region importCpp
- [DllImport("hik_grabber_cpp.dll")]
- public static extern IntPtr display_feed(IntPtr instance);
- [DllImport("hik_grabber_cpp.dll")]
- public static extern void wiper(IntPtr instance);
- [DllImport("hik_grabber_cpp.dll")]
- public static extern void pump(IntPtr instance);
- [DllImport("hik_grabber_cpp.dll")]
- public static extern void stop(IntPtr instance);
- [DllImport("hik_grabber_cpp.dll")]
- public static extern IntPtr hikCam_new(int connectTime, int reconnectTime, string ipAddress, int port, string userName, string password, bool hasWiper);
- [DllImport("hik_grabber_cpp.dll")]
- public static extern void test(IntPtr instance);
- [DllImport("hik_grabber_cpp.dll")]
- public static extern void getRes(IntPtr instance, ref int width, ref int height);
- #endregion
- class camera
- {
- public int width;
- public int height;
- public int size;
- public int linesize;
- public byte[] buffer;
- 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)
- {
- this.name = name;
- hikCam = hikCam_new(connectTime, reconnectTime, ipAddress, port, userName, password, hasWiper);
- getRes(hikCam, ref width, ref height);
- this.size = (int)height * width * 5;
- this.linesize = width * 4;
- this.buffer = new byte[size];
- }
- public string name;
- public IntPtr hikCam;
- public async Task grabImage()
- {
- IntPtr data = display_feed(this.hikCam);
- IntPtr data_ptr = data + this.linesize * (this.height - 1);
- Mat image = new Mat(this.height, this.width, DepthType.Cv8U, 4, data_ptr + 54, -this.linesize);
- CvInvoke.Resize(image, image, new Size(0, 0), 0.3, 0.3);
- CvInvoke.Imshow(this.name, image);
- CvInvoke.WaitKey(1); //Wait for the key pressing event
- image.Dispose();
- }
- public async Task Pump()
- {
- pump(this.hikCam);
- }
- public async Task Wiper()
- {
- wiper(this.hikCam);
- }
- public async Task Test()
- {
- test(this.hikCam);
- }
- // ***************** TIMER FOR WATERPUMP **********************************
- public Timer timer1;
- public int timerCounter;
- public bool limitReached = false;
- public void timerStart()
- {
- timer1 = new Timer();
- timer1.Elapsed += new ElapsedEventHandler(timer1_Tick);
- timer1.Interval = 1000; // Interval is at 1 second
- timer1.Start();
- }
- public void timer1_Tick(object sender, EventArgs e)
- {
- if (timerCounter >= 600) // 600 is our max value for how often the pump can run
- {
- limitReached = true;
- }
- if(limitReached && timerCounter == 0)
- {
- limitReached = false;
- timerStart();
- }
- if(timerCounter == 0)
- {
- timer1.Stop();
- }
- else
- {
- timerCounter--;
- Console.WriteLine("The timer is at: " + timerCounter + " seconds");
- }
- }
- // ********************* END OF TIMER ********************************************
- // Just a dummy function instead of starting the pump again and again.
- public void virtualPump()
- {
- Console.WriteLine("The pump har been activated!");
- }
- }
- static void Main(string[] args)
- {
- List<camera> cameras = new List<camera>();
- cameras.Add(new camera(name: "cam1", ipAddress: "192.168.1.164", password: "Strix126", hasWiper: true));
- //cameras.Add(new camera(name: "cam2", ipAddress: "192.168.1.164", password: "Strix126"));
- foreach (camera cam in cameras)
- {
- CvInvoke.NamedWindow(cam.name);
- }
- while (!Console.KeyAvailable)
- {
- foreach (camera cam in cameras)
- {
- cam.grabImage();
- };
- }
- // Reads the first key input from keyboard
- // This code will only run once, and the the program will terminate.
- Console.ReadKey();
- foreach (camera cam in cameras)
- {
- if (cam.timerCounter >= 600 || cam.limitReached)
- {
- Console.WriteLine("You have to wait " + (cam.timerCounter) + "Seconds to run the pump again");
- }
- if (cam.timerCounter == 0 && !cam.limitReached)
- {
- cam.virtualPump(); // Just a dummy function, only writes out a string that says the waterpump has started
- cam.timerStart();
- cam.timerCounter += 75;
- // ******* Functions integrated in camera ************
- //cam.Wiper();
- //cam.Test();
- //cam.Pump();
- // ***************************************************
- }
- // Helt ærlig så vet jeg ikke hvorfor jeg har inkludert det som står under her da det er helt likt som if-setningen
- // ovenfor. Det var sent på dagen. Det kan sikkret bare fjernes.
- if (cam.timerCounter == 0 && !cam.limitReached)
- {
- cam.virtualPump(); // Just a dummy function, only writes out a string that says the waterpump has started
- cam.timerStart();
- cam.timerCounter += 75;
- Console.WriteLine(cam.timerCounter);
- // ******* Functions integrated in camera ************
- //cam.Wiper();
- //cam.Test();
- //cam.Pump();
- // ***************************************************
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement