forked from Iemand005/IRCameraView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIRController.cs
More file actions
168 lines (132 loc) · 6.3 KB
/
IRController.cs
File metadata and controls
168 lines (132 loc) · 6.3 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Windows.Graphics.Imaging;
using Windows.Media.Capture.Frames;
using Windows.Media.Capture;
using Windows.Media.Playback;
using Windows.Media.MediaProperties;
using Windows.Media;
using System.Diagnostics;
namespace IRCameraView
{
public enum IRFrameFilter
{
None,
Raw,
Illuminated
}
public class IRController
{
public MediaFrameReader MediaFrameReader { get; private set; }
public MediaPlayer MediaPlayer { get; private set; }
public MediaCapture MediaCapture { get; private set; }
public List<MediaFrameSourceGroup> Devices { get; private set; }
public MediaFrameSourceGroup Device { get; private set; }
public IRFrameFilter FrameFilter { get; set; }
public delegate void FrameReady(SoftwareBitmap bitmap);
public event FrameReady OnFrameReady;
private SoftwareBitmap _backBuffer;
public IRController()
{
FrameFilter = IRFrameFilter.None;
MediaCapture = new MediaCapture();
LoadCameras(MediaFrameSourceKind.Infrared);
if (Devices.Count == 0)
throw new Exception("No infrared cameras were found.");
}
private List<MediaFrameSourceGroup> LoadCameras(MediaFrameSourceKind allowedKind)
{
return LoadCameras([allowedKind]);
}
private List<MediaFrameSourceGroup> LoadCameras(List<MediaFrameSourceKind> allowedKinds)
{
Devices = new List<MediaFrameSourceGroup>();
var devices = MediaFrameSourceGroup.FindAllAsync().AsTask().Result;
// Filter out the IR camera's
foreach (var device in devices)
foreach (var sourceInfo in device.SourceInfos)
if (allowedKinds.Contains(sourceInfo.SourceKind))
Devices.Add(device);
return Devices;
}
public void SelectDevice(MediaFrameSourceGroup sourceGroup)
{
// Clean up previous MediaFrameReader
if (MediaFrameReader != null)
{
MediaFrameReader.FrameArrived -= FrameArrived;
MediaFrameReader.StopAsync().AsTask().Wait();
MediaFrameReader.Dispose();
MediaFrameReader = null;
}
// Clean up previous MediaCapture
if (MediaCapture != null)
{
MediaCapture.Dispose();
MediaCapture = null;
}
Device = sourceGroup;
MediaCapture = new MediaCapture();
MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings
{
SourceGroup = Device,
SharingMode = MediaCaptureSharingMode.SharedReadOnly,
StreamingCaptureMode = StreamingCaptureMode.Video,
MemoryPreference = MediaCaptureMemoryPreference.Cpu
};
MediaCapture.InitializeAsync(settings).AsTask().Wait();
// MediaEncodingProfile mediaEncodingProfile = new MediaEncodingProfile();
//MediaEncodingProfile encodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Qvga);
// Windows.UI.Xaml.Controls.CaptureElement
//var mfExtension = await mediaSink.InitializeAsync(encodingProfile.Audio, encodingProfile.Video);
//MediaCapture.StartPreviewToCustomSinkAsync(mediaEncodingProfile, mediaExtension);
//MediaCapture.StartPreviewAsync().AsTask().Wait();
//var infraredTorchControl = MediaCapture.VideoDeviceController.InfraredTorchControl;
//if (infraredTorchControl.IsSupported)
//{
// //infraredTorchControl.SupportedModes
//}
var frameSources = MediaCapture.FrameSources;
var frameSource = frameSources.First().Value;
var preferredFormat = frameSource.SupportedFormats.First();
frameSource.SetFormatAsync(preferredFormat).AsTask().Wait();
MediaFrameReader = MediaCapture.CreateFrameReaderAsync(frameSource).AsTask().Result;
MediaFrameReader.AcquisitionMode = MediaFrameReaderAcquisitionMode.Realtime;
MediaFrameReader.FrameArrived += FrameArrived;
MediaFrameReader.StartAsync().AsTask().Wait();
}
public void SelectDeviceByIndex(int index)
{
if (index < 0 || index >= Devices.Count)
throw new ArgumentOutOfRangeException(nameof(index), "Invalid device index.");
SelectDevice(Devices[index]);
}
public List<string> GetDeviceNames()
{
return Devices.Select(d => d.DisplayName).ToList();
}
private void FrameArrived(MediaFrameReader sender, MediaFrameArrivedEventArgs args)
{
using (var frameReference = sender.TryAcquireLatestFrame())
{
var videoMediaFrame = frameReference?.VideoMediaFrame;
var softwareBitmap = videoMediaFrame?.SoftwareBitmap;
if (softwareBitmap == null) return;
if (softwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 || softwareBitmap.BitmapAlphaMode != BitmapAlphaMode.Premultiplied)
softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
softwareBitmap = Interlocked.Exchange(ref _backBuffer, softwareBitmap);
softwareBitmap?.Dispose();
SoftwareBitmap latestBitmap;
while ((latestBitmap = Interlocked.Exchange(ref _backBuffer, null)) != null)
{
var isIlluminated = videoMediaFrame.InfraredMediaFrame.IsIlluminated; // This filter gives a similar result to having the torch enabled or disabled even if we can't control the torch. (It halves framerate tho)
if (FrameFilter == IRFrameFilter.None || (!isIlluminated && FrameFilter == IRFrameFilter.Raw) || (isIlluminated && FrameFilter == IRFrameFilter.Illuminated))
OnFrameReady(latestBitmap);
//latestBitmap.Dispose(); Needs to be done by the event handler.
}
}
}
}
}