-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaptureContext.cs
148 lines (128 loc) · 4.6 KB
/
CaptureContext.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
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
using Suconbu.Mobile;
using Suconbu.Toolbox;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Suconbu.Sumacon
{
struct ContinuousCaptureSetting
{
public int IntervalMilliseconds;
public bool SkipDuplicatedImage;
public int LimitCount;
}
class CaptureContext : IDisposable
{
public enum CaptureMode { Single, Continuous }
public Device Device { get; private set; }
public CaptureMode Mode { get; private set; }
public int RemainingCount
{
get
{
return
(this.Mode == CaptureMode.Continuous && this.continousSetting.LimitCount > 0) ?
(this.continousSetting.LimitCount - this.capturedCount) :
int.MaxValue;
}
}
public int CapturedCount { get { return this.capturedCount; } }
CommandContext commandContext;
ContinuousCaptureSetting continousSetting;
DateTime startedAt;
int capturedCount;
string previousImageHash;
string continuousTimeoutId;
Action<Bitmap> onCaptured = delegate { };
Action onFinished = delegate { };
public static CaptureContext StartSingleCapture(Device device, Action<Bitmap> onCaptured, Action onFinished)
{
var instance = new CaptureContext();
instance.Device = device ?? throw new ArgumentNullException(nameof(device));
instance.Mode = CaptureMode.Single;
instance.onCaptured = onCaptured;
instance.onFinished = onFinished;
instance.RunCapture();
return instance;
}
public static CaptureContext StartContinuousCapture(Device device, ContinuousCaptureSetting setting, Action<Bitmap> onCaptured, Action onFinished)
{
var instance = new CaptureContext();
instance.Device = device ?? throw new ArgumentNullException(nameof(device));
instance.Mode = CaptureMode.Continuous;
instance.continousSetting = setting;
instance.onCaptured = onCaptured;
instance.onFinished = onFinished;
instance.RunCapture();
return instance;
}
public void Stop()
{
this.onFinished?.Invoke();
this.Dispose();
}
void RunCapture()
{
this.startedAt = DateTime.Now;
this.commandContext = this.Device.Screen.CaptureAsync(this.ScreenCaptured);
if (this.commandContext == null)
{
this.onFinished();
}
}
void ScreenCaptured(Bitmap bitmap)
{
if (bitmap == null || this.disposed)
{
this.onFinished?.Invoke();
return;
}
this.commandContext = null;
var skip = false;
if (this.Mode == CaptureMode.Continuous && this.continousSetting.SkipDuplicatedImage)
{
var hash = bitmap.ComputeMD5();
if (this.previousImageHash == hash)
{
skip = true;
}
this.previousImageHash = hash;
}
if (!skip)
{
this.capturedCount++;
}
if (this.Mode == CaptureMode.Continuous && this.RemainingCount > 0)
{
// 連続撮影中なら撮影に掛かった時間を勘案して次を予約しておく
var elapsed = (int)(DateTime.Now - this.startedAt).TotalMilliseconds;
var nextInterval = Math.Max(1, this.continousSetting.IntervalMilliseconds - elapsed);
Debug.Print($"ScreenCaptured - elapsed: {elapsed} ms, nextInterval: {nextInterval} ms");
this.continuousTimeoutId = Delay.SetTimeout(() => this.RunCapture(), nextInterval);
}
if (!skip)
{
this.onCaptured?.Invoke(bitmap);
}
if (this.Mode == CaptureMode.Single ||
(this.Mode == CaptureMode.Continuous && this.RemainingCount == 0))
{
this.onFinished?.Invoke();
}
}
#region IDisposable Support
bool disposed = false;
public virtual void Dispose()
{
if (this.disposed) return;
this.commandContext?.Cancel();
this.commandContext = null;
this.disposed = true;
}
#endregion
}
}