o
asmrobot
2019-10-27 c4bd9d8c587bd1401f0fb2f60c34a4964d7afe20
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace DetectorUtils.Captures
{
    public class CaptureAuto
    {
 
 
        public CaptureAuto(double frameRate, Action<Bitmap> captureCallback)
            :this(frameRate,captureCallback,-1,-1,-1,-1)
        {
        }
 
        public CaptureAuto(double frameRate, Action<Bitmap> captureCallback, Int32 startX, Int32 startY, Int32 width, Int32 height)
        {
            this.frameRate = frameRate;
            this.captureCallback = captureCallback;
            this.Status = CaptureStatus.Stop;
            this.AreaStartX = startX;
            this.AreaStartY = startY;
            this.AreaWidth = width;
            this.AreaHeight = height;
 
            Int32 screenWidth;
            Int32 screenHeight;
            Utils.GetWindowSize(out screenWidth, out screenHeight);
            if (startX >= 0 && startY >= 0 && width > 0 && height > 0 && (startX + width) <= screenWidth && (startY + height) <= screenHeight)
            {
                captureArea = true;
            }
            else
            {
                captureArea = false;
            }
        }
 
        private CancellationTokenSource CancellationSource;//取消源
        private double frameRate = 30d;//30帧
        private Action<Bitmap> captureCallback;//回调
        private DateTime time = DateTime.Now;
        private bool captureArea = false;//是否只截取一部分画面
        public Int32 AreaStartX=0;
        public Int32 AreaStartY=0;
        public Int32 AreaWidth=0;
        public Int32 AreaHeight=0;
        /// <summary>
        /// 状态
        /// </summary>
        public CaptureStatus Status
        {
            get;
            private set;
        }
 
        public void Start()
        {
            if (this.Status == CaptureStatus.Stop)
            {
                CancellationSource = new CancellationTokenSource();
                ThreadPool.QueueUserWorkItem(AutoCapture, CancellationSource.Token);
                Status = CaptureStatus.Captureing;
            }
        }
 
        public void Stop()
        {
            if (CancellationSource != null&&Status==CaptureStatus.Captureing)
            {
                CancellationSource.Cancel();
                CancellationSource = null;
                Status = CaptureStatus.Stop;
            }
        }
 
        /// <summary>
        /// 改变大小
        /// </summary>
        public void Resize(Int32 width,Int32 height)
        {
            Int32 screenWidth = 0;
            Int32 screenHeight = 0;
            Utils.GetWindowSize(out screenWidth, out screenHeight);
            if (width <= 0 || (this.AreaStartX + width) > screenWidth)
            {
                this.AreaWidth = screenWidth - this.AreaStartX;
            }
            else
            {
                this.AreaWidth = width;
            }
 
            if (height <= 0 || (this.AreaStartY + height) > screenHeight)
            {
                this.AreaHeight = screenHeight - this.AreaStartY;
            }
            else
            {
                this.AreaHeight = height;
            }
 
        }
 
        /// <summary>
        /// 移动到指定位置
        /// </summary>
        public void MoveTo(Int32 x,Int32 y)
        {
            Int32 screenWidth = 0;
            Int32 screenHeight = 0;
            Utils.GetWindowSize(out screenWidth, out screenHeight);
 
            if (x < 0 || x > screenWidth)
            {
                this.AreaStartX = 0;
            }
            else
            {
                this.AreaStartX = x;
            }
            if (y < 0 || y > screenHeight)
            {
                this.AreaStartY = 0;
            }
            else
            {
                this.AreaStartY = y;
            }
 
            if ((this.AreaStartX + this.AreaWidth) > screenWidth)
            {
                this.AreaStartX = screenWidth - this.AreaWidth;
            }
 
            if ( (this.AreaStartY + this.AreaHeight) > screenHeight)
            {
                this.AreaStartY = screenHeight - this.AreaHeight;
            }
 
        }
 
 
 
 
 
        /// <summary>
        /// 自动截屏
        /// </summary>
        /// <param name="obj"></param>
        private void AutoCapture(object obj)
        {
            double d = 1000d / frameRate;
            CancellationToken token = (CancellationToken)obj;
            
            while (!token.IsCancellationRequested)
            {
                double ts = (DateTime.Now - time).TotalMilliseconds;
                if (ts < d)
                {
                    Thread.Sleep((Int32)(d - ts));
                    continue;
                }
                Bitmap bitmap = null;
                if (captureArea)
                {
                    bitmap = ScreenCapture.CaptureScreen(this.AreaStartX, this.AreaStartY, this.AreaWidth, this.AreaHeight);
                }
                else
                {
                    bitmap = ScreenCapture.CaptureScreen();
                }
                if (this.captureCallback != null)
                {
                    this.captureCallback(bitmap);
                }
                time = DateTime.Now;
            }
 
        }
 
        public enum CaptureStatus : byte
        {
            Stop,//停止状态
            Captureing//截屏状态
        }
    }
}