-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFCamera.cs
103 lines (92 loc) · 3.14 KB
/
BFCamera.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SpinnakerNET;
using SpinnakerNET.GenApi;
using Emgu;
using Emgu.CV;
using Emgu.CV.Structure;
using System.Drawing;
using Emgu.CV.OCR;
namespace Kitchen
{
public class BFCamera
{
public ManagedSystem system;
public ManagedCameraList camList;
public IManagedCamera managedCam;
public INodeMap nodeMapTLDevice;
public INodeMap nodeMapTLStream;
public INodeMap nodeMap;
public string filePath;
public Form2 form;
public Size frameSize;
public Mat cvMat;
ManagedImage convertedImage = new ManagedImage();
public BFCamera(Form2 _form)
{
form = _form;
filePath = "C:\\Users\\JohnsonRobertEvan\\CSharpProjects\\Kitchen\\data";
}
public void OpenCamera()
{
// Get system, camera list, and camera (single camera only)
system = new ManagedSystem();
camList = system.GetCameras();
managedCam = camList[0];
// Get nodemaps and initialize camera
nodeMapTLDevice = managedCam.GetTLDeviceNodeMap();
nodeMapTLStream = managedCam.GetTLStreamNodeMap();
managedCam.Init();
nodeMap = managedCam.GetNodeMap();
// Adjust camera settings and save to file
CameraSettings camSettings = new CameraSettings(managedCam, nodeMapTLDevice, nodeMapTLStream, nodeMap);
camSettings.saveSettings();
frameSize = camSettings.GetFrameSize();
managedCam.BeginAcquisition();
}
public unsafe (int, Mat) GetOneImage()
{
int frameID;
try
{
using(IManagedImage rawImage = managedCam.GetNextImage())
{
rawImage.Convert(convertedImage, PixelFormatEnums.RGB8);
IntPtr point = convertedImage.DataPtr;
int width = (int)convertedImage.Width;
int height = (int)convertedImage.Height;
frameID = (int)rawImage.ChunkData.FrameID;
Size frameSize = new Size(width, height);
cvMat = new Mat(frameSize, default, 3, point, width);
rawImage.Release();
}
return (frameID, cvMat);
}
catch (SpinnakerException ex)
{
Console.WriteLine("Error: " + ex.Message);
return (0, new Mat());
}
}
public void CloseCamera(bool _factoryReset = false)
{
managedCam.EndAcquisition();
if (_factoryReset)
{
ICommand iFactoryReset = nodeMap.GetNode<ICommand>("FactoryReset");
iFactoryReset.Execute();
}
else
{
managedCam.DeInit();
}
// Clear camera list and release system.
camList.Clear();
system.Dispose();
Console.WriteLine("\nSystem released.");
}
}
}