├── .gitattributes
├── .gitignore
├── README.md
├── VisionCamera.sln
└── Yoga.Camera
├── BaslerCamera.cs
├── CameraBase.cs
├── CameraManger.cs
├── CameraOperator.cs
├── CameraPram.cs
├── DHCamera.cs
├── DirectShowCamera.cs
├── EnumInfo.cs
├── Fps.cs
├── GigeCamera.cs
├── HikvisionCamera.cs
├── ICImagingCamera.cs
├── IOSerial.cs
├── ImageEventArgs.cs
├── MVGigE.cs
├── MVSDK.cs
├── MicrovisionCamera.cs
├── MindCamera.cs
├── Properties
└── AssemblyInfo.cs
├── Yoga.Camera.csproj
├── frmCameraSetting.Designer.cs
├── frmCameraSetting.cs
└── frmCameraSetting.resx
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.userosscache
8 | *.sln.docstates
9 |
10 | # User-specific files (MonoDevelop/Xamarin Studio)
11 | *.userprefs
12 |
13 | # Build results
14 | [Dd]ebug/
15 | [Dd]ebugPublic/
16 | [Rr]elease/
17 | [Rr]eleases/
18 | [Xx]64/
19 | [Xx]86/
20 | [Bb]uild/
21 | bld/
22 | [Bb]in/
23 | [Oo]bj/
24 |
25 | # Visual Studio 2015 cache/options directory
26 | .vs/
27 | # Uncomment if you have tasks that create the project's static files in wwwroot
28 | #wwwroot/
29 |
30 | # MSTest test Results
31 | [Tt]est[Rr]esult*/
32 | [Bb]uild[Ll]og.*
33 |
34 | # NUNIT
35 | *.VisualState.xml
36 | TestResult.xml
37 |
38 | # Build Results of an ATL Project
39 | [Dd]ebugPS/
40 | [Rr]eleasePS/
41 | dlldata.c
42 |
43 | # DNX
44 | project.lock.json
45 | artifacts/
46 |
47 | *_i.c
48 | *_p.c
49 | *_i.h
50 | *.ilk
51 | *.meta
52 | *.obj
53 | *.pch
54 | *.pdb
55 | *.pgc
56 | *.pgd
57 | *.rsp
58 | *.sbr
59 | *.tlb
60 | *.tli
61 | *.tlh
62 | *.tmp
63 | *.tmp_proj
64 | *.log
65 | *.vspscc
66 | *.vssscc
67 | .builds
68 | *.pidb
69 | *.svclog
70 | *.scc
71 |
72 | # Chutzpah Test files
73 | _Chutzpah*
74 |
75 | # Visual C++ cache files
76 | ipch/
77 | *.aps
78 | *.ncb
79 | *.opendb
80 | *.opensdf
81 | *.sdf
82 | *.cachefile
83 | *.VC.db
84 |
85 | # Visual Studio profiler
86 | *.psess
87 | *.vsp
88 | *.vspx
89 | *.sap
90 |
91 | # TFS 2012 Local Workspace
92 | $tf/
93 |
94 | # Guidance Automation Toolkit
95 | *.gpState
96 |
97 | # ReSharper is a .NET coding add-in
98 | _ReSharper*/
99 | *.[Rr]e[Ss]harper
100 | *.DotSettings.user
101 |
102 | # JustCode is a .NET coding add-in
103 | .JustCode
104 |
105 | # TeamCity is a build add-in
106 | _TeamCity*
107 |
108 | # DotCover is a Code Coverage Tool
109 | *.dotCover
110 |
111 | # NCrunch
112 | _NCrunch_*
113 | .*crunch*.local.xml
114 | nCrunchTemp_*
115 |
116 | # MightyMoose
117 | *.mm.*
118 | AutoTest.Net/
119 |
120 | # Web workbench (sass)
121 | .sass-cache/
122 |
123 | # Installshield output folder
124 | [Ee]xpress/
125 |
126 | # DocProject is a documentation generator add-in
127 | DocProject/buildhelp/
128 | DocProject/Help/*.HxT
129 | DocProject/Help/*.HxC
130 | DocProject/Help/*.hhc
131 | DocProject/Help/*.hhk
132 | DocProject/Help/*.hhp
133 | DocProject/Help/Html2
134 | DocProject/Help/html
135 |
136 | # Click-Once directory
137 | publish/
138 |
139 | # Publish Web Output
140 | *.[Pp]ublish.xml
141 | *.azurePubxml
142 |
143 | # TODO: Un-comment the next line if you do not want to checkin
144 | # your web deploy settings because they may include unencrypted
145 | # passwords
146 | #*.pubxml
147 | *.publishproj
148 |
149 | # NuGet Packages
150 | *.nupkg
151 | # The packages folder can be ignored because of Package Restore
152 | **/packages/*
153 | # except build/, which is used as an MSBuild target.
154 | !**/packages/build/
155 | # Uncomment if necessary however generally it will be regenerated when needed
156 | #!**/packages/repositories.config
157 | # NuGet v3's project.json files produces more ignoreable files
158 | *.nuget.props
159 | *.nuget.targets
160 |
161 | # Microsoft Azure Build Output
162 | csx/
163 | *.build.csdef
164 |
165 | # Microsoft Azure Emulator
166 | ecf/
167 | rcf/
168 |
169 | # Windows Store app package directory
170 | AppPackages/
171 | BundleArtifacts/
172 |
173 | # Visual Studio cache files
174 | # files ending in .cache can be ignored
175 | *.[Cc]ache
176 | # but keep track of directories ending in .cache
177 | !*.[Cc]ache/
178 |
179 | # Others
180 | ClientBin/
181 | [Ss]tyle[Cc]op.*
182 | ~$*
183 | *~
184 | *.dbmdl
185 | *.dbproj.schemaview
186 | *.pfx
187 | *.publishsettings
188 | node_modules/
189 | orleans.codegen.cs
190 |
191 | # RIA/Silverlight projects
192 | Generated_Code/
193 |
194 | # Backup & report files from converting an old project file
195 | # to a newer Visual Studio version. Backup files are not needed,
196 | # because we have git ;-)
197 | _UpgradeReport_Files/
198 | Backup*/
199 | UpgradeLog*.XML
200 | UpgradeLog*.htm
201 |
202 | # SQL Server files
203 | *.mdf
204 | *.ldf
205 |
206 | # Business Intelligence projects
207 | *.rdl.data
208 | *.bim.layout
209 | *.bim_*.settings
210 |
211 | # Microsoft Fakes
212 | FakesAssemblies/
213 |
214 | # GhostDoc plugin setting file
215 | *.GhostDoc.xml
216 |
217 | # Node.js Tools for Visual Studio
218 | .ntvs_analysis.dat
219 |
220 | # Visual Studio 6 build log
221 | *.plg
222 |
223 | # Visual Studio 6 workspace options file
224 | *.opt
225 |
226 | # Visual Studio LightSwitch build output
227 | **/*.HTMLClient/GeneratedArtifacts
228 | **/*.DesktopClient/GeneratedArtifacts
229 | **/*.DesktopClient/ModelManifest.xml
230 | **/*.Server/GeneratedArtifacts
231 | **/*.Server/ModelManifest.xml
232 | _Pvt_Extensions
233 |
234 | # LightSwitch generated files
235 | GeneratedArtifacts/
236 | ModelManifest.xml
237 |
238 | # Paket dependency manager
239 | .paket/paket.exe
240 |
241 | # FAKE - F# Make
242 | .fake/
243 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # VisionCamera
2 | 多相机兼容驱动
3 | 巴斯勒=1 海康=2 迈德威视=4 维视图像=8(未完成) 映美精=16 usb=32
4 | 相机可以混合使用,使用相机的用户自定义名称来识别相机,定义为1/2/3等
5 | 当前问题点:
6 | 1.直接gige相机由于各个品牌的属性名称不一致取消使用,直接用sdk操作
7 | 2.相机打开后一直处于连接状态,一般demo的操作会是开始采集后才连接相机,为了减小连接时候的时间延迟,直接在打开相机时候就开启数据流通道
8 |
--------------------------------------------------------------------------------
/VisionCamera.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 14
4 | VisualStudioVersion = 14.0.25420.1
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CShap", "CShap", "{BC5D0D55-8C4C-4671-A0E4-3D87A8961A7E}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yoga.Camera", "Yoga.Camera\Yoga.Camera.csproj", "{306083DA-D250-4B6D-A487-F684F4330C0D}"
9 | EndProject
10 | Global
11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
12 | Debug|x64 = Debug|x64
13 | Debug|x86 = Debug|x86
14 | Release|x64 = Release|x64
15 | Release|x86 = Release|x86
16 | EndGlobalSection
17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
18 | {306083DA-D250-4B6D-A487-F684F4330C0D}.Debug|x64.ActiveCfg = Debug|x64
19 | {306083DA-D250-4B6D-A487-F684F4330C0D}.Debug|x64.Build.0 = Debug|x64
20 | {306083DA-D250-4B6D-A487-F684F4330C0D}.Debug|x86.ActiveCfg = Debug|x86
21 | {306083DA-D250-4B6D-A487-F684F4330C0D}.Debug|x86.Build.0 = Debug|x86
22 | {306083DA-D250-4B6D-A487-F684F4330C0D}.Release|x64.ActiveCfg = Release|x64
23 | {306083DA-D250-4B6D-A487-F684F4330C0D}.Release|x64.Build.0 = Release|x64
24 | {306083DA-D250-4B6D-A487-F684F4330C0D}.Release|x86.ActiveCfg = Release|x86
25 | {306083DA-D250-4B6D-A487-F684F4330C0D}.Release|x86.Build.0 = Release|x86
26 | EndGlobalSection
27 | GlobalSection(SolutionProperties) = preSolution
28 | HideSolutionNode = FALSE
29 | EndGlobalSection
30 | GlobalSection(NestedProjects) = preSolution
31 | {306083DA-D250-4B6D-A487-F684F4330C0D} = {BC5D0D55-8C4C-4671-A0E4-3D87A8961A7E}
32 | EndGlobalSection
33 | EndGlobal
34 |
--------------------------------------------------------------------------------
/Yoga.Camera/CameraBase.cs:
--------------------------------------------------------------------------------
1 | using HalconDotNet;
2 | using System;
3 | using System.Threading;
4 | using Yoga.Common;
5 |
6 | namespace Yoga.Camera
7 | {
8 |
9 |
10 |
11 | public abstract class CameraBase
12 | {
13 | protected HImage hPylonImage;
14 |
15 | #region 相机参数
16 |
17 | protected int cameraIndex;
18 |
19 | protected HTuple startTime;
20 | //相机参数
21 | ///
22 | /// 曝光初始值,单位us
23 | ///
24 | public virtual double ShotInitValue
25 | {
26 | get
27 | {
28 | return 3900;
29 |
30 | }
31 | }
32 | ///
33 | /// 增益初始值
34 | ///
35 | public virtual double GainInitValue
36 | {
37 | get
38 | {
39 | return gainMin;
40 | }
41 | }
42 | protected long shuterCur; //当前曝光值
43 | protected long shuterMin; //最小值
44 | protected long shuterMax; //最大值
45 | protected string shuterUnit; //单位
46 |
47 | protected double gainCur; //当前增益值
48 | protected double gainMin; //最小值
49 | protected double gainMax; //最大值
50 | protected string gainUnit; //单位
51 |
52 | protected double triggerDelayAbsMax;
53 | protected double triggerDelayAbsMin;
54 | ///
55 | /// 外触发延时
56 | ///
57 | protected double triggerDelayAbs;
58 |
59 | protected double lineDebouncerTimeAbsMax;
60 | protected double lineDebouncerTimeAbsMin;
61 | ///
62 | /// 外触发防抖动
63 | ///
64 | protected double lineDebouncerTimeAbs;
65 |
66 | protected double outLineTime;
67 | ///
68 | /// 当前触发模式 连续触发=0 ;软触发=1
69 | ///
70 | protected int triggerMode;
71 | protected Fps fps = new Fps();
72 | public int CameraIndex
73 | {
74 | get
75 | {
76 | return cameraIndex;
77 | }
78 | }
79 | ///
80 | /// 当前曝光值单位us
81 | ///
82 | public abstract long ShuterCur { get; set; } //当前曝光值
83 | public long ShuterMin
84 | {
85 | get
86 | {
87 | return shuterMin;
88 | }
89 | } //最小值
90 | public long ShuterMax
91 | {
92 | get
93 | {
94 | return shuterMax;
95 | }
96 | } //最大值
97 | public string ShuterUnit
98 | {
99 | get
100 | {
101 | return shuterUnit;
102 | }
103 | }
104 | //单位
105 |
106 | public abstract double GainCur { get; set; } //当前增益值
107 | public double GainMin
108 | {
109 | get
110 | {
111 | return gainMin;
112 | }
113 |
114 | } //最小值
115 | public double GainMax
116 | {
117 | get
118 | {
119 | return gainMax;
120 | }
121 | } //最大值
122 | public string GainUnit
123 | {
124 | get
125 | {
126 | return gainUnit;
127 | }
128 | } //单位
129 | #endregion
130 | private ImageAngle imageAngle = ImageAngle.角度0;
131 |
132 | ///
133 | /// 异常信息
134 | ///
135 | public string ErrMessage { get; protected set; }
136 | ///
137 | /// 是否建立采集连接
138 | ///
139 | public bool IsLink { get; protected set; }
140 | ///
141 | /// 是否在连续采集中
142 | ///
143 | public bool IsContinuousShot { get; protected set; }
144 | protected ManualResetEvent imageResetEvent = new ManualResetEvent(false);
145 | public bool IsExtTrigger { get; protected set; }
146 | private readonly object commandLock = new object();
147 |
148 | private Command command = Command.ExtTrigger;
149 | ///
150 | /// 当前采集图像附带命令信息-包含线程安全锁定
151 | ///
152 | public Command Command
153 | {
154 | protected get
155 | {
156 | lock (commandLock)
157 | {
158 | return command;
159 | }
160 | }
161 | set
162 | {
163 | lock (commandLock)
164 | {
165 | command = value;
166 | }
167 | }
168 | }
169 | public virtual double TriggerDelayAbsInit
170 | {
171 | get
172 | {
173 | return 10;
174 | }
175 | }
176 | ///
177 | /// 触发延时
178 | ///
179 | public virtual double TriggerDelayAbs
180 | {
181 | get
182 | {
183 | return triggerDelayAbs;
184 | }
185 |
186 | set
187 | {
188 | try
189 | {
190 | double data = value;
191 |
192 | //判断输入值是否在范围内
193 | //若大于最大值则将曝光值设为最大值
194 |
195 | if (data > TriggerDelayAbsMax)
196 | {
197 | data = TriggerDelayAbsMax;
198 | }
199 | //若小于最小值将曝光值设为最小值
200 | else if (data < triggerDelayAbsMin)
201 | {
202 | data = triggerDelayAbsMin;
203 | }
204 |
205 | triggerDelayAbs = data;
206 | if (IsExtTrigger)
207 | {
208 | SetExtTrigger();
209 | }
210 |
211 | }
212 | catch (Exception ex)
213 | {
214 | Util.WriteLog(this.GetType(), ex);
215 | Util.Notify("相机触发延时设置异常");
216 | }
217 | }
218 | }
219 | ///
220 | /// 触发防抖默认值(us)
221 | ///
222 | public virtual double LineDebouncerTimeAbsInit
223 | {
224 | get
225 | {
226 | return 10;
227 | }
228 | }
229 | ///
230 | /// 触发防抖(us)
231 | ///
232 | public virtual double LineDebouncerTimeAbs
233 | {
234 | get
235 | {
236 | return lineDebouncerTimeAbs;
237 | }
238 |
239 | set
240 | {
241 | try
242 | {
243 | double data = value;
244 |
245 | //判断输入值是否在范围内
246 | //若大于最大值则将触发防抖设为最大值
247 |
248 | if (data > lineDebouncerTimeAbsMax)
249 | {
250 | data = lineDebouncerTimeAbsMax;
251 | }
252 | //若小于最小值将触发防抖设为最小值
253 | else if (data < lineDebouncerTimeAbsMin)
254 | {
255 | data = lineDebouncerTimeAbsMin;
256 | }
257 |
258 | lineDebouncerTimeAbs = data;
259 | if (IsExtTrigger)
260 | {
261 | SetExtTrigger();
262 | }
263 | }
264 | catch (Exception ex)
265 | {
266 | Util.WriteLog(this.GetType(), ex);
267 | Util.Notify("相机触发防抖设置异常");
268 | }
269 | }
270 | }
271 |
272 | public double TriggerDelayAbsMax
273 | {
274 | get
275 | {
276 | return triggerDelayAbsMax;
277 | }
278 | }
279 |
280 | public double TriggerDelayAbsMin
281 | {
282 | get
283 | {
284 | return triggerDelayAbsMin;
285 | }
286 | }
287 |
288 | public double LineDebouncerTimeAbsMax
289 | {
290 | get
291 | {
292 | return lineDebouncerTimeAbsMax;
293 | }
294 | }
295 |
296 | public double LineDebouncerTimeAbsMin
297 | {
298 | get
299 | {
300 | return lineDebouncerTimeAbsMin;
301 | }
302 | }
303 | ///
304 | /// 外部信号输出时间
305 | ///
306 | public double OutLineTime
307 | {
308 | get
309 | {
310 | return outLineTime;
311 | }
312 |
313 | set
314 | {
315 | outLineTime = value;
316 | }
317 | }
318 |
319 | public ImageAngle ImageAngle
320 | {
321 | get
322 | {
323 | return imageAngle;
324 | }
325 | set
326 | {
327 | imageAngle = value;
328 | }
329 | }
330 |
331 | public HTuple StartTime
332 | {
333 | get
334 | {
335 | return startTime;
336 | }
337 | }
338 |
339 | public event EventHandler ImageEvent;
340 | public void TrigerImageEvent(object sender, ImageEventArgs e)
341 | {
342 | if (ImageEvent != null)
343 | {
344 | ImageEvent(sender, e);
345 | }
346 | }
347 |
348 | private HTuple timeOld;
349 | protected void TrigerImageEvent()
350 | {
351 | //帧率统计增加
352 | fps.IncreaseFrameNum();
353 | if (hPylonImage != null && hPylonImage.IsInitialized())
354 | {
355 |
356 | HImage imgTmp = RotateImage(hPylonImage);
357 | hPylonImage.Dispose();
358 | hPylonImage = imgTmp;
359 | imageResetEvent.Set();
360 | if (Command != Command.Grab)
361 | {
362 | if (timeOld!=null)
363 | {
364 | double toolTime = (startTime - timeOld) * 1000.0;
365 | if (Command!= Command.Video)
366 | {
367 | Util.Notify(string.Format("相机{0}收到图像,采集间隔{1:f2}ms", CameraIndex, toolTime));
368 | }
369 |
370 | }
371 | timeOld = startTime;
372 | //Util.Notify("收到图像"+ startTime.D);
373 | TrigerImageEvent(this, new ImageEventArgs(Command, hPylonImage, cameraIndex, new HTuple(startTime.D)));
374 | }
375 | //if (Command!= Command.Video)
376 | //{
377 | // Command = Command.None;
378 | //}
379 | }
380 | else
381 | {
382 | Util.Notify(string.Format("相机{0}图像数据为空", cameraIndex));
383 | }
384 | }
385 |
386 | protected abstract void GetCameraSettingData();
387 | int fpsCount = 0;
388 | public virtual string GetCameraAcqInfo()
389 | {
390 | fpsCount++;
391 | if (fpsCount > 10 && IsContinuousShot)
392 | {
393 | fps.UpdateFps();
394 | fpsCount = 0;
395 | }
396 | string info = "";
397 | if (IsContinuousShot)
398 | {
399 | info = string.Format("FPS:{0:F1}|帧:{1}|",
400 | fps.GetFps(), fps.GetTotalFrameCount());
401 | }
402 | else
403 | {
404 | info = string.Format(" 帧:{0} |",
405 | fps.GetTotalFrameCount());
406 | }
407 | return info;
408 | }
409 | ///
410 | /// 打开相机
411 | ///
412 | ///
413 | public abstract bool Open();
414 | ///
415 | /// 关闭设备
416 | ///
417 | public abstract void Close();
418 | ///
419 | /// 连续采集图像
420 | ///
421 | public abstract void ContinuousShot();
422 | ///
423 | /// 停止连续采集
424 | ///
425 | public abstract void ContinuousShotStop();
426 | ///
427 | /// 采集一张图像
428 | ///
429 | public abstract void OneShot(Command command);
430 |
431 |
432 | public virtual void ResetFps()
433 | {
434 | fps.Reset();
435 | }
436 | public virtual HImage GrabImage(int delayMs)
437 | {
438 | imageResetEvent.Reset();
439 | OneShot(Command.Grab);
440 | if (imageResetEvent.WaitOne(delayMs)&& hPylonImage.IsInitialized())
441 | {
442 | return hPylonImage;
443 | }
444 | else
445 | {
446 | throw new Exception("图像软触发采集超时");
447 | }
448 | }
449 | ///
450 | /// 设置为外触发模式
451 | ///
452 | public abstract void SetExtTrigger();
453 |
454 | public abstract void Output();
455 |
456 | protected HImage RotateImage(HImage img)
457 | {
458 | HImage img1=null;
459 | switch (ImageAngle)
460 | {
461 | case ImageAngle.角度0:
462 | img1= img.CopyImage();
463 | break;
464 | case ImageAngle.角度90:
465 | img1 = img.RotateImage((double)90, "constant");
466 | break;
467 | case ImageAngle.角度180:
468 | img1 = img.RotateImage((double)180, "constant");
469 | break;
470 | case ImageAngle.角度270:
471 | img1 = img.RotateImage((double)270, "constant");
472 | break;
473 | default:
474 | break;
475 | }
476 | return img1;
477 | }
478 | }
479 | }
480 |
--------------------------------------------------------------------------------
/Yoga.Camera/CameraManger.cs:
--------------------------------------------------------------------------------
1 | //using Basler.Pylon;
2 | using DeviceSource;
3 | using MvCamCtrl.NET;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Runtime.InteropServices;
8 | using System.Threading;
9 | using Yoga.Common;
10 | using MVSDK;
11 | using CameraHandle = System.Int32;
12 | using MVAPI;
13 |
14 | using MVSTATUS = MVAPI.MVSTATUS_CODES;
15 | using HalconDotNet;
16 | //using GxIAPINET;
17 |
18 | namespace Yoga.Camera
19 | {
20 | [Flags]
21 | public enum CameraFlag
22 | {
23 | Basler = 1,
24 | Hikvision = 2,
25 | Mindvision = 4,
26 | Microvision = 8,
27 | ICImaging = 16,
28 | DirectShow = 32,
29 | DaHeng = 64,
30 | Gige = 128
31 | }
32 | public static class CameraManger
33 | {
34 | static Dictionary cameraDic;
35 |
36 | public static event EventHandler CameraInitFinishEvent;
37 |
38 |
39 | ///
40 | /// 相机类型标记
41 | ///
42 | public static CameraFlag CameraFlag = CameraFlag.Basler;
43 |
44 | public static string DirectShowIndex = "1";
45 | public static string DirectShowColorSpace = "gray";
46 | public static string DirectShowCameraType = "yuv (1600x1200)";
47 | public static Dictionary CameraDic
48 | {
49 | get
50 | {
51 | if (cameraDic == null)
52 | {
53 | cameraDic = new Dictionary();
54 | }
55 | return cameraDic;
56 | }
57 |
58 | set
59 | {
60 | cameraDic = value;
61 | }
62 | }
63 | ///
64 | /// 获取gige相机的属性信息
65 | ///
66 | ///
67 | ///
68 | ///
69 | private static string GetGigeValue(string dat, string valType)
70 | {
71 | int userNameIndex = dat.IndexOf(valType);
72 | if (userNameIndex == -1)
73 | {
74 | return null;
75 | }
76 | int count = valType.Count();
77 | string userNameTr = dat.Substring(userNameIndex + count);
78 | int sub = userNameTr.IndexOf("|");
79 | if (sub == -1)
80 | {
81 | sub = userNameTr.Length;
82 | }
83 | string name = userNameTr.Substring(0, sub);
84 | string ttt = name.Trim();
85 | return ttt;
86 | }
87 | public static bool Open()
88 | {
89 | try
90 | {
91 | int cameraCount = 0;
92 | if ((CameraFlag & CameraFlag.Basler) == CameraFlag.Basler)
93 | {
94 | //#region basler相机遍历查询
95 |
96 | //List g_allCameras = Basler.Pylon.CameraFinder.Enumerate();
97 |
98 | //if (g_allCameras.Count > 0)
99 | //{
100 | // for (int i = 0; i < g_allCameras.Count; i++)
101 | // {
102 | // Basler.Pylon.Camera g_camera = new Basler.Pylon.Camera(g_allCameras[i]);
103 | // if (true == g_camera.IsOpen)
104 | // g_camera.Close();
105 |
106 | // g_camera.Open();
107 | // string id = g_camera.Parameters[Basler.Pylon.PLCamera.DeviceUserID].GetValue();
108 | // int id1;
109 | // if (int.TryParse(id, out id1))
110 | // {
111 | // if (CameraDic.Keys.Contains(id1) == false)
112 | // {
113 | // CameraDic.Add(id1, new BaslerCamera(g_camera, id1));
114 | // cameraCount++;
115 | // }
116 | // }
117 | // else
118 | // {
119 | // Util.Notify("相机ID未设置");
120 | // }
121 | // }
122 | //}
123 |
124 | //#endregion
125 | }
126 | if ((CameraFlag & CameraFlag.Hikvision) == CameraFlag.Hikvision)
127 | {
128 | #region 海康相机遍历枚举
129 | MyCamera.MV_CC_DEVICE_INFO_LIST m_pDeviceList = new MyCamera.MV_CC_DEVICE_INFO_LIST();
130 | int nRet;
131 |
132 |
133 | nRet = CameraOperator.EnumDevices(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref m_pDeviceList);
134 | if (0 == nRet && m_pDeviceList.nDeviceNum > 0)
135 | {
136 | for (int i = 0; i < m_pDeviceList.nDeviceNum; i++)
137 | {
138 |
139 |
140 | MyCamera.MV_CC_DEVICE_INFO device =
141 | (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[i],
142 | typeof(MyCamera.MV_CC_DEVICE_INFO));
143 |
144 | if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
145 | {
146 | IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stGigEInfo, 0);
147 | MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_GIGE_DEVICE_INFO));
148 |
149 | int key;
150 |
151 | if (int.TryParse(gigeInfo.chUserDefinedName, out key))
152 | {
153 | if (CameraDic.Keys.Contains(key) == false)
154 | {
155 | CameraOperator m_pOperator = new CameraOperator();
156 | //打开设备
157 | nRet = m_pOperator.Open(ref device);
158 | //Util.Notify("打开相机");
159 | if (MyCamera.MV_OK == nRet)
160 | {
161 | CameraDic.Add(key, new HikvisionCamera(m_pOperator, key));
162 | cameraCount++;
163 | }
164 | }
165 | }
166 | else
167 | {
168 | Util.Notify("相机ID未设置");
169 | }
170 | }
171 | else if (device.nTLayerType == MyCamera.MV_USB_DEVICE)
172 | {
173 | IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stUsb3VInfo, 0);
174 | MyCamera.MV_USB3_DEVICE_INFO usbInfo = (MyCamera.MV_USB3_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_USB3_DEVICE_INFO));
175 |
176 | int key;
177 |
178 | if (int.TryParse(usbInfo.chUserDefinedName, out key))
179 | {
180 | if (CameraDic.Keys.Contains(key) == false)
181 | {
182 | CameraOperator m_pOperator = new CameraOperator();
183 | //打开设备
184 | nRet = m_pOperator.Open(ref device);
185 | if (MyCamera.MV_OK == nRet)
186 | {
187 | CameraDic.Add(key, new HikvisionCamera(m_pOperator, key));
188 | cameraCount++;
189 | }
190 | }
191 | else
192 | {
193 | Util.Notify("相机ID未设置");
194 | }
195 | }
196 | }
197 | }
198 | }
199 |
200 | #endregion
201 | }
202 | if ((CameraFlag & CameraFlag.Mindvision) == CameraFlag.Mindvision)
203 | {
204 | #region 迈徳威视相机遍历枚举
205 |
206 | CameraSdkStatus status;
207 | tSdkCameraDevInfo[] tCameraDevInfoList;
208 |
209 | status = MvApi.CameraEnumerateDevice(out tCameraDevInfoList);
210 | if (status == CameraSdkStatus.CAMERA_STATUS_SUCCESS)
211 | {
212 | if (tCameraDevInfoList != null && tCameraDevInfoList.Count() > 0)//此时iCameraCounts返回了实际连接的相机个数。如果大于1,则初始化第一个相机
213 | {
214 | for (int i = 0; i < tCameraDevInfoList.Count(); i++)
215 | {
216 | IntPtr m_Grabber;
217 | status = MvApi.CameraGrabber_Create(out m_Grabber, ref tCameraDevInfoList[i]);
218 | if (status == CameraSdkStatus.CAMERA_STATUS_SUCCESS)
219 | {
220 | CameraHandle m_hCamera = 0; // 句柄
221 | MvApi.CameraGrabber_GetCameraHandle(m_Grabber, out m_hCamera);
222 | byte[] pName = new byte[255];
223 | MvApi.CameraGetFriendlyName(m_hCamera, pName);
224 | string str = System.Text.Encoding.ASCII.GetString(pName).Substring(0, 1);
225 |
226 | int key = -1;
227 |
228 | if (int.TryParse(str, out key) && CameraDic.Keys.Contains(key) == false)
229 | {
230 | CameraDic.Add(key, new MindCamera(m_hCamera, m_Grabber, key));
231 | cameraCount++;
232 | }
233 | //else
234 | //{
235 | // Util.Notify("相机ID未设置");
236 | //}
237 | }
238 | }
239 | }
240 | }
241 |
242 | #endregion
243 | }
244 | if ((CameraFlag & CameraFlag.Microvision) == CameraFlag.Microvision)
245 | {
246 | #region 维视图像相机相机遍历枚举
247 |
248 | //个数枚举
249 | MVGigE.MVInitLib();
250 | int CamNum = 0;
251 | MVSTATUS r = MVGigE.MVGetNumOfCameras(out CamNum);
252 | for (int i = 0; i < CamNum; i++)
253 | {
254 | IntPtr m_hCam;
255 | byte index = (byte)i;
256 | r = MVGigE.MVOpenCamByIndex(index, out m_hCam);
257 | if (m_hCam != IntPtr.Zero)
258 | {
259 | MVCamInfo pCamInfo;
260 | MVGigE.MVGetCameraInfo(index, out pCamInfo);
261 |
262 | string str = pCamInfo.mUserDefinedName;
263 |
264 | int key = -1;
265 |
266 | if (int.TryParse(str, out key) && CameraDic.Keys.Contains(key) == false)
267 | {
268 | CameraDic.Add(key, new MicrovisionCamera(m_hCam, key));
269 | cameraCount++;
270 | }
271 | else
272 | {
273 | Util.Notify("相机ID未设置");
274 | }
275 |
276 | }
277 | }
278 |
279 | #endregion
280 | }
281 | if ((CameraFlag & CameraFlag.ICImaging) == CameraFlag.ICImaging)
282 | {
283 | #region 映美精相机遍历查询
284 | TIS.Imaging.ICImagingControl cameraFind = new TIS.Imaging.ICImagingControl();
285 |
286 | foreach (TIS.Imaging.Device Item in cameraFind.Devices)
287 | {
288 | string cameraName = Item.Name;
289 | int index1 = cameraName.IndexOf("[");
290 | if (index1 > -1)
291 | {
292 | string dat = cameraName.Substring(index1);
293 | string dat1 = dat.Replace("[", "").Replace("]", "");
294 | int id1;
295 | if (int.TryParse(dat1, out id1))
296 | {
297 | if (CameraDic.Keys.Contains(id1) == false)
298 | {
299 | TIS.Imaging.ICImagingControl cameraTmp = new TIS.Imaging.ICImagingControl();
300 | cameraTmp.Device = Item.Name;
301 | bool x = cameraTmp.DeviceValid;
302 | CameraDic.Add(id1, new ICImagingCamera(cameraTmp, id1));
303 | cameraCount++;
304 | }
305 | }
306 | }
307 |
308 | }
309 |
310 | #endregion
311 | }
312 | if ((CameraFlag & CameraFlag.DirectShow) == CameraFlag.DirectShow)
313 | {
314 | #region DirectShow相机 当前只支持一个相机
315 | HFramegrabber framegrabber = new HFramegrabber();
316 | //HTuple Information = null;
317 | //HTuple ValueList = null;
318 | //HOperatorSet.InfoFramegrabber("DirectShow", "info_boards",out Information,out ValueList);
319 | framegrabber.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, DirectShowColorSpace,
320 | -1, "false", DirectShowCameraType, DirectShowIndex, 0, -1);
321 | CameraDic.Add(1, new DirectShowCamera(framegrabber, 1));
322 | #endregion
323 | }
324 | if ((CameraFlag & CameraFlag.DaHeng) == CameraFlag.DaHeng)
325 | {
326 | //#region 大恒相机
327 | //List listGXDeviceInfo = new List();
328 | //IGXFactory IGXFactory1 = IGXFactory.GetInstance();
329 | //IGXFactory1.Init();
330 | //IGXFactory1.UpdateDeviceList(200, listGXDeviceInfo);
331 | //foreach (var item in listGXDeviceInfo)
332 | //{
333 | // string name = item.GetUserID();
334 | // int key;
335 |
336 | // if (int.TryParse(name, out key))
337 | // {
338 | // if (CameraDic.Keys.Contains(key) == false)
339 | // {
340 | // IGXDevice IGXDevice1 = null;
341 | // IGXDevice1 = IGXFactory1.OpenDeviceBySN(item.GetSN(), GX_ACCESS_MODE.GX_ACCESS_EXCLUSIVE);
342 | // CameraDic.Add(key, new DHCamera(IGXFactory1, IGXDevice1));
343 |
344 | // }
345 | // }
346 |
347 | //}
348 |
349 | //#endregion
350 | }
351 |
352 | if ((CameraFlag & CameraFlag.Gige) == CameraFlag.Gige)
353 | {
354 | //#region Gige相机遍历查询
355 |
356 | HTuple Information = null;
357 | HTuple ValueList = null;
358 | HOperatorSet.InfoFramegrabber("GigEVision", "info_boards", out Information, out ValueList);
359 | for (int i = 0; i < ValueList.Length; i++)
360 | {
361 | string valueTmp = ValueList[i];
362 | string name = GetGigeValue(valueTmp, "user_name:");
363 |
364 | string device = GetGigeValue(valueTmp, "device:");
365 | if (name != null && device != null)
366 | {
367 | int id1;
368 | if (int.TryParse(name, out id1))
369 | {
370 | if (CameraDic.Keys.Contains(id1) == false)
371 | {
372 | HFramegrabber grabber = new HFramegrabber();
373 | grabber.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1,
374 | "default", -1, "false", "default", device, 0, -1);
375 | CameraDic.Add(id1, new GigeCamera(grabber, id1));
376 | cameraCount++;
377 | }
378 | }
379 | }
380 | }
381 | //List g_allCameras = CameraFinder.Enumerate();
382 |
383 | //if (g_allCameras.Count > 0)
384 | //{
385 | // for (int i = 0; i < g_allCameras.Count; i++)
386 | // {
387 | // Basler.Pylon.Camera g_camera = new Basler.Pylon.Camera(g_allCameras[i]);
388 | // if (true == g_camera.IsOpen)
389 | // g_camera.Close();
390 |
391 | // g_camera.Open();
392 | // string id = g_camera.Parameters[PLCamera.DeviceUserID].GetValue();
393 | // int id1;
394 | // if (int.TryParse(id, out id1))
395 | // {
396 | // if (CameraDic.Keys.Contains(id1) == false)
397 | // {
398 | // CameraDic.Add(id1, new BaslerCamera(g_camera, id1));
399 | // cameraCount++;
400 | // }
401 | // }
402 | // else
403 | // {
404 | // Util.Notify("相机ID未设置");
405 | // }
406 | // }
407 | //}
408 |
409 | //#endregion
410 | }
411 | //打开相机
412 | foreach (var item in CameraDic.Values)
413 | {
414 | item.Open();
415 | Thread.Sleep(30);
416 | }
417 |
418 | if (CameraInitFinishEvent != null)
419 | {
420 | CameraInitFinishEvent(null, null);
421 | }
422 | //else
423 | //{
424 | // Util.Notify("无相机连接");
425 | // return false;
426 | //}
427 | }
428 | catch (Exception ex)
429 | {
430 | Util.WriteLog(typeof(CameraManger), ex);
431 | Util.Notify("相机打开出现异常");
432 | Close();
433 | return false;
434 | }
435 | return true;
436 | }
437 |
438 | public static void Close()
439 | {
440 | foreach (var item in CameraDic.Values)
441 | {
442 | try
443 | {
444 | item.Close();
445 | }
446 | catch (Exception ex)
447 | {
448 | Util.WriteLog(typeof(CameraManger), ex);
449 | Util.Notify("相机关闭出现异常");
450 | }
451 | }
452 | CameraDic = new Dictionary();
453 | }
454 | }
455 | }
456 |
--------------------------------------------------------------------------------
/Yoga.Camera/CameraOperator.cs:
--------------------------------------------------------------------------------
1 | /***************************************************************************************************
2 | *
3 | * 版权信息:版权所有 (c) 2015, 杭州海康威视数字技术股份有限公司, 保留所有权利
4 | *
5 | * 文件名称:CameraOperator.cs
6 | * 摘 要:相机操作类
7 | *
8 | * 当前版本:1.0.0.0
9 | * 作 者:孙海祥
10 | * 日 期:2016-07-07
11 | * 备 注:新建
12 | ***************************************************************************************************/
13 | using System;
14 | using System.Collections.Generic;
15 | using System.Linq;
16 | using System.Text;
17 | using MvCamCtrl.NET;
18 | using System.Runtime.InteropServices;
19 |
20 |
21 | namespace DeviceSource
22 | {
23 | using ImageCallBack = MyCamera.cbOutputdelegate;
24 | using ExceptionCallBack = MyCamera.cbExceptiondelegate;
25 | public class CameraOperator
26 | {
27 | public const int CO_FAIL = -1;
28 | public const int CO_OK = 0;
29 | private MyCamera m_pCSI;
30 | //private delegate void ImageCallBack(IntPtr pData, ref MyCamera.MV_FRAME_OUT_INFO pFrameInfo, IntPtr pUser);
31 |
32 | public CameraOperator()
33 | {
34 | // m_pDeviceList = new MyCamera.MV_CC_DEVICE_INFO_LIST();
35 | m_pCSI = new MyCamera();
36 | }
37 |
38 | /****************************************************************************
39 | * @fn EnumDevices
40 | * @brief 枚举可连接设备
41 | * @param nLayerType IN 传输层协议:1-GigE; 4-USB;可叠加
42 | * @param stDeviceList OUT 设备列表
43 | * @return 成功:0;错误:错误码
44 | ****************************************************************************/
45 | ///
46 | /// 枚举可连接设备
47 | ///
48 | /// 传输层协议:1-GigE; 4-USB;可叠加
49 | /// 设备列表
50 | /// 成功:0;错误:错误码
51 | public static int EnumDevices(uint nLayerType, ref MyCamera.MV_CC_DEVICE_INFO_LIST stDeviceList)
52 | {
53 | return MyCamera.MV_CC_EnumDevices_NET(nLayerType, ref stDeviceList);
54 | }
55 |
56 |
57 | /****************************************************************************
58 | * @fn Open
59 | * @brief 连接设备
60 | * @param stDeviceInfo IN 设备信息结构体
61 | * @return 成功:0;错误:-1
62 | ****************************************************************************/
63 | ///
64 | /// 连接设备
65 | ///
66 | /// 设备信息结构体
67 | /// 成功:0;错误:-1
68 | public int Open(ref MyCamera.MV_CC_DEVICE_INFO stDeviceInfo)
69 | {
70 | if (null == m_pCSI)
71 | {
72 | m_pCSI = new MyCamera();
73 | if (null == m_pCSI)
74 | {
75 | return CO_FAIL;
76 | }
77 | }
78 |
79 | int nRet;
80 | nRet = m_pCSI.MV_CC_CreateDevice_NET(ref stDeviceInfo);
81 | if (MyCamera.MV_OK != nRet)
82 | {
83 | return CO_FAIL;
84 | }
85 |
86 | nRet = m_pCSI.MV_CC_OpenDevice_NET();
87 | if (MyCamera.MV_OK != nRet)
88 | {
89 | return CO_FAIL;
90 | }
91 | return CO_OK;
92 | }
93 |
94 |
95 | /****************************************************************************
96 | * @fn Close
97 | * @brief 关闭设备
98 | * @param none
99 | * @return 成功:0;错误:-1
100 | ****************************************************************************/
101 | ///
102 | /// 关闭设备
103 | ///
104 | /// 成功:0;错误:-1
105 | public int Close()
106 | {
107 | int nRet;
108 |
109 | nRet = m_pCSI.MV_CC_CloseDevice_NET();
110 | if (MyCamera.MV_OK != nRet)
111 | {
112 | return CO_FAIL;
113 | }
114 |
115 | nRet = m_pCSI.MV_CC_DestroyDevice_NET();
116 | if (MyCamera.MV_OK != nRet)
117 | {
118 | return CO_FAIL;
119 | }
120 | return CO_OK;
121 | }
122 |
123 |
124 | /****************************************************************************
125 | * @fn StartGrabbing
126 | * @brief 开始采集
127 | * @param none
128 | * @return 成功:0;错误:-1
129 | ****************************************************************************/
130 | ///
131 | /// 开始采集
132 | ///
133 | /// 成功:0;错误:-1
134 | public int StartGrabbing()
135 | {
136 | int nRet;
137 | //开始采集
138 | nRet = m_pCSI.MV_CC_StartGrabbing_NET();
139 | if (MyCamera.MV_OK != nRet)
140 | {
141 | return CO_FAIL;
142 | }
143 | return CO_OK;
144 | }
145 |
146 |
147 |
148 | /****************************************************************************
149 | * @fn StopGrabbing
150 | * @brief 停止采集
151 | * @param none
152 | * @return 成功:0;错误:-1
153 | ****************************************************************************/
154 | ///
155 | /// 停止采集
156 | ///
157 | /// 成功:0;错误:-1
158 | public int StopGrabbing()
159 | {
160 | int nRet;
161 | nRet = m_pCSI.MV_CC_StopGrabbing_NET();
162 | if (MyCamera.MV_OK != nRet)
163 | {
164 | return CO_FAIL;
165 | }
166 | return CO_OK;
167 | }
168 |
169 |
170 | /****************************************************************************
171 | * @fn RegisterImageCallBack
172 | * @brief 注册取流回调函数
173 | * @param CallBackFunc IN 回调函数
174 | * @param pUser IN 用户参数
175 | * @return 成功:0;错误:-1
176 | ****************************************************************************/
177 | ///
178 | /// 注册取流回调函数
179 | ///
180 | /// 回调函数
181 | /// 用户参数
182 | /// 成功:0;错误:-1
183 | public int RegisterImageCallBack(ImageCallBack CallBackFunc, IntPtr pUser)
184 | {
185 | int nRet;
186 | nRet = m_pCSI.MV_CC_RegisterImageCallBack_NET(CallBackFunc, pUser);
187 | if (MyCamera.MV_OK != nRet)
188 | {
189 | return CO_FAIL;
190 | }
191 | return CO_OK;
192 | }
193 |
194 |
195 | /****************************************************************************
196 | * @fn RegisterExceptionCallBack
197 | * @brief 注册异常回调函数
198 | * @param CallBackFunc IN 回调函数
199 | * @param pUser IN 用户参数
200 | * @return 成功:0;错误:-1
201 | ****************************************************************************/
202 | ///
203 | /// 注册异常回调函数
204 | ///
205 | /// 回调函数
206 | /// 用户参数
207 | /// 成功:0;错误:-1
208 | public int RegisterExceptionCallBack(ExceptionCallBack CallBackFunc, IntPtr pUser)
209 | {
210 | int nRet;
211 | nRet = m_pCSI.MV_CC_RegisterExceptionCallBack_NET(CallBackFunc, pUser);
212 | if (MyCamera.MV_OK != nRet)
213 | {
214 | return CO_FAIL;
215 | }
216 | return CO_OK;
217 | }
218 |
219 |
220 | /****************************************************************************
221 | * @fn GetOneFrame
222 | * @brief 获取一帧图像数据
223 | * @param pData IN-OUT 数据数组指针
224 | * @param pnDataLen IN 数据大小
225 | * @param nDataSize IN 数组缓存大小
226 | * @param pFrameInfo OUT 数据信息
227 | * @return 成功:0;错误:-1
228 | ****************************************************************************/
229 | ///
230 | /// 获取一帧图像数据
231 | ///
232 | /// 数据数组指针
233 | /// 数据大小
234 | /// 数组缓存大小
235 | /// 数据信息
236 | /// 成功:0;错误:-1
237 | public int GetOneFrame(IntPtr pData, ref UInt32 pnDataLen, UInt32 nDataSize, ref MyCamera.MV_FRAME_OUT_INFO pFrameInfo)
238 | {
239 | pnDataLen = 0;
240 | int nRet = m_pCSI.MV_CC_GetOneFrame_NET(pData, nDataSize, ref pFrameInfo);
241 | pnDataLen = pFrameInfo.nFrameLen;
242 | if (MyCamera.MV_OK != nRet)
243 | {
244 | return nRet;
245 | }
246 |
247 | return nRet;
248 | }
249 |
250 | public int GetOneFrameTimeout(IntPtr pData, ref UInt32 pnDataLen, UInt32 nDataSize, ref MyCamera.MV_FRAME_OUT_INFO_EX pFrameInfo, Int32 nMsec)
251 | {
252 | pnDataLen = 0;
253 | int nRet = m_pCSI.MV_CC_GetOneFrameTimeout_NET(pData, nDataSize, ref pFrameInfo, nMsec);
254 | pnDataLen = pFrameInfo.nFrameLen;
255 | if (MyCamera.MV_OK != nRet)
256 | {
257 | return nRet;
258 | }
259 |
260 | return nRet;
261 | }
262 |
263 |
264 | /****************************************************************************
265 | * @fn Display
266 | * @brief 显示图像
267 | * @param hWnd IN 窗口句柄
268 | * @return 成功:0;错误:-1
269 | ****************************************************************************/
270 | ///
271 | /// 显示图像
272 | ///
273 | /// 窗口句柄
274 | /// 成功:0;错误:-1
275 | public int Display(IntPtr hWnd)
276 | {
277 | int nRet;
278 | nRet = m_pCSI.MV_CC_Display_NET(hWnd);
279 | if (MyCamera.MV_OK != nRet)
280 | {
281 | return CO_FAIL;
282 | }
283 | return CO_OK;
284 | }
285 |
286 |
287 | /****************************************************************************
288 | * @fn GetIntValue
289 | * @brief 获取Int型参数值
290 | * @param strKey IN 参数键值,具体键值名称参考HikCameraNode.xls文档
291 | * @param pnValue OUT 返回值
292 | * @return 成功:0;错误:-1
293 | ****************************************************************************/
294 | ///
295 | /// 获取Int型参数值
296 | ///
297 | /// 参数键值,具体键值名称参考HikCameraNode.xls文档
298 | /// 返回值
299 | /// 成功:0;错误:-1
300 | public int GetIntValue(string strKey, ref UInt32 pnValue)
301 | {
302 |
303 | MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
304 | int nRet = m_pCSI.MV_CC_GetIntValue_NET(strKey, ref stParam);
305 | if (MyCamera.MV_OK != nRet)
306 | {
307 | return CO_FAIL;
308 | }
309 |
310 | pnValue = stParam.nCurValue;
311 |
312 | return CO_OK;
313 | }
314 |
315 |
316 | /****************************************************************************
317 | * @fn SetIntValue
318 | * @brief 设置Int型参数值
319 | * @param strKey IN 参数键值,具体键值名称参考HikCameraNode.xls文档
320 | * @param nValue IN 设置参数值,具体取值范围参考HikCameraNode.xls文档
321 | * @return 成功:0;错误:-1
322 | ****************************************************************************/
323 | ///
324 | /// 设置Int型参数值
325 | ///
326 | ///
327 | ///
328 | /// 成功:0;错误:-1
329 | public int SetIntValue(string strKey, UInt32 nValue)
330 | {
331 |
332 | int nRet = m_pCSI.MV_CC_SetIntValue_NET(strKey, nValue);
333 | if (MyCamera.MV_OK != nRet)
334 | {
335 | return CO_FAIL;
336 | }
337 | return CO_OK;
338 | }
339 |
340 |
341 |
342 | /****************************************************************************
343 | * @fn GetFloatValue
344 | * @brief 获取Float型参数值
345 | * @param strKey IN 参数键值,具体键值名称参考HikCameraNode.xls文档
346 | * @param pValue OUT 返回值
347 | * @return 成功:0;错误:-1
348 | ****************************************************************************/
349 | ///
350 | /// 获取Float型参数值
351 | ///
352 | ///
353 | ///
354 | ///
355 | public int GetFloatValue(string strKey, ref float pfValue)
356 | {
357 | MyCamera.MVCC_FLOATVALUE stParam = new MyCamera.MVCC_FLOATVALUE();
358 | int nRet = m_pCSI.MV_CC_GetFloatValue_NET(strKey, ref stParam);
359 | if (MyCamera.MV_OK != nRet)
360 | {
361 | return CO_FAIL;
362 | }
363 |
364 | pfValue = stParam.fCurValue;
365 |
366 | return CO_OK;
367 | }
368 |
369 |
370 | /****************************************************************************
371 | * @fn SetFloatValue
372 | * @brief 设置Float型参数值
373 | * @param strKey IN 参数键值,具体键值名称参考HikCameraNode.xls文档
374 | * @param fValue IN 设置参数值,具体取值范围参考HikCameraNode.xls文档
375 | * @return 成功:0;错误:-1
376 | ****************************************************************************/
377 | ///
378 | /// 设置Float型参数值
379 | ///
380 | ///
381 | ///
382 | ///
383 | public int SetFloatValue(string strKey, float fValue)
384 | {
385 | int nRet = m_pCSI.MV_CC_SetFloatValue_NET(strKey, fValue);
386 | if (MyCamera.MV_OK != nRet)
387 | {
388 | return CO_FAIL;
389 | }
390 | return CO_OK;
391 | }
392 |
393 |
394 | /****************************************************************************
395 | * @fn GetEnumValue
396 | * @brief 获取Enum型参数值
397 | * @param strKey IN 参数键值,具体键值名称参考HikCameraNode.xls文档
398 | * @param pnValue OUT 返回值
399 | * @return 成功:0;错误:-1
400 | ****************************************************************************/
401 | ///
402 | /// 获取Enum型参数值
403 | ///
404 | ///
405 | ///
406 | ///
407 | public int GetEnumValue(string strKey, ref UInt32 pnValue)
408 | {
409 | MyCamera.MVCC_ENUMVALUE stParam = new MyCamera.MVCC_ENUMVALUE();
410 | int nRet = m_pCSI.MV_CC_GetEnumValue_NET(strKey, ref stParam);
411 | if (MyCamera.MV_OK != nRet)
412 | {
413 | return CO_FAIL;
414 | }
415 |
416 | pnValue = stParam.nCurValue;
417 |
418 | return CO_OK;
419 | }
420 |
421 |
422 |
423 | /****************************************************************************
424 | * @fn SetEnumValue
425 | * @brief 设置Float型参数值
426 | * @param strKey IN 参数键值,具体键值名称参考HikCameraNode.xls文档
427 | * @param nValue IN 设置参数值,具体取值范围参考HikCameraNode.xls文档
428 | * @return 成功:0;错误:-1
429 | ****************************************************************************/
430 | public int SetEnumValue(string strKey, UInt32 nValue)
431 | {
432 | int nRet = m_pCSI.MV_CC_SetEnumValue_NET(strKey, nValue);
433 | if (MyCamera.MV_OK != nRet)
434 | {
435 | return CO_FAIL;
436 | }
437 | return CO_OK;
438 | }
439 |
440 |
441 |
442 | /****************************************************************************
443 | * @fn GetBoolValue
444 | * @brief 获取Bool型参数值
445 | * @param strKey IN 参数键值,具体键值名称参考HikCameraNode.xls文档
446 | * @param pbValue OUT 返回值
447 | * @return 成功:0;错误:-1
448 | ****************************************************************************/
449 | public int GetBoolValue(string strKey, ref bool pbValue)
450 | {
451 | int nRet = m_pCSI.MV_CC_GetBoolValue_NET(strKey, ref pbValue);
452 | if (MyCamera.MV_OK != nRet)
453 | {
454 | return CO_FAIL;
455 | }
456 |
457 | return CO_OK;
458 | }
459 |
460 |
461 | /****************************************************************************
462 | * @fn SetBoolValue
463 | * @brief 设置Bool型参数值
464 | * @param strKey IN 参数键值,具体键值名称参考HikCameraNode.xls文档
465 | * @param bValue IN 设置参数值,具体取值范围参考HikCameraNode.xls文档
466 | * @return 成功:0;错误:-1
467 | ****************************************************************************/
468 | public int SetBoolValue(string strKey, bool bValue)
469 | {
470 | int nRet = m_pCSI.MV_CC_SetBoolValue_NET(strKey, bValue);
471 | if (MyCamera.MV_OK != nRet)
472 | {
473 | return CO_FAIL;
474 | }
475 | return CO_OK;
476 | }
477 |
478 |
479 | /****************************************************************************
480 | * @fn GetStringValue
481 | * @brief 获取String型参数值
482 | * @param strKey IN 参数键值,具体键值名称参考HikCameraNode.xls文档
483 | * @param strValue OUT 返回值
484 | * @return 成功:0;错误:-1
485 | ****************************************************************************/
486 | public int GetStringValue(string strKey, ref string strValue)
487 | {
488 | MyCamera.MVCC_STRINGVALUE stParam = new MyCamera.MVCC_STRINGVALUE();
489 | int nRet = m_pCSI.MV_CC_GetStringValue_NET(strKey, ref stParam);
490 | if (MyCamera.MV_OK != nRet)
491 | {
492 | return CO_FAIL;
493 | }
494 |
495 | strValue = stParam.chCurValue;
496 |
497 | return CO_OK;
498 | }
499 |
500 |
501 | /****************************************************************************
502 | * @fn SetStringValue
503 | * @brief 设置String型参数值
504 | * @param strKey IN 参数键值,具体键值名称参考HikCameraNode.xls文档
505 | * @param strValue IN 设置参数值,具体取值范围参考HikCameraNode.xls文档
506 | * @return 成功:0;错误:-1
507 | ****************************************************************************/
508 | public int SetStringValue(string strKey, string strValue)
509 | {
510 | int nRet = m_pCSI.MV_CC_SetStringValue_NET(strKey, strValue);
511 | if (MyCamera.MV_OK != nRet)
512 | {
513 | return CO_FAIL;
514 | }
515 | return CO_OK;
516 | }
517 |
518 |
519 | /****************************************************************************
520 | * @fn CommandExecute
521 | * @brief Command命令
522 | * @param strKey IN 参数键值,具体键值名称参考HikCameraNode.xls文档
523 | * @return 成功:0;错误:-1
524 | ****************************************************************************/
525 | public int CommandExecute(string strKey)
526 | {
527 | int nRet = m_pCSI.MV_CC_SetCommandValue_NET(strKey);
528 | if (MyCamera.MV_OK != nRet)
529 | {
530 | return CO_FAIL;
531 | }
532 | return CO_OK;
533 | }
534 |
535 |
536 | /****************************************************************************
537 | * @fn SaveImage
538 | * @brief 保存图片
539 | * @param pSaveParam IN 保存图片配置参数结构体
540 | * @return 成功:0;错误:-1
541 | ****************************************************************************/
542 | public int SaveImage(ref MyCamera.MV_SAVE_IMAGE_PARAM_EX pSaveParam)
543 | {
544 | int nRet;
545 | nRet = m_pCSI.MV_CC_SaveImageEx_NET(ref pSaveParam);
546 | return nRet;
547 | }
548 | }
549 | }
550 |
551 |
--------------------------------------------------------------------------------
/Yoga.Camera/CameraPram.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Yoga.Camera
8 | {
9 |
10 | public enum ImageAngle
11 | {
12 | 角度0,
13 | 角度90,
14 | 角度180,
15 | 角度270
16 | }
17 |
18 | [Serializable]
19 | public class CameraPram
20 | {
21 | ///
22 | /// 曝光时间
23 | ///
24 | private long shutter = 3500;
25 | ///
26 | /// 增益
27 | ///
28 | private double gain = 0;
29 | ///
30 | /// 外触发延时
31 | ///
32 | private double triggerDelayAbs = 10;
33 | ///
34 | /// 外触发防抖动
35 | ///
36 | private double lineDebouncerTimeAbs = 10;
37 | ///
38 | /// 输出信号输出时间
39 | ///
40 | private double outLineTime = 1000;
41 |
42 | private ImageAngle imageAngle = ImageAngle.角度0;
43 |
44 | public ImageAngle ImageAngle
45 | {
46 | get
47 | {
48 | return imageAngle;
49 | }
50 | set
51 | {
52 | imageAngle = value;
53 | }
54 | }
55 | public long Shutter
56 | {
57 | get
58 | {
59 | return shutter;
60 | }
61 |
62 | set
63 | {
64 | shutter = value;
65 | }
66 | }
67 |
68 | public double Gain
69 | {
70 | get
71 | {
72 | return gain;
73 | }
74 |
75 | set
76 | {
77 | gain = value;
78 | }
79 | }
80 |
81 | public double TriggerDelayAbs
82 | {
83 | get
84 | {
85 | return triggerDelayAbs;
86 | }
87 |
88 | set
89 | {
90 | triggerDelayAbs = value;
91 | }
92 | }
93 |
94 | public double LineDebouncerTimeAbs
95 | {
96 | get
97 | {
98 | return lineDebouncerTimeAbs;
99 | }
100 |
101 | set
102 | {
103 | lineDebouncerTimeAbs = value;
104 | }
105 | }
106 |
107 | public double OutLineTime
108 | {
109 | get
110 | {
111 | return outLineTime;
112 | }
113 |
114 | set
115 | {
116 | outLineTime = value;
117 | }
118 | }
119 | }
120 | }
121 |
--------------------------------------------------------------------------------
/Yoga.Camera/DHCamera.cs:
--------------------------------------------------------------------------------
1 | //using GxIAPINET;
2 | using HalconDotNet;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading;
8 | using System.Threading.Tasks;
9 | using Yoga.Common;
10 |
11 | namespace Yoga.Camera
12 | {
13 | //public class DHCamera : CameraBase
14 | // {
15 | // private bool ignoreImage = false;
16 | // private IGXDevice IGXDevice1 = null;
17 | // ///
18 | // /// Factory对像
19 | // ///
20 | // private IGXFactory IGXFactory1 = null;
21 | // ///
22 | // /// 流对像
23 | // ///
24 | // private IGXStream IGXStream1 = null;
25 | // ///
26 | // /// 远端设备属性控制器对像
27 | // ///
28 | // private IGXFeatureControl IGXFeatureControl = null;
29 | // public override double GainCur
30 | // {
31 | // get
32 | // {
33 | // return gainCur;
34 | // }
35 |
36 | // set
37 | // {
38 | // try
39 | // {
40 | // double gainValue = value;
41 |
42 | // //判断输入值是否在增益值的范围内
43 | // //若输入的值大于最大值则将增益值设置成最大值
44 | // if (gainValue > GainMax)
45 | // {
46 | // gainValue = GainMax;
47 | // }
48 |
49 | // //若输入的值小于最小值则将增益的值设置成最小值
50 | // if (gainValue < GainMin)
51 | // {
52 | // gainValue = GainMin;
53 | // }
54 |
55 | // IGXFeatureControl.GetFloatFeature("Gain").SetValue(gainValue);
56 | // gainCur = gainValue;
57 | // }
58 | // catch (Exception ex)
59 | // {
60 | // Util.WriteLog(this.GetType(), ex);
61 | // Util.Notify("相机增益设置异常");
62 | // }
63 | // }
64 | // }
65 |
66 | // public override long ShuterCur
67 | // {
68 | // get
69 | // {
70 | // return shuterCur;
71 | // }
72 |
73 | // set
74 | // {
75 | // try
76 | // {
77 | // long shutterValue = value;
78 |
79 | // //获取当前相机的曝光值、最小值、最大值和单位
80 |
81 | // //判断输入值是否在曝光时间的范围内
82 | // //若大于最大值则将曝光值设为最大值
83 |
84 | // if (shutterValue > ShuterMax)
85 | // {
86 | // shutterValue = ShuterMax;
87 | // }
88 | // //若小于最小值将曝光值设为最小值
89 | // else if (shutterValue < ShuterMin)
90 | // {
91 | // shutterValue = ShuterMin;
92 | // }
93 |
94 | // IGXFeatureControl.GetFloatFeature("ExposureTime").SetValue(shutterValue);
95 | // shuterCur = shutterValue;
96 | // }
97 | // catch (Exception ex)
98 | // {
99 | // Util.WriteLog(this.GetType(), ex);
100 | // Util.Notify("相机曝光设置异常");
101 | // }
102 | // }
103 | // }
104 | // public DHCamera(IGXFactory IGXFactory1, IGXDevice IGXDevice1)
105 | // {
106 | // this.IGXFactory1 = IGXFactory1;
107 | // this.IGXDevice1 = IGXDevice1;
108 | // IGXFeatureControl = IGXDevice1.GetRemoteFeatureControl();
109 | // }
110 | // public override void Close()
111 | // {
112 | // try
113 | // {
114 | // ContinuousShotStop();
115 | // }
116 | // catch (Exception )
117 | // {
118 | // //LogHelper.WriteLog(typeof(DHCamera), "相机停止采集异常" + ex.ToString());
119 | // }
120 |
121 | // try
122 | // {
123 | // //停止流通道、注销采集回调和关闭流
124 | // if (null != IGXStream1)
125 | // {
126 | // IGXStream1.StopGrab();
127 | // IGXStream1.UnregisterCaptureCallback();
128 | // IGXStream1.Close();
129 | // IGXStream1 = null;
130 | // }
131 | // }
132 | // catch (Exception )
133 | // {
134 | // //LogHelper.WriteLog(typeof(DHCamera), "相机停止采集异常" + ex.ToString());
135 | // }
136 |
137 | // try
138 | // {
139 | // //关闭设备
140 | // if (null != IGXDevice1)
141 | // {
142 | // IGXDevice1.Close();
143 | // IGXDevice1 = null;
144 | // }
145 | // }
146 | // catch (Exception )
147 | // {
148 | // //LogHelper.WriteLog(typeof(DHCamera), "相机停止采集异常" + ex.ToString());
149 | // }
150 |
151 | // try
152 | // {
153 | // //反初始化
154 | // if (null != IGXFactory1)
155 | // {
156 | // IGXFactory1.Uninit();
157 | // }
158 | // }
159 | // catch (Exception )
160 | // {
161 | // //LogHelper.WriteLog(typeof(DHCamera), "相机初始化异常" + ex.ToString());
162 | // }
163 | // }
164 |
165 | // public override void ContinuousShot()
166 | // {
167 | // try
168 | // {
169 | // Command = Command.Video;
170 | // #region 停止采集
171 | // //发送停采命令
172 | // if (null != IGXFeatureControl)
173 | // {
174 | // IGXFeatureControl.GetCommandFeature("AcquisitionStop").Execute();
175 | // }
176 | // //关闭采集流通道
177 | // if (null != IGXStream1)
178 | // {
179 | // IGXStream1.StopGrab();
180 | // //注销采集回调函数
181 | // IGXStream1.UnregisterCaptureCallback();
182 | // }
183 | // #endregion
184 | // #region 修改触发模式
185 | // if (null != IGXFeatureControl)
186 | // {
187 | // //设置触发模式为关
188 | // IGXFeatureControl.GetEnumFeature("TriggerMode").SetValue("Off");
189 | // }
190 | // #endregion
191 | // #region 打开采集
192 | // //开启采集流通道
193 | // if (null != IGXStream1)
194 | // {
195 | // //RegisterCaptureCallback第一个参数属于用户自定参数(类型必须为引用
196 | // //类型),若用户想用这个参数可以在委托函数中进行使用
197 | // IGXStream1.RegisterCaptureCallback(this, CaptureCallbackPro);
198 | // IGXStream1.StartGrab();
199 | // }
200 | // //发送开采命令
201 | // if (null != IGXFeatureControl)
202 | // {
203 | // IGXFeatureControl.GetCommandFeature("AcquisitionStart").Execute();
204 | // }
205 | // #endregion
206 | // IsContinuousShot = true;
207 | // }
208 | // catch (Exception ex)
209 | // {
210 | // Util.WriteLog(this.GetType(), ex);
211 | // Util.Notify("相机连续采集开始异常");
212 | // }
213 | // }
214 |
215 | // public override void ContinuousShotStop()
216 | // {
217 | // try
218 | // {
219 | // #region 停止采集
220 | // //发送停采命令
221 | // if (null != IGXFeatureControl)
222 | // {
223 | // IGXFeatureControl.GetCommandFeature("AcquisitionStop").Execute();
224 | // }
225 | // //关闭采集流通道
226 | // if (null != IGXStream1)
227 | // {
228 | // IGXStream1.StopGrab();
229 | // //注销采集回调函数
230 | // IGXStream1.UnregisterCaptureCallback();
231 | // }
232 | // #endregion
233 |
234 | // #region 修改触发模式
235 | // if (null != IGXFeatureControl)
236 | // {
237 | // //设置触发模式为开
238 | // IGXFeatureControl.GetEnumFeature("TriggerMode").SetValue("On");
239 | // }
240 | // #endregion
241 | // #region 打开采集
242 | // //开启采集流通道
243 | // if (null != IGXStream1)
244 | // {
245 | // //RegisterCaptureCallback第一个参数属于用户自定参数(类型必须为引用
246 | // //类型),若用户想用这个参数可以在委托函数中进行使用
247 | // IGXStream1.RegisterCaptureCallback(this, CaptureCallbackPro);
248 | // IGXStream1.StartGrab();
249 | // }
250 | // //发送开采命令
251 | // if (null != IGXFeatureControl)
252 | // {
253 | // IGXFeatureControl.GetCommandFeature("AcquisitionStart").Execute();
254 | // }
255 | // #endregion
256 | // IsContinuousShot = false;
257 | // IsExtTrigger = false;
258 | // ignoreImage = true;
259 | // Task.Run(() =>
260 | // {
261 | // Thread.Sleep(1000);
262 | // ignoreImage = false;
263 | // });
264 | // }
265 | // catch (Exception ex)
266 | // {
267 | // Util.WriteLog(this.GetType(), ex);
268 | // Util.Notify("相机连续采集停止异常");
269 | // }
270 | // }
271 |
272 | // public override void OneShot(Command command)
273 | // {
274 | // try
275 | // {
276 | // if (IsContinuousShot || IsExtTrigger)
277 | // {
278 | // ContinuousShotStop();
279 | // }
280 | // Command = command;
281 | // //发送软触发命令
282 | // if (null != IGXFeatureControl)
283 | // {
284 | // IGXFeatureControl.GetCommandFeature("TriggerSoftware").Execute();
285 | // }
286 | // else
287 | // {
288 | // Util.Notify("相机软触发异常,设备未连接");
289 | // }
290 | // }
291 | // catch
292 | // {
293 | // IsLink = false;
294 | // Util.Notify("相机软触发异常");
295 | // }
296 | // }
297 | // private void CaptureCallbackPro(object objUserParam, IFrameData objIFrameData)
298 | // {
299 | // try
300 | // {
301 | // if (ignoreImage)
302 | // {
303 | // return;
304 | // }
305 | // HOperatorSet.CountSeconds(out startTime);
306 | // {
307 |
308 | // hPylonImage = new HImage();
309 |
310 | // IntPtr ImageData = objIFrameData.GetBuffer();
311 | // if (objIFrameData.GetPixelFormat()== GX_PIXEL_FORMAT_ENTRY.GX_PIXEL_FORMAT_MONO8)
312 | // {
313 | // hPylonImage.GenImage1("byte",(int) objIFrameData.GetWidth(), (int)objIFrameData.GetHeight(), objIFrameData.GetBuffer());
314 | // }
315 | // else if (objIFrameData.GetPixelFormat() == GX_PIXEL_FORMAT_ENTRY.GX_PIXEL_FORMAT_RGB8_PLANAR)
316 | // {
317 | // hPylonImage.GenImageInterleaved(objIFrameData.GetBuffer(),
318 | // "rgb",
319 | // (int)objIFrameData.GetWidth(), (int)objIFrameData.GetHeight(),
320 | // -1, "byte",
321 | // (int)objIFrameData.GetWidth(), (int)objIFrameData.GetHeight(),
322 | // 0, 0, -1, 0);
323 | // }
324 | // else
325 | // {
326 | // Util.Notify(string.Format("相机{0}编码格式不正确,当前格式{1}", cameraIndex, objIFrameData.GetPixelFormat()));
327 | // }
328 | // TrigerImageEvent();
329 |
330 | // }
331 |
332 | // }
333 | // catch (System.ArgumentException ex)
334 | // {
335 | // Util.WriteLog(this.GetType(), ex);
336 | // Util.Notify(string.Format("相机{0}图像数据包丢失", cameraIndex));
337 | // }
338 | // catch (Exception ex)
339 | // {
340 | // Util.WriteLog(this.GetType(), ex);
341 | // Util.Notify(string.Format("相机{0}图像数据返回出现异常", cameraIndex));
342 | // }
343 | // }
344 | // public override bool Open()
345 | // {
346 | // try
347 | // {
348 | // //打开流
349 | // if (null != IGXDevice1)
350 | // {
351 | // IGXStream1 = IGXDevice1.OpenStream(0);
352 | // }
353 |
354 | // //初始化相机参数
355 | // if (null != IGXFeatureControl)
356 | // {
357 | // //设置采集模式连续采集------------------------------
358 | // IGXFeatureControl.GetEnumFeature("AcquisitionMode").SetValue("Continuous");
359 | // //设置触发模式为关
360 | // IGXFeatureControl.GetEnumFeature("TriggerMode").SetValue("On");
361 | // }
362 | // //开启采集流通道
363 | // if (null != IGXStream1)
364 | // {
365 | // //RegisterCaptureCallback第一个参数属于用户自定参数(类型必须为引用
366 | // //类型),若用户想用这个参数可以在委托函数中进行使用
367 | // IGXStream1.RegisterCaptureCallback(this, CaptureCallbackPro);
368 | // IGXStream1.StartGrab();
369 | // }
370 |
371 | // //发送开采命令
372 | // if (null != IGXFeatureControl)
373 | // {
374 | // IGXFeatureControl.GetCommandFeature("AcquisitionStart").Execute();
375 | // }
376 |
377 | // ContinuousShotStop();
378 |
379 | // // Reset the stopwatch used to reduce the amount of displayed images. The camera may acquire images faster than the images can be displayed
380 | // //stopWatch.Reset();
381 |
382 | // GetCameraSettingData();
383 |
384 | // IsLink = true;
385 |
386 |
387 | // }
388 | // catch (Exception ex)
389 | // {
390 | // Util.WriteLog(this.GetType(), ex);
391 | // Util.Notify("相机打开出现异常:" + ex.Message);
392 | // throw ex;
393 | // }
394 | // return true;
395 | // }
396 |
397 | // public override void Output()
398 | // {
399 | // throw new NotImplementedException();
400 | // }
401 |
402 | // public override void SetExtTrigger()
403 | // {
404 | // throw new NotImplementedException();
405 | // }
406 |
407 | // protected override void GetCameraSettingData()
408 | // {
409 | // try
410 | // {
411 | // //long max, min, cur;
412 |
413 | // gainCur = IGXFeatureControl.GetFloatFeature("Gain").GetValue();
414 | // gainMin = IGXFeatureControl.GetFloatFeature("Gain").GetMin();
415 | // gainMax = IGXFeatureControl.GetFloatFeature("Gain").GetMax();
416 |
417 | // gainUnit = "db";
418 |
419 |
420 | // shuterCur = (long)IGXFeatureControl.GetFloatFeature("ExposureTime").GetValue();
421 | // shuterMin = (long)IGXFeatureControl.GetFloatFeature("ExposureTime").GetMin();
422 | // shuterMax = (long)IGXFeatureControl.GetFloatFeature("ExposureTime").GetMax();
423 | // shuterUnit = IGXFeatureControl.GetFloatFeature("ExposureTime").GetUnit();
424 |
425 |
426 | // //float fTriggerDelay = 0;
427 | // //m_pOperator.GetFloatValue("TriggerDelay", ref fTriggerDelay);
428 | // //triggerDelayAbsMin = 0;
429 | // //triggerDelayAbsMax = 1000;
430 | // //triggerDelayAbs = fTriggerDelay;
431 |
432 | // lineDebouncerTimeAbsMin = 0;
433 | // lineDebouncerTimeAbsMax = 5000;
434 | // lineDebouncerTimeAbs = 0;
435 |
436 | // }
437 | // catch (Exception ex)
438 | // {
439 | // Util.WriteLog(this.GetType(), ex);
440 | // Util.Notify("相机设置信息获取异常");
441 | // }
442 | // }
443 | // }
444 | }
445 |
--------------------------------------------------------------------------------
/Yoga.Camera/DirectShowCamera.cs:
--------------------------------------------------------------------------------
1 | using HalconDotNet;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading;
7 | using System.Threading.Tasks;
8 | using Yoga.Common;
9 |
10 | namespace Yoga.Camera
11 | {
12 | public class DirectShowCamera : CameraBase
13 | {
14 | HFramegrabber framegrabber;
15 | AutoResetEvent threadRunSignal = new AutoResetEvent(false);
16 |
17 | //private bool ignoreImage = false;
18 | Thread runThread ;
19 | public DirectShowCamera(HFramegrabber framegrabber, int index)
20 | {
21 | this.framegrabber = framegrabber;
22 | this.cameraIndex = index;
23 | }
24 | ///
25 | /// 图像采集线程对应方法
26 | ///
27 | public void Run()
28 | {
29 | while (IsLink)
30 | {
31 |
32 | threadRunSignal.WaitOne();
33 |
34 | Util.Notify("开始连续采集图像");
35 | if (IsLink)
36 | {
37 | while (IsContinuousShot)
38 | {
39 | GetImage();
40 | if (hPylonImage!=null&& hPylonImage.IsInitialized())
41 | {
42 | TrigerImageEvent();
43 | }
44 | }
45 |
46 | }
47 | }
48 | }
49 | int reTryCount = 0;
50 | public override HImage GrabImage(int delayMs)
51 | {
52 | if (framegrabber == null || framegrabber.IsInitialized() == false)
53 | {
54 | Util.Notify("图像采集设备打开异常");
55 | return null;
56 | }
57 | GetImage();
58 | if (hPylonImage==null|| hPylonImage.IsInitialized()==false)
59 | {
60 | reTryCount++;
61 | if (reTryCount < 3)
62 | {
63 | GrabImage(1);
64 | }
65 | }
66 | else
67 | {
68 | //帧率统计增加
69 | fps.IncreaseFrameNum();
70 | }
71 | reTryCount = 0;
72 | return hPylonImage;
73 | }
74 |
75 | private void GetImage()
76 | {
77 | try
78 | {
79 | hPylonImage = null;
80 | HOperatorSet.CountSeconds(out startTime);
81 | hPylonImage = framegrabber.GrabImage();
82 |
83 | }
84 | catch(Exception ex)
85 | {
86 | Util.WriteLog(this.GetType(), ex);
87 | Util.Notify("图像采集发生异常,usb设备图像采集失败" );
88 | }
89 | }
90 |
91 | public override double GainCur
92 | {
93 | get
94 | {
95 | return -1;
96 | }
97 |
98 | set
99 | {
100 | ;
101 | }
102 | }
103 |
104 |
105 |
106 | public override long ShuterCur
107 | {
108 | get
109 | {
110 | return -1;
111 | }
112 |
113 | set
114 | {
115 | ;
116 | }
117 | }
118 |
119 | public override void Close()
120 | {
121 | try
122 | {
123 | IsLink = false;
124 | threadRunSignal.Set();
125 | // Reset the stopwatch.
126 | //stopWatch.Reset();
127 | if (framegrabber != null)
128 | {
129 | framegrabber.Dispose();
130 | framegrabber = null;
131 | }
132 | }
133 | catch (Exception ex)
134 | {
135 | Util.WriteLog(this.GetType(), ex);
136 | Util.Notify("相机关闭异常");
137 | }
138 | }
139 |
140 | public override void ContinuousShot()
141 | {
142 | if (framegrabber == null || framegrabber.IsInitialized() == false)
143 | {
144 | return;
145 | }
146 | try
147 | {
148 | Command = Command.Video;
149 |
150 | IsContinuousShot = true;
151 | threadRunSignal.Set();
152 | }
153 | catch (Exception ex)
154 | {
155 | Util.WriteLog(this.GetType(), ex);
156 | Util.Notify("相机连续采集开始异常");
157 | }
158 | }
159 |
160 | public override void ContinuousShotStop()
161 | {
162 | try
163 | {
164 | // Set an enum parameter.
165 | if (framegrabber == null || framegrabber.IsInitialized() == false)
166 | {
167 | return;
168 | }
169 |
170 | IsContinuousShot = false;
171 | IsExtTrigger = false;
172 | //Task.Run(() =>
173 | //{
174 | // ignoreImage = true;
175 | // Thread.Sleep(1000);
176 | // ignoreImage = false;
177 | //});
178 | }
179 | catch (Exception ex)
180 | {
181 | Util.WriteLog(this.GetType(), ex);
182 | Util.Notify("相机连续采集停止异常");
183 | }
184 | }
185 |
186 | public override void OneShot(Command command)
187 | {
188 | try
189 | {
190 | if (IsContinuousShot || IsExtTrigger)
191 | {
192 | ContinuousShotStop();
193 | }
194 | Command = command;
195 | // Execute the software trigger. Wait up to 1000 ms until the camera is ready for trigger.
196 | threadRunSignal.Set();
197 | }
198 | catch
199 | {
200 | IsLink = false;
201 | Util.Notify("相机软触发异常");
202 | }
203 | }
204 |
205 | public override bool Open()
206 | {
207 | try
208 | {
209 |
210 |
211 | //ContinuousShotStop();//设置为软触发模式
212 |
213 |
214 | // Reset the stopwatch used to reduce the amount of displayed images. The camera may acquire images faster than the images can be displayed
215 | //stopWatch.Reset();
216 |
217 | GetCameraSettingData();
218 | //usb相机第一次采集图像缓慢,采集一张图像不使用来提速
219 | GetImage();
220 |
221 | IsLink = true;
222 | runThread = new Thread(new ThreadStart(Run));
223 | runThread.IsBackground = true;
224 | runThread.Start();
225 | //}
226 | //else
227 | //{
228 | // Util.Notify("无相机连接");
229 | // return false;
230 | //}
231 | }
232 | catch (Exception ex)
233 | {
234 | Util.WriteLog(this.GetType(), ex);
235 | Util.Notify("相机打开出现异常");
236 |
237 | throw ex;
238 | }
239 | return true;
240 |
241 | }
242 |
243 | public override void Output()
244 | {
245 |
246 | }
247 |
248 | public override void SetExtTrigger()
249 | {
250 |
251 | }
252 |
253 | protected override void GetCameraSettingData()
254 | {
255 | try
256 | {
257 | //long max, min, cur;
258 | //gainMin = g_camera.Parameters[PLCamera.GainRaw].GetMinimum();
259 | //gainMax = g_camera.Parameters[PLCamera.GainRaw].GetMaximum();
260 | //gainCur = g_camera.Parameters[PLCamera.GainRaw].GetValue();
261 | gainUnit = "";
262 |
263 | shuterUnit = "us";
264 |
265 | //shuterMin = g_camera.Parameters[PLCamera.ExposureTimeRaw].GetMinimum();
266 | //shuterMax = g_camera.Parameters[PLCamera.ExposureTimeRaw].GetMaximum();
267 | //shuterCur = g_camera.Parameters[PLCamera.ExposureTimeRaw].GetValue();
268 |
269 | //triggerDelayAbsMin = g_camera.Parameters[PLCamera.TriggerDelayAbs].GetMinimum();
270 | //triggerDelayAbsMax = g_camera.Parameters[PLCamera.TriggerDelayAbs].GetMaximum();
271 | triggerDelayAbs = -1;
272 |
273 | lineDebouncerTimeAbsMin = 0;
274 | lineDebouncerTimeAbsMax = 5000;
275 | lineDebouncerTimeAbs =-1;
276 |
277 | }
278 | catch (Exception ex)
279 | {
280 | Util.WriteLog(this.GetType(), ex);
281 | Util.Notify("相机设置信息获取异常");
282 | }
283 | }
284 | }
285 | }
286 |
--------------------------------------------------------------------------------
/Yoga.Camera/EnumInfo.cs:
--------------------------------------------------------------------------------
1 | namespace Yoga.Camera
2 | {
3 | ///
4 | /// 命令枚举
5 | ///
6 | public enum Command
7 | {
8 | None,
9 | ExtTrigger,
10 | Video,
11 | Grab
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/Yoga.Camera/Fps.cs:
--------------------------------------------------------------------------------
1 | using HalconDotNet;
2 | using System;
3 |
4 | namespace Yoga.Camera
5 | {
6 | public class Fps
7 | {
8 | ulong frameCount = 0; // 从上次计算完毕开始累积的帧数
9 | HTuple beginTime = 0.0; // 第一帧之前的一帧的时间(初始为0)
10 | HTuple endTime = 0.0; //最后一帧的时间
11 |
12 |
13 | double fps = 0.0; //通过帧数与时间间隔之比得出的帧率(帧/秒)
14 | double currentFps = 0.0; //当前的帧率,可能是预测得到的(帧/秒)
15 | ulong totalFrameCount = 0; //累积的帧数
16 | //TimeWatch objTime = new TimeWatch(); // 计时器
17 | object m_objLock = new object();
18 |
19 | ///
20 | /// 构造函数
21 | ///
22 | public Fps()
23 | {
24 | //重置所有参数
25 | Reset();
26 | }
27 |
28 |
29 | ///
30 | /// 获取最近一次的帧率
31 | ///
32 | /// 当前帧图像
33 | public double GetFps()
34 | {
35 | lock (m_objLock)
36 | {
37 | //返回当前的帧率
38 | return currentFps;
39 | }
40 | }
41 |
42 | ///
43 | /// 获取累积的总帧数
44 | ///
45 | /// 当前帧图像
46 | public ulong GetTotalFrameCount()
47 | {
48 | lock (m_objLock)
49 | {
50 | return totalFrameCount;
51 | }
52 | }
53 |
54 | ///
55 | /// 增加帧数
56 | ///
57 | public void IncreaseFrameNum()
58 | {
59 | lock (m_objLock)
60 | {
61 | //累积帧数
62 | totalFrameCount++;
63 |
64 | //增加帧数
65 | frameCount++;
66 |
67 | //更新时间间隔
68 | HOperatorSet.CountSeconds(out endTime);
69 | //endTime = objTime.ElapsedTime();
70 | }
71 | }
72 |
73 | ///
74 | /// 更新帧率
75 | /// 如果该函数被调用的频率超过了帧频率,则帧率会降为零
76 | ///
77 | public void UpdateFps()
78 | {
79 | lock (m_objLock)
80 | {
81 | //计算时间间隔
82 | double dInterval =( endTime - beginTime)*1000.0;
83 |
84 | //时间间隔大于零(有新帧)
85 | if (dInterval > 0)
86 | {
87 | fps = 1000.0 * frameCount / dInterval;
88 | frameCount = 0; //累积帧数清零
89 | beginTime = endTime; //更新起始时间
90 |
91 | currentFps = fps;
92 | }
93 | else if (dInterval == 0) //时间间隔等于零(无新帧)
94 | {
95 | //如果上次的帧率非零,则调整帧率
96 | if (currentFps != 0)
97 | {
98 |
99 |
100 | //从上一帧到现在的经历的时间(毫秒)
101 | HTuple nowTime;
102 | HOperatorSet.CountSeconds(out nowTime);
103 |
104 | //从上一帧到现在的经历的时间(毫秒)
105 | double dCurrentInterval = (nowTime - beginTime)*1000.0;
106 |
107 | //根据当前帧率计算更新帧率的时间阈值
108 | double dPeriod = 1000.0 / currentFps; //上次的帧周期(毫秒)
109 | const double RATIO = 1.5; //超过帧周期的多少倍,帧率才更新
110 | double dThresh = RATIO * dPeriod; //多长时间没有来帧,帧率就更新
111 |
112 | //如果超过2秒没有来帧,则帧率降为零。
113 | const double ZERO_FPS_INTERVAL = 2000;
114 | if (dCurrentInterval > ZERO_FPS_INTERVAL)
115 | {
116 | currentFps = 0;
117 | }
118 | //如果在2秒之内已经超过1.5倍的帧周期没有来帧,则降低帧率
119 | else if (dCurrentInterval > dThresh)
120 | {
121 | currentFps = fps / (dCurrentInterval / (1000.0 / fps));
122 | }
123 | else { }
124 | }
125 | else { }
126 | }
127 | else { }
128 | }
129 |
130 | }
131 |
132 | ///
133 | /// 将计时器恢复为初始状态
134 | ///
135 | public void Reset()
136 | {
137 | frameCount = 0;
138 | beginTime = 0.0;
139 | endTime = 0.0;
140 | totalFrameCount = 0;
141 | fps = 0.0;
142 | currentFps = 0.0;
143 | HOperatorSet.CountSeconds(out beginTime);
144 | //objTime.Start(); //重启计时器
145 | }
146 | }
147 | }
148 |
--------------------------------------------------------------------------------
/Yoga.Camera/GigeCamera.cs:
--------------------------------------------------------------------------------
1 | using HalconDotNet;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Diagnostics;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading;
8 | using System.Threading.Tasks;
9 | using Yoga.Common;
10 |
11 | namespace Yoga.Camera
12 | {
13 | public class GigeCamera : CameraBase
14 | {
15 | private Stopwatch stopWatch = new Stopwatch();
16 | HFramegrabber framegrabber;
17 | Thread runThread;
18 | AutoResetEvent threadRunSignal = new AutoResetEvent(false);
19 | public override double GainCur
20 | {
21 | get
22 | {
23 | throw new NotImplementedException();
24 | }
25 |
26 | set
27 | {
28 | ; ;
29 | }
30 | }
31 |
32 | public override long ShuterCur
33 | {
34 | get
35 | {
36 | throw new NotImplementedException();
37 | }
38 |
39 | set
40 | {
41 | ;
42 | }
43 | }
44 | public GigeCamera(HFramegrabber framegrabber, int index)
45 | {
46 | this.framegrabber = framegrabber;
47 | this.cameraIndex = index;
48 | }
49 | public override void Close()
50 | {
51 | try
52 | {
53 | IsLink = false;
54 |
55 |
56 | // Reset the stopwatch.
57 | stopWatch.Reset();
58 | framegrabber.Dispose();
59 |
60 | }
61 | catch (Exception ex)
62 | {
63 | Util.WriteLog(this.GetType(), ex);
64 | Util.Notify("相机关闭异常");
65 | }
66 | }
67 |
68 | public override void ContinuousShot()
69 | {
70 | if (framegrabber == null || framegrabber.IsInitialized() == false)
71 | {
72 | return;
73 | }
74 | try
75 | {
76 | Command = Command.Video;
77 |
78 | IsContinuousShot = true;
79 | threadRunSignal.Set();
80 | }
81 | catch (Exception ex)
82 | {
83 | Util.WriteLog(this.GetType(), ex);
84 | Util.Notify("相机连续采集开始异常");
85 | }
86 | }
87 | public override HImage GrabImage(int delayMs)
88 | {
89 | if (framegrabber == null || framegrabber.IsInitialized() == false)
90 | {
91 | Util.Notify("图像采集设备打开异常");
92 | return null;
93 | }
94 | GetImage();
95 | return hPylonImage;
96 | }
97 | private void GetImage()
98 | {
99 | try
100 | {
101 | hPylonImage = null;
102 | HOperatorSet.CountSeconds(out startTime);
103 | hPylonImage = framegrabber.GrabImage();
104 |
105 | }
106 | catch (Exception ex)
107 | {
108 | Util.WriteLog(this.GetType(), ex);
109 | Util.Notify(string.Format("图像采集发生异常,相机{0}采集失败", cameraIndex));
110 | }
111 | }
112 |
113 | public override void ContinuousShotStop()
114 | {
115 | try
116 | {
117 | // Set an enum parameter.
118 | if (framegrabber == null)
119 | {
120 | return;
121 | }
122 |
123 | IsContinuousShot = false;
124 | IsExtTrigger = false;
125 | }
126 | catch (Exception ex)
127 | {
128 | Util.WriteLog(this.GetType(), ex);
129 | Util.Notify("相机连续采集停止异常");
130 | }
131 | }
132 | ///
133 | /// 图像采集线程对应方法
134 | ///
135 | public void Run()
136 | {
137 | while (IsLink)
138 | {
139 |
140 | threadRunSignal.WaitOne();
141 |
142 | Util.Notify("开始连续采集图像");
143 | if (IsLink)
144 | {
145 | while (IsContinuousShot)
146 | {
147 | GetImage();
148 | if (hPylonImage != null && hPylonImage.IsInitialized())
149 | {
150 | TrigerImageEvent();
151 | }
152 | }
153 |
154 | }
155 | }
156 | }
157 | public override void OneShot(Command command)
158 | {
159 | try
160 | {
161 | if (IsContinuousShot || IsExtTrigger)
162 | {
163 | ContinuousShotStop();
164 | }
165 | Command = command;
166 | // Execute the software trigger. Wait up to 1000 ms until the camera is ready for trigger.
167 | threadRunSignal.Set();
168 | }
169 | catch
170 | {
171 | IsLink = false;
172 | Util.Notify("相机软触发异常");
173 | }
174 | }
175 |
176 | public override bool Open()
177 | {
178 | try
179 | {
180 |
181 |
182 | //ContinuousShotStop();//设置为软触发模式
183 |
184 |
185 | // Reset the stopwatch used to reduce the amount of displayed images. The camera may acquire images faster than the images can be displayed
186 | //stopWatch.Reset();
187 |
188 | GetCameraSettingData();
189 | //usb相机第一次采集图像缓慢,采集一张图像不使用来提速
190 | GetImage();
191 |
192 | IsLink = true;
193 | runThread = new Thread(new ThreadStart(Run));
194 | runThread.IsBackground = true;
195 | runThread.Start();
196 | //}
197 | //else
198 | //{
199 | // Util.Notify("无相机连接");
200 | // return false;
201 | //}
202 | }
203 | catch (Exception ex)
204 | {
205 | Util.WriteLog(this.GetType(), ex);
206 | Util.Notify("相机打开出现异常");
207 |
208 | throw ex;
209 | }
210 | return true;
211 | }
212 |
213 | public override void Output()
214 | {
215 | throw new NotImplementedException();
216 | }
217 |
218 | public override void SetExtTrigger()
219 | {
220 | throw new NotImplementedException();
221 | }
222 |
223 | protected override void GetCameraSettingData()
224 | {
225 | try
226 | {
227 | //long max, min, cur;
228 |
229 | HTuple fGainRange = 0;
230 | fGainRange=framegrabber.GetFramegrabberParam("Gain_range");
231 | gainMin = fGainRange[0];
232 | gainMax = fGainRange[1];
233 | gainCur = fGainRange[3];
234 | gainUnit = "db";
235 |
236 | shuterUnit = "us";
237 |
238 | HTuple exposureRange = framegrabber.GetFramegrabberParam("ExposureTimeAbs_range");
239 |
240 | shuterMin =(long) exposureRange[0].D;
241 | shuterMax = (long)exposureRange[1].D;
242 | shuterCur = (long)exposureRange[3].D;
243 |
244 |
245 | HTuple fTriggerDelayRange= framegrabber.GetFramegrabberParam("TriggerDelayAbs_range");
246 | triggerDelayAbsMin = fTriggerDelayRange[0];
247 | triggerDelayAbsMax = fTriggerDelayRange[1];
248 | triggerDelayAbs = fTriggerDelayRange[3];
249 |
250 | lineDebouncerTimeAbsMin = 0;
251 | lineDebouncerTimeAbsMax = 5000;
252 | lineDebouncerTimeAbs = 0;
253 |
254 | }
255 | catch (Exception ex)
256 | {
257 | Util.WriteLog(this.GetType(), ex);
258 | Util.Notify("相机设置信息获取异常");
259 | }
260 | }
261 | }
262 | }
263 |
--------------------------------------------------------------------------------
/Yoga.Camera/HikvisionCamera.cs:
--------------------------------------------------------------------------------
1 | using DeviceSource;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 | using Yoga.Common;
8 | using System.Diagnostics;
9 | using MvCamCtrl.NET;
10 | using System.Threading;
11 | using HalconDotNet;
12 |
13 | namespace Yoga.Camera
14 | {
15 | public class HikvisionCamera : CameraBase
16 | {
17 | #region 字段
18 | private bool ignoreImage = false;
19 |
20 | CameraOperator m_pOperator;
21 | MyCamera.cbOutputdelegate ImageCallback;
22 | private object lockobj1 = new object();
23 | private object lockobj2 = new object();
24 | #endregion
25 | #region 属性设置
26 | public override double GainCur
27 | {
28 | get
29 | {
30 | return gainCur;
31 | }
32 |
33 | set
34 | {
35 | try
36 | {
37 | double gainValue = value;
38 |
39 | //判断输入值是否在增益值的范围内
40 | //若输入的值大于最大值则将增益值设置成最大值
41 | if (gainValue > GainMax)
42 | {
43 | gainValue = GainMax;
44 | }
45 |
46 | //若输入的值小于最小值则将增益的值设置成最小值
47 | if (gainValue < GainMin)
48 | {
49 | gainValue = GainMin;
50 | }
51 |
52 | m_pOperator.SetEnumValue("GainAuto", 0);
53 | int nRet;
54 | nRet = m_pOperator.SetFloatValue("Gain", (float)gainValue);
55 |
56 | if (nRet != CameraOperator.CO_OK)
57 | {
58 | throw new Exception("设置曝光时间失败!");
59 | }
60 | gainCur = gainValue;
61 | }
62 | catch (Exception ex)
63 | {
64 | Util.WriteLog(this.GetType(), ex);
65 | Util.Notify("相机增益设置异常");
66 | }
67 | }
68 | }
69 |
70 | public override long ShuterCur
71 | {
72 | get
73 | {
74 | return shuterCur;
75 | }
76 |
77 | set
78 | {
79 | try
80 | {
81 | long shutterValue = value;
82 |
83 | //获取当前相机的曝光值、最小值、最大值和单位
84 |
85 | //判断输入值是否在曝光时间的范围内
86 | //若大于最大值则将曝光值设为最大值
87 |
88 | if (shutterValue > ShuterMax)
89 | {
90 | shutterValue = ShuterMax;
91 | }
92 | //若小于最小值将曝光值设为最小值
93 | else if (shutterValue < ShuterMin)
94 | {
95 | shutterValue = ShuterMin;
96 | }
97 |
98 | m_pOperator.SetEnumValue("ExposureAuto", 0);
99 | int nRet;
100 | nRet = m_pOperator.SetFloatValue("ExposureTime", shutterValue);
101 | if (nRet != CameraOperator.CO_OK)
102 | {
103 | throw new Exception("设置曝光时间失败!");
104 | }
105 | shuterCur = shutterValue;
106 | }
107 | catch (Exception ex)
108 | {
109 | Util.WriteLog(this.GetType(), ex);
110 | Util.Notify("相机曝光设置异常");
111 | }
112 | }
113 | }
114 |
115 |
116 | #endregion
117 | public HikvisionCamera(CameraOperator m_pOperator, int index)
118 | {
119 | this.m_pOperator = m_pOperator;
120 | this.cameraIndex = index;
121 | }
122 | public override void Close()
123 | {
124 | try
125 | {
126 | IsLink = false;
127 | if (m_pOperator != null)
128 | {
129 | //停止采集
130 | m_pOperator.StopGrabbing();
131 | //关闭设备
132 | m_pOperator.Close();
133 |
134 |
135 |
136 | m_pOperator = null;
137 | }
138 | }
139 | catch (Exception ex)
140 | {
141 | Util.WriteLog(this.GetType(), ex);
142 | Util.Notify("相机关闭异常");
143 | }
144 | }
145 |
146 | public override void ContinuousShot()
147 | {
148 | if (m_pOperator == null || IsLink == false)
149 | {
150 | return;
151 | }
152 | try
153 | {
154 | Command = Command.Video;
155 | m_pOperator.SetEnumValue("TriggerMode", 0);
156 | IsContinuousShot = true;
157 |
158 |
159 |
160 |
161 | }
162 | catch (Exception ex)
163 | {
164 | Util.WriteLog(this.GetType(), ex);
165 | Util.Notify("相机连续采集开始异常");
166 | }
167 | }
168 |
169 | public override void ContinuousShotStop()
170 | {
171 | try
172 | {
173 | // Set an enum parameter.
174 | if (m_pOperator == null)
175 | {
176 | return;
177 | }
178 | // m_pOperator.SetEnumValue("AcquisitionMode", 2);
179 | m_pOperator.SetEnumValue("TriggerMode", 1);
180 | //触发源选择:0 - Line0;
181 | // 1 - Line1;
182 | // 2 - Line2;
183 | // 3 - Line3;
184 | // 4 - Counter;
185 | // 7 - Software;
186 | m_pOperator.SetEnumValue("TriggerSource", 7);
187 | IsContinuousShot = false;
188 | IsExtTrigger = false;
189 | ignoreImage = true;
190 | Task.Run(() =>
191 | {
192 | Thread.Sleep(1000);
193 | ignoreImage = false;
194 | });
195 | }
196 | catch (Exception ex)
197 | {
198 | Util.WriteLog(this.GetType(), ex);
199 | Util.Notify("相机连续采集停止异常");
200 | }
201 | }
202 | public override void OneShot(Command command)
203 | {
204 | try
205 | {
206 | if (IsContinuousShot || IsExtTrigger)
207 | {
208 | ContinuousShotStop();
209 | }
210 | Command =command;
211 | int nRet;
212 |
213 | //触发命令
214 | nRet = m_pOperator.CommandExecute("TriggerSoftware");
215 | if (CameraOperator.CO_OK != nRet)
216 | {
217 | Util.Notify("相机软触发异常");
218 | }
219 | }
220 | catch
221 | {
222 | IsLink = false;
223 | Util.Notify("相机软触发异常");
224 | }
225 | }
226 | public override bool Open()
227 | {
228 | try
229 | {
230 | int nRet;
231 |
232 |
233 | //Util.Notify("相机开始打开");
234 |
235 | uint pixelFormat = 0;
236 | nRet = m_pOperator.GetEnumValue("PixelFormat", ref pixelFormat);
237 | if (MyCamera.MV_OK != nRet)
238 | {
239 | throw new Exception("图像格式获取错误");
240 | }
241 | MyCamera.MvGvspPixelType imgType = (MyCamera.MvGvspPixelType)pixelFormat;
242 | if (imgType == MyCamera.MvGvspPixelType.PixelType_Gvsp_YUV411_Packed ||
243 | imgType == MyCamera.MvGvspPixelType.PixelType_Gvsp_YUV422_Packed ||
244 | imgType == MyCamera.MvGvspPixelType.PixelType_Gvsp_YUV422_YUYV_Packed ||
245 | imgType == MyCamera.MvGvspPixelType.PixelType_Gvsp_YUV444_Packed)
246 | {
247 | int result = m_pOperator.SetEnumValue("PixelFormat", (uint)MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed);
248 | if (MyCamera.MV_OK != result)
249 | {
250 | throw new Exception("图像格式设置错误");
251 | }
252 | }
253 |
254 | ////设置采集连续模式
255 | //nRet = m_pOperator.SetEnumValue("AcquisitionMode", 2);
256 | //if (MyCamera.MV_OK != nRet)
257 | //{
258 | // throw new Exception("采集模式设置失败");
259 |
260 | //}
261 |
262 | //nRet = m_pOperator.SetEnumValue("TriggerMode", 0);
263 | //if (MyCamera.MV_OK != nRet)
264 | //{
265 | // throw new Exception("触发模式设置失败");
266 |
267 | //}
268 | ImageCallback = new MyCamera.cbOutputdelegate(SaveImage);
269 | nRet = m_pOperator.RegisterImageCallBack(ImageCallback, IntPtr.Zero);
270 | if (MyCamera.MV_OK != nRet)
271 | {
272 | throw new Exception("回调函数注册失败");
273 |
274 | }
275 |
276 |
277 | ContinuousShotStop();
278 | //开始采集
279 | nRet = m_pOperator.StartGrabbing();
280 | if (MyCamera.MV_OK != nRet)
281 | {
282 | throw new Exception("开始采集失败");
283 |
284 | }
285 |
286 |
287 |
288 | // Reset the stopwatch used to reduce the amount of displayed images. The camera may acquire images faster than the images can be displayed
289 | //stopWatch.Reset();
290 |
291 | GetCameraSettingData();
292 |
293 | IsLink = true;
294 |
295 | //Thread.Sleep(500);
296 | //nRet = m_pOperator.SetEnumValue("TriggerMode", 1);
297 | //if (MyCamera.MV_OK != nRet)
298 | //{
299 | // throw new Exception("触发模式设置失败");
300 |
301 | //}
302 | //Thread.Sleep(500);
303 | //nRet = m_pOperator.SetEnumValue("TriggerSource", 0);
304 |
305 | //if (MyCamera.MV_OK != nRet)
306 | //{
307 | // throw new Exception("触发源设置失败");
308 |
309 | //}
310 |
311 | //}
312 | //else
313 | //{
314 | // Util.Notify("无相机连接");
315 | // return false;
316 | //}
317 | }
318 | catch (Exception ex)
319 | {
320 | Util.WriteLog(this.GetType(), ex);
321 | Util.Notify("相机打开出现异常:" + ex.Message);
322 | throw ex;
323 | }
324 | return true;
325 | }
326 |
327 | private void SaveImage(IntPtr pData, ref MyCamera.MV_FRAME_OUT_INFO pFrameInfo, IntPtr pUser)
328 | {
329 | try
330 | {
331 | if (ignoreImage)
332 | {
333 | return;
334 | }
335 |
336 | //HTuple startTime;
337 | HOperatorSet.CountSeconds(out startTime);
338 |
339 | // Reduce the number of displayed images to a reasonable amount if the camera is acquiring images very fast.
340 | //if (!stopWatch.IsRunning || stopWatch.ElapsedMilliseconds > 10)
341 | {
342 | //stopWatch.Restart();
343 |
344 | //if (hPylonImage != null && hPylonImage.IsInitialized())
345 | //{
346 | // hPylonImage.Dispose();
347 | //}
348 | hPylonImage = new HImage();
349 | if (pFrameInfo.enPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)
350 | {
351 | hPylonImage.GenImage1("byte", pFrameInfo.nWidth, pFrameInfo.nHeight, pData);
352 | }
353 | else if (pFrameInfo.enPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed)
354 | {
355 | hPylonImage.GenImageInterleaved(pData,
356 | "rgb",
357 | pFrameInfo.nWidth, pFrameInfo.nHeight,
358 | -1, "byte",
359 | pFrameInfo.nWidth, pFrameInfo.nHeight,
360 | 0, 0, -1, 0);
361 | }
362 | else
363 | {
364 | Util.Notify(string.Format("相机{0}编码格式不正确,当前格式{1}", cameraIndex, pFrameInfo.enPixelType));
365 | }
366 | TrigerImageEvent();
367 |
368 | }
369 |
370 | }
371 | catch (System.ArgumentException ex)
372 | {
373 | Util.WriteLog(this.GetType(), ex);
374 | Util.Notify(string.Format("相机{0}图像数据包丢失", cameraIndex));
375 | }
376 | catch (Exception ex)
377 | {
378 | Util.WriteLog(this.GetType(), ex);
379 | Util.Notify(string.Format("相机{0}图像数据返回出现异常", cameraIndex));
380 | }
381 | }
382 |
383 | protected override void GetCameraSettingData()
384 | {
385 | try
386 | {
387 | //long max, min, cur;
388 |
389 | float fGain = 0;
390 | m_pOperator.GetFloatValue("Gain", ref fGain);
391 |
392 | gainMin = 0;
393 | gainMax = 17;
394 | gainCur = fGain;
395 | gainUnit = "db";
396 |
397 | shuterUnit = "us";
398 |
399 | float fExposure = 0;
400 | m_pOperator.GetFloatValue("ExposureTime", ref fExposure);
401 | shuterMin = 20;
402 | shuterMax = 1000000;
403 | shuterCur = (long)fExposure;
404 |
405 |
406 | float fTriggerDelay = 0;
407 | m_pOperator.GetFloatValue("TriggerDelay", ref fTriggerDelay);
408 | triggerDelayAbsMin = 0;
409 | triggerDelayAbsMax = 1000;
410 | triggerDelayAbs = fTriggerDelay;
411 |
412 | lineDebouncerTimeAbsMin = 0;
413 | lineDebouncerTimeAbsMax = 5000;
414 | lineDebouncerTimeAbs = 0;
415 |
416 | }
417 | catch (Exception ex)
418 | {
419 | Util.WriteLog(this.GetType(), ex);
420 | Util.Notify("相机设置信息获取异常");
421 | }
422 | }
423 |
424 | public override void SetExtTrigger()
425 | {
426 | try
427 | {
428 | //int ret = 1;
429 | // ret= m_pOperator.SetEnumValue("TriggerSelector", 0);//0 :AcquisitionStart
430 | //// if (ret==0)
431 | // {
432 | // ret = m_pOperator.SetEnumValue("TriggerSelector", 3);//3:FrameStart
433 | // if (ret==0)
434 | // {
435 | // m_pOperator.SetEnumValue("TriggerSelector", 0);//0 :AcquisitionStart
436 | // m_pOperator.SetEnumValue("TriggerMode", 0);//0:Off
437 |
438 | // m_pOperator.SetEnumValue("TriggerSelector", 3);//3:FrameStart
439 | // m_pOperator.SetEnumValue("TriggerMode", 1);//1:On
440 |
441 | // //触发源选择:0 - Line0;
442 | // // 1 - Line1;
443 | // // 2 - Line2;
444 | // // 3 - Line3;
445 | // // 4 - Counter;
446 | // // 7 - Software;
447 | // m_pOperator.SetEnumValue("TriggerSource", 1);
448 |
449 | // }
450 | // else
451 | // {
452 | // // m_pOperator.SetEnumValue("TriggerSelector", 0);//0 :AcquisitionStart
453 |
454 | // m_pOperator.SetEnumValue("TriggerMode", 1);//1:On
455 | // m_pOperator.SetEnumValue("TriggerSource", 1);
456 | // // m_pOperator.StartGrabbing();
457 |
458 |
459 | // }
460 | // }
461 | m_pOperator.SetEnumValue("TriggerMode", 1);
462 | //触发源选择:0 - Line0;
463 | // 1 - Line1;
464 | // 2 - Line2;
465 | // 3 - Line3;
466 | // 4 - Counter;
467 | // 7 - Software;
468 | m_pOperator.SetEnumValue("TriggerSource", 0);
469 |
470 | //m_pOperator.SetEnumValue("TriggerMode", 1);
471 | //m_pOperator.SetEnumValue("TriggerSource", 1);
472 |
473 |
474 | m_pOperator.SetEnumValue("ExposureAuto", 0);
475 |
476 | m_pOperator.SetFloatValue("TriggerDelay", (float)triggerDelayAbs);
477 | m_pOperator.SetIntValue("LineDebouncerTime", (uint)LineDebouncerTimeAbs);
478 | Command = Command.ExtTrigger;
479 | IsExtTrigger = true;
480 | }
481 | catch (Exception ex)
482 | {
483 | Util.WriteLog(this.GetType(), ex);
484 | Util.Notify("相机外触发设置异常");
485 | }
486 | }
487 | ///
488 | /// 信号输出,已经在task中运行
489 | ///
490 | public override void Output()
491 | {
492 | if (IOSerial.Instance.Rs232Param.Use == false)
493 | {
494 | Task.Run(() =>
495 | {
496 | lock (lockobj1)
497 | {
498 | int nRet;
499 | nRet = m_pOperator.SetEnumValue("LineSelector", 1);
500 | if (nRet != CameraOperator.CO_OK)
501 | {
502 | Util.Notify("LineSelector异常");
503 | }
504 | nRet = m_pOperator.SetEnumValue("LineMode", 8);
505 | if (nRet != CameraOperator.CO_OK)
506 | {
507 | Util.Notify("LineMode异常");
508 | }
509 | nRet = m_pOperator.SetBoolValue("LineInverter", true);
510 | if (nRet != CameraOperator.CO_OK)
511 | {
512 | Util.Notify("LineInverter异常");
513 | }
514 | Thread.Sleep((int)outLineTime);
515 | nRet = m_pOperator.SetBoolValue("LineInverter", false);
516 | if (nRet != CameraOperator.CO_OK)
517 | {
518 | Util.Notify("LineInverter异常");
519 | }
520 | // g_camera.Parameters[PLCamera.LineSelector].TrySetValue(PLCamera.LineSelector.Line1);
521 | //m_pOperator.SetEnumValue("LineSelector", 1);
522 | //g_camera.Parameters[PLCamera.LineSource].TrySetValue(PLCamera.LineSource.UserOutput);
523 | //g_camera.Parameters[PLCamera.UserOutputValue].TrySetValue(true);
524 | //Thread.Sleep((int)outLineTime);
525 | //g_camera.Parameters[PLCamera.UserOutputValue].TrySetValue(false);
526 | //g_camera.Parameters[PLCamera.timer].TrySetValue(PLCamera.LineSource.Timer1Active.);
527 | Util.Notify("海康相机报警输出完成");
528 | }
529 | });
530 | }
531 |
532 | else
533 | {
534 | Task.Run(() =>
535 | {
536 | lock (lockobj2)
537 | {
538 | try
539 | {
540 | IOSerial.Instance.WriteDataToSerial("#sw1n");
541 | IOSerial.Instance.WriteDataToSerial("#sw2n");
542 | IOSerial.Instance.WriteDataToSerial("#sw3n");
543 | Thread.Sleep((int)outLineTime);
544 | IOSerial.Instance.WriteDataToSerial("#sw1f");
545 | IOSerial.Instance.WriteDataToSerial("#sw2f");
546 | IOSerial.Instance.WriteDataToSerial("#sw3f");
547 |
548 | Util.Notify("串口io报警输出完成");
549 | }
550 | catch (Exception ex)
551 | {
552 | Util.WriteLog(this.GetType(), ex);
553 | Util.Notify("串口io通信失败");
554 | }
555 | }
556 | });
557 | }
558 | }
559 | }
560 | }
561 |
--------------------------------------------------------------------------------
/Yoga.Camera/ICImagingCamera.cs:
--------------------------------------------------------------------------------
1 | using HalconDotNet;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading;
7 | using System.Threading.Tasks;
8 | using TIS.Imaging;
9 | using TIS.Imaging.VCDHelpers;
10 | using Yoga.Common;
11 |
12 | namespace Yoga.Camera
13 | {
14 | public class ICImagingCamera : CameraBase
15 | {
16 | ICImagingControl camera;
17 | private VCDAbsoluteValueProperty gainAbsoluteValue;
18 | private VCDAbsoluteValueProperty exposureAbsoluteValue;
19 | private VCDAbsoluteValueProperty triggerDelayTime;
20 | private VCDAbsoluteValueProperty triggerDebounceTime;
21 | private VCDSwitchProperty trigEnableSwitch;//触发使能开关
22 | private VCDButtonProperty softtrigger;
23 |
24 | private VCDSimpleProperty VCDProp;
25 | private bool ignoreImage = false;
26 | ///
27 | /// 曝光缩放比例 相机默认为s?转换为us
28 | ///
29 | private const long exposureSocle = 1000000;
30 | public ICImagingCamera(ICImagingControl camera, int index)
31 | {
32 | this.camera = camera;
33 | this.cameraIndex = index;
34 |
35 | VCDProp = VCDSimpleModule.GetSimplePropertyContainer(camera.VCDPropertyItems);
36 | }
37 | public override double GainCur
38 | {
39 | get
40 | {
41 | return gainCur;
42 | }
43 |
44 | set
45 | {
46 | try
47 | {
48 | double gainValue = value;
49 |
50 | //判断输入值是否在增益值的范围内
51 | //若输入的值大于最大值则将增益值设置成最大值
52 | if (gainValue > GainMax)
53 | {
54 | gainValue = GainMax;
55 | }
56 |
57 | //若输入的值小于最小值则将增益的值设置成最小值
58 | if (gainValue < GainMin)
59 | {
60 | gainValue = GainMin;
61 | }
62 | GainAbsoluteValue.Value = gainValue;
63 |
64 | gainCur = gainValue;
65 | }
66 | catch (Exception ex)
67 | {
68 | Util.WriteLog(this.GetType(), ex);
69 | Util.Notify("相机增益设置异常");
70 | }
71 | }
72 | }
73 |
74 | public override long ShuterCur
75 | {
76 | get
77 | {
78 | return shuterCur;
79 | }
80 |
81 | set
82 | {
83 | try
84 | {
85 | long shutterValue = value;
86 |
87 | if (shutterValue > ShuterMax)
88 | {
89 | shutterValue = ShuterMax;
90 | }
91 | //若小于最小值将曝光值设为最小值
92 | else if (shutterValue < ShuterMin)
93 | {
94 | shutterValue = ShuterMin;
95 | }
96 |
97 | double dat = shutterValue / (exposureSocle * 1.0);
98 | ExposureAbsoluteValue.Value = dat;
99 |
100 | shuterCur = shutterValue;
101 | }
102 | catch (Exception ex)
103 | {
104 | Util.WriteLog(this.GetType(), ex);
105 | Util.Notify("相机曝光设置异常");
106 | }
107 | }
108 | }
109 |
110 | #region 相机驱动对应属性
111 | protected VCDAbsoluteValueProperty GainAbsoluteValue
112 | {
113 | get
114 | {
115 |
116 | if (gainAbsoluteValue == null)
117 | {
118 | gainAbsoluteValue = (VCDAbsoluteValueProperty)camera.VCDPropertyItems.FindInterface(
119 | VCDIDs.VCDID_Gain + ":" +
120 | VCDIDs.VCDElement_Value + ":" +
121 | VCDIDs.VCDInterface_AbsoluteValue);
122 | }
123 | return gainAbsoluteValue;
124 | }
125 | }
126 |
127 | protected VCDAbsoluteValueProperty ExposureAbsoluteValue
128 | {
129 | get
130 | {
131 | if (exposureAbsoluteValue == null)
132 | {
133 | exposureAbsoluteValue = (VCDAbsoluteValueProperty)camera.VCDPropertyItems.FindInterface(
134 | VCDIDs.VCDID_Exposure + ":" +
135 | VCDIDs.VCDElement_Value + ":" +
136 | VCDIDs.VCDInterface_AbsoluteValue);
137 | }
138 | return exposureAbsoluteValue;
139 | }
140 | }
141 |
142 | protected VCDAbsoluteValueProperty TriggerDelayTime
143 | {
144 | get
145 | {
146 | if (triggerDelayTime == null)
147 | {
148 | triggerDelayTime = (VCDAbsoluteValueProperty)camera.VCDPropertyItems.FindInterface(
149 | VCDIDs.VCDID_TriggerMode + ":" +
150 | VCDIDs.VCDElement_TriggerDelay + ":" +
151 | VCDIDs.VCDInterface_AbsoluteValue);
152 | }
153 | return triggerDelayTime;
154 | }
155 |
156 | }
157 |
158 | protected VCDAbsoluteValueProperty TriggerDebounceTime
159 | {
160 | get
161 | {
162 | if (triggerDebounceTime == null)
163 | {
164 | triggerDebounceTime = (VCDAbsoluteValueProperty)camera.VCDPropertyItems.FindInterface(
165 | VCDIDs.VCDID_TriggerMode + ":" +
166 | VCDIDs.VCDElement_TriggerDebounceTime + ":" +
167 | VCDIDs.VCDInterface_AbsoluteValue);
168 | }
169 | return triggerDebounceTime;
170 | }
171 |
172 | }
173 |
174 | protected VCDSwitchProperty TrigEnableSwitch
175 | {
176 | get
177 | {
178 | if (trigEnableSwitch == null)
179 | {
180 | trigEnableSwitch = (VCDSwitchProperty)camera.VCDPropertyItems.FindInterface(
181 | VCDIDs.VCDID_TriggerMode + ":" +
182 | VCDIDs.VCDElement_Value + ":" +
183 | VCDIDs.VCDInterface_Switch);
184 |
185 | }
186 | return trigEnableSwitch;
187 | }
188 | }
189 |
190 | protected VCDButtonProperty Softtrigger
191 | {
192 | get
193 | {
194 | if (softtrigger == null)
195 | {
196 | softtrigger = (VCDButtonProperty)camera.VCDPropertyItems.FindInterface(
197 | VCDIDs.VCDID_TriggerMode + ":" +
198 | VCDIDs.VCDElement_SoftwareTrigger + ":" +
199 | VCDIDs.VCDInterface_Button);
200 |
201 | }
202 | return softtrigger;
203 | }
204 |
205 | }
206 | #endregion
207 |
208 | public override void Close()
209 | {
210 | try
211 | {
212 | IsLink = false;
213 | // Reset the stopwatch.
214 | //stopWatch.Reset();
215 | if (camera != null && camera.DeviceValid)
216 | {
217 | camera.LiveStop();
218 | camera.Dispose();
219 | }
220 | }
221 | catch (Exception ex)
222 | {
223 | Util.WriteLog(this.GetType(), ex);
224 | Util.Notify("相机关闭异常");
225 | }
226 |
227 | }
228 |
229 | public override void ContinuousShot()
230 | {
231 | if (camera == null || camera.DeviceValid == false)
232 | {
233 | return;
234 | }
235 | try
236 | {
237 | Command = Command.Video;
238 | TrigEnableSwitch.Switch = false;
239 | IsContinuousShot = true;
240 | }
241 | catch (Exception ex)
242 | {
243 | Util.WriteLog(this.GetType(), ex);
244 | Util.Notify("相机连续采集开始异常");
245 | }
246 | }
247 |
248 | public override void ContinuousShotStop()
249 | {
250 | try
251 | {
252 | // Set an enum parameter.
253 | if (camera == null || camera.DeviceValid == false)
254 | {
255 | return;
256 | }
257 | TrigEnableSwitch.Switch = true;
258 |
259 | IsContinuousShot = false;
260 | IsExtTrigger = false;
261 | //OneShot(Command.Test);
262 | Task.Run(() =>
263 | {
264 | ignoreImage = true;
265 | Thread.Sleep(1000);
266 | ignoreImage = false;
267 | });
268 | }
269 | catch (Exception ex)
270 | {
271 | Util.WriteLog(this.GetType(), ex);
272 | Util.Notify("相机连续采集停止异常");
273 | }
274 | }
275 |
276 | public override void OneShot(Command command)
277 | {
278 | try
279 | {
280 | if (IsContinuousShot || IsExtTrigger)
281 | {
282 | ContinuousShotStop();
283 | }
284 | Command = command;
285 | Softtrigger.Push();
286 | }
287 | catch
288 | {
289 | IsLink = false;
290 | Util.Notify("相机软触发异常");
291 | }
292 | }
293 |
294 | public override bool Open()
295 | {
296 | try
297 | {
298 |
299 | string name = camera.Device.Substring(0, 3);
300 | if (name == "DMK") //DMK为黑白相机标识 DFK则为彩色相机 当前未测试
301 | {
302 | camera.MemoryCurrentGrabberColorformat = ICImagingControlColorformats.ICY8;
303 | }
304 | else
305 | {
306 | camera.MemoryCurrentGrabberColorformat = ICImagingControlColorformats.ICRGB32;
307 | }
308 |
309 | ContinuousShotStop();//设置为软触发模式
310 | //设置帧率为最大帧率模式
311 | camera.DeviceFrameRate = camera.DeviceFrameRates.Max();
312 | camera.LiveDisplay = false;
313 | camera.LiveCaptureContinuous = true; // ImageAvailable
314 |
315 |
316 | camera.ImageAvailable += new System.EventHandler(TIS_ImageAvailable);
317 | GetCameraSettingData();
318 | camera.LiveStart();
319 |
320 |
321 | IsLink = true;
322 | //}
323 | //else
324 | //{
325 | // Util.Notify("无相机连接");
326 | // return false;
327 | //}
328 | }
329 | catch (Exception ex)
330 | {
331 | Util.WriteLog(this.GetType(), ex);
332 | Util.Notify("相机打开出现异常");
333 | throw ex;
334 | }
335 | return true;
336 |
337 |
338 | }
339 |
340 | private void TIS_ImageAvailable(object sender, ICImagingControl.ImageAvailableEventArgs e)
341 | {
342 | try
343 | {
344 | //Util.Notify(string.Format("相机{0}收到图像", cameraIndex));
345 | if (ignoreImage)
346 | {
347 | return;
348 | }
349 |
350 | //HTuple startTime;
351 | HOperatorSet.CountSeconds(out startTime);
352 | // Acquire the image from the camera. Only show the latest image. The camera may acquire images faster than the images can be displayed.
353 |
354 | ImageBuffer ImgBuffer = e.ImageBuffer;
355 | // Reduce the number of displayed images to a reasonable amount if the camera is acquiring images very fast.
356 | //if (!stopWatch.IsRunning || stopWatch.ElapsedMilliseconds > 33)
357 | {
358 | //stopWatch.Restart();
359 | //if (hPylonImage != null && hPylonImage.IsInitialized())
360 | //{
361 | // hPylonImage.Dispose();
362 | //}
363 | hPylonImage = new HImage();
364 |
365 | if (ImgBuffer.GetIntPtr() == IntPtr.Zero)
366 | {
367 | Util.Notify(string.Format("相机{0}数据损坏,采集失败", cameraIndex));
368 | return;
369 | }
370 |
371 | if (camera.MemoryCurrentGrabberColorformat == ICImagingControlColorformats.ICY8)
372 | {
373 |
374 |
375 |
376 | hPylonImage.GenImage1("byte", ImgBuffer.Size.Width, ImgBuffer.Size.Height, ImgBuffer.GetImageDataPtr());
377 | HImage imgTmp = hPylonImage.MirrorImage("row");
378 | hPylonImage.Dispose();
379 | hPylonImage = imgTmp;
380 |
381 | }
382 | else if (camera.MemoryCurrentGrabberColorformat == ICImagingControlColorformats.ICRGB32)
383 | {
384 | //allocate the m_stream_size amount of bytes in non-managed environment
385 | hPylonImage.GenImageInterleaved(ImgBuffer.GetImageDataPtr(), "rgb",
386 | ImgBuffer.Size.Width, ImgBuffer.Size.Height, -1, "byte", ImgBuffer.Size.Width, ImgBuffer.Size.Height, 0, 0, -1, 0);
387 |
388 | }
389 | else
390 | {
391 | Util.Notify(string.Format("相机{0}编码格式不正确", cameraIndex));
392 | }
393 | TrigerImageEvent();
394 | }
395 | }
396 | catch (System.ArgumentException ex)
397 | {
398 | Util.WriteLog(this.GetType(), ex);
399 | Util.Notify(string.Format("相机{0}图像数据包丢失", cameraIndex));
400 | }
401 | catch (Exception ex)
402 | {
403 | Util.WriteLog(this.GetType(), ex);
404 | Util.Notify(string.Format("相机{0}图像数据返回出现异常", cameraIndex));
405 | }
406 | }
407 | private object lockObj1 = new object();
408 |
409 | private bool inOutPut = false;
410 | public override void Output()
411 | {
412 |
413 | if (inOutPut)
414 | {
415 | Util.Notify("报警输出中当前输出忽略");
416 | return;
417 | }
418 | //
419 | Task.Run(() =>
420 | {
421 | lock (lockObj1)
422 | {
423 | inOutPut = true;
424 |
425 | VCDProp.RangeValue[VCDIDs.VCDElement_GPIOOut] = 1;
426 | // Now write it into the video capture device.
427 | VCDProp.OnePush(VCDIDs.VCDElement_GPIOWrite);
428 | Thread.Sleep((int)outLineTime);
429 | VCDProp.RangeValue[VCDIDs.VCDElement_GPIOOut] = 0;
430 | // Now write it into the video capture device.
431 | VCDProp.OnePush(VCDIDs.VCDElement_GPIOWrite);
432 | Util.Notify("相机报警输出完成");
433 | inOutPut = false;
434 | }
435 | });
436 | }
437 |
438 | public override void SetExtTrigger()
439 | {
440 | try
441 | {
442 | if (IsContinuousShot || IsExtTrigger)
443 | {
444 | ContinuousShotStop();
445 | }
446 | TriggerDelayTime.Value = TriggerDelayAbs;
447 | TriggerDebounceTime.Value = LineDebouncerTimeAbs;
448 | Command = Command.ExtTrigger;
449 | IsExtTrigger = true;
450 | }
451 | catch (Exception ex)
452 | {
453 | Util.WriteLog(this.GetType(), ex);
454 | Util.Notify("相机外触发设置异常");
455 | }
456 | }
457 |
458 | protected override void GetCameraSettingData()
459 | {
460 | try
461 | {
462 | //关闭自动曝光
463 | VCDSwitchProperty exposureAuto = (VCDSwitchProperty)camera.VCDPropertyItems.FindInterface(
464 | VCDIDs.VCDID_Exposure + ":" +
465 | VCDIDs.VCDElement_Auto + ":" +
466 | VCDIDs.VCDInterface_Switch);
467 | exposureAuto.Switch = false;
468 |
469 | //关闭自动增益
470 | VCDSwitchProperty gainSwith = (VCDSwitchProperty)camera.VCDPropertyItems.FindInterface(
471 | VCDIDs.VCDID_Gain + ":" +
472 | VCDIDs.VCDElement_Auto + ":" +
473 | VCDIDs.VCDInterface_Switch);
474 | gainSwith.Switch = false;
475 |
476 |
477 | //long max, min, cur;
478 | gainMin = GainAbsoluteValue.RangeMin;
479 | gainMax = GainAbsoluteValue.RangeMax;
480 | gainCur = GainAbsoluteValue.Value;
481 | gainUnit = "";
482 |
483 | shuterUnit = "us";
484 |
485 |
486 | shuterMin = (long)ExposureAbsoluteValue.RangeMin * exposureSocle;
487 | shuterMax = (long)ExposureAbsoluteValue.RangeMax * exposureSocle;
488 | shuterCur = (long)ExposureAbsoluteValue.Value * exposureSocle;
489 |
490 |
491 | triggerDelayAbsMin = TriggerDelayTime.RangeMin;
492 | triggerDelayAbsMax = TriggerDelayTime.RangeMax;
493 | triggerDelayAbs = TriggerDelayTime.Value;
494 |
495 | lineDebouncerTimeAbsMin = TriggerDebounceTime.RangeMin;
496 | lineDebouncerTimeAbsMax = TriggerDebounceTime.RangeMax;
497 | lineDebouncerTimeAbs = TriggerDebounceTime.Value;
498 |
499 | }
500 | catch (Exception ex)
501 | {
502 | Util.WriteLog(this.GetType(), ex);
503 | Util.Notify("相机设置信息获取异常");
504 | }
505 | }
506 | }
507 | }
508 |
--------------------------------------------------------------------------------
/Yoga.Camera/IOSerial.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO.Ports;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 | using Yoga.Common.Basic;
8 |
9 | namespace Yoga.Camera
10 | {
11 | public class IOSerial
12 | {
13 | private static IOSerial instance;
14 |
15 | private SerialPort com = new SerialPort();
16 |
17 |
18 | private CommunicationParam rs232Param;
19 | public CommunicationParam Rs232Param
20 | {
21 | get
22 | {
23 | if (rs232Param == null)
24 | {
25 | rs232Param = new CommunicationParam();
26 | }
27 | return rs232Param;
28 | }
29 |
30 | set
31 | {
32 | rs232Param = value;
33 | }
34 | }
35 | public static IOSerial Instance
36 | {
37 | get
38 | {
39 | if (instance == null)
40 | {
41 | instance = new IOSerial();
42 | }
43 | return instance;
44 | }
45 | }
46 |
47 | ///
48 | /// 串口初始化
49 | ///
50 | public void InitSerial()
51 | {
52 | try
53 | {
54 | if (com.IsOpen)
55 | {
56 | Close();
57 | }
58 |
59 | com.PortName = Rs232Param.ComName;
60 | com.BaudRate = Convert.ToInt32(Rs232Param.BaudRate);
61 | com.Parity = (Parity)Convert.ToInt32(Rs232Param.Parity);
62 | com.DataBits = Convert.ToInt32(Rs232Param.DataBits);
63 | com.StopBits = (StopBits)Convert.ToInt32(Rs232Param.StopBits);
64 | //com.NewLine = "\r\n";
65 | //com.NewLine = "\r";
66 | //com.DataReceived += new SerialDataReceivedEventHandler(this.OnDataReceived);
67 |
68 | com.Open();
69 | }
70 | catch (Exception ex)
71 | {
72 | throw new ApplicationException("串口打开失败," + ex.Message);
73 | }
74 | }
75 |
76 | public void Close()
77 | {
78 | if (com.IsOpen)
79 | com.Close();
80 | }
81 |
82 | ///
83 | /// 写入数据到串口
84 | ///
85 | /// 待写入字符串-无校验
86 | ///
87 | public bool WriteDataToSerial(string str)
88 | {
89 | bool writeFlag = false;
90 | try
91 | {
92 | if (!com.IsOpen)
93 | {
94 | InitSerial();
95 | //com.Open();
96 | }
97 | com.Write(str);
98 | writeFlag = true;
99 | }
100 | catch
101 | {
102 | throw new ApplicationException("串口设置异常");
103 | }
104 | return writeFlag;
105 | }
106 | }
107 | }
108 |
--------------------------------------------------------------------------------
/Yoga.Camera/ImageEventArgs.cs:
--------------------------------------------------------------------------------
1 | using HalconDotNet;
2 | using System;
3 | using System.IO;
4 | using System.Runtime.Serialization;
5 | using System.Runtime.Serialization.Formatters.Binary;
6 |
7 | namespace Yoga.Camera
8 | {
9 | [Serializable]
10 | public class ImageEventArgs : EventArgs, IDisposable
11 | {
12 | public readonly Command Command;
13 | public readonly HImage CameraImage;
14 | public readonly HTuple StartTime;
15 |
16 | int? cameraIndex;
17 | int? settingIndex;
18 | bool _disposed;
19 | public void Dispose()
20 | {
21 | this.Dispose(true);
22 | GC.SuppressFinalize(this);
23 | }
24 |
25 | protected virtual void Dispose(bool disposing)
26 | {
27 | if (_disposed) return;
28 | if (disposing)
29 | {
30 | if (CameraImage != null && CameraImage.IsInitialized())
31 | {
32 | CameraImage.Dispose();
33 | }
34 | _disposed = true;
35 | }
36 | cameraIndex = null;
37 | settingIndex = null;
38 |
39 | }
40 |
41 | ~ImageEventArgs()
42 | {
43 | this.Dispose(false);
44 | }
45 | ///
46 | /// 工具编号,默认为1,只能设置一次
47 | ///
48 | public int SettingIndex
49 | {
50 | get
51 | {
52 | if (settingIndex == null)
53 | {
54 | return 1;
55 | }
56 | return settingIndex.Value;
57 | }
58 |
59 | set
60 | {
61 | if (settingIndex == null)
62 | {
63 | settingIndex = value;
64 | }
65 | }
66 | }
67 | ///
68 | /// 相机编号,默认为1,只能设置一次
69 | ///
70 | public int CameraIndex
71 | {
72 | get
73 | {
74 | if (cameraIndex == null)
75 | {
76 | return 1;
77 | }
78 | return cameraIndex.Value;
79 | }
80 |
81 | set
82 | {
83 | if (cameraIndex == null)
84 | {
85 | cameraIndex = value;
86 | }
87 | }
88 | }
89 | public ImageEventArgs Clone()
90 | {
91 | using (Stream objectStream = new MemoryStream())
92 | {
93 | IFormatter formatter = new BinaryFormatter();
94 | formatter.Serialize(objectStream, this);
95 |
96 | BinaryFormatter b = new BinaryFormatter();
97 | object obj = b.Deserialize(objectStream);
98 | return obj as ImageEventArgs;
99 | }
100 | }
101 | public ImageEventArgs(Command command, HImage cameraImage, int cameraIndex, HTuple startTime)
102 | {
103 | Command = command;
104 | CameraImage = cameraImage;
105 | CameraIndex = cameraIndex;
106 | StartTime = startTime;
107 | }
108 | public ImageEventArgs(Command command, HImage cameraImage, int cameraIndex, int settingIndex, HTuple startTime)
109 | {
110 | Command = command;
111 | CameraImage = cameraImage;
112 | CameraIndex = cameraIndex;
113 | SettingIndex = SettingIndex;
114 | StartTime = startTime;
115 | }
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/Yoga.Camera/MicrovisionCamera.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using MVGigE = MVAPI.MVGigE;
7 | using MVPro = MVAPI.MVCamProptySheet;
8 | using MVSTATUS = MVAPI.MVSTATUS_CODES;
9 | using MVImage = MVAPI.MVImage;
10 | using Yoga.Common;
11 | using HalconDotNet;
12 |
13 | namespace Yoga.Camera
14 | {
15 | public class MicrovisionCamera : CameraBase
16 | {
17 | IntPtr m_hCam = IntPtr.Zero;
18 |
19 | MVAPI.MV_PixelFormatEnums m_PixelFormat;
20 | int m_nWidth;
21 | int m_nHeight;
22 |
23 | IntPtr m_hImage = IntPtr.Zero;
24 |
25 | MVAPI.MV_SNAPPROC StreamCBDelegate = null;
26 |
27 |
28 | private bool ignoreImage = false;
29 |
30 | public MicrovisionCamera(IntPtr m_hCam, int index)
31 | {
32 | this.m_hCam = m_hCam;
33 | this.cameraIndex = index;
34 | }
35 |
36 | int StreamCB(ref MVAPI.IMAGE_INFO pInfo, IntPtr UserVal)
37 | {
38 | MVGigE.MVInfo2Image(m_hCam, ref pInfo, m_hImage);
39 | try
40 | {
41 | if (ignoreImage)
42 | {
43 | return 0;
44 | }
45 |
46 | //HTuple startTime;
47 | HOperatorSet.CountSeconds(out startTime);
48 |
49 | // Check if the image can be displayed.
50 | if (m_hImage!=IntPtr.Zero)
51 | {
52 | // Reduce the number of displayed images to a reasonable amount if the camera is acquiring images very fast.
53 | //if (!stopWatch.IsRunning || stopWatch.ElapsedMilliseconds > 33)
54 | {
55 | //stopWatch.Restart();
56 |
57 | //if (hPylonImage != null && hPylonImage.IsInitialized())
58 | //{
59 | // hPylonImage.Dispose();
60 | //}
61 | hPylonImage = new HImage();
62 | if (m_PixelFormat == MVAPI.MV_PixelFormatEnums.PixelFormat_Mono8)
63 | {
64 | //Util.Notify(string.Format("相机{0}数据尺寸{1}", cameraIndex, grabResult.PayloadSize));
65 | //allocate the m_stream_size amount of bytes in non-managed environment
66 |
67 | //转换为Halcon图像显示
68 | hPylonImage.GenImage1("byte", m_nWidth, m_nHeight, m_hImage);
69 |
70 |
71 | }
72 | else if (m_PixelFormat == MVAPI.MV_PixelFormatEnums.PixelFormat_BayerRG8)
73 | {
74 |
75 | hPylonImage.GenImageInterleaved(m_hImage, "bgr",
76 | m_nWidth, m_nHeight, -1, "byte", m_nWidth, m_nHeight, 0, 0, -1, 0);
77 |
78 | }
79 | else
80 | {
81 | Util.Notify(string.Format("相机{0}编码格式不正确", cameraIndex));
82 | }
83 | TrigerImageEvent();
84 | }
85 | }
86 | }
87 | catch (System.ArgumentException ex)
88 | {
89 | Util.WriteLog(this.GetType(), ex);
90 | Util.Notify(string.Format("相机{0}图像数据包丢失", cameraIndex));
91 | }
92 | catch (Exception ex)
93 | {
94 | Util.WriteLog(this.GetType(), ex);
95 | Util.Notify(string.Format("相机{0}图像数据返回出现异常", cameraIndex));
96 | }
97 | return 0;
98 | }
99 |
100 | private void ImageCreat()
101 | {
102 | int w = 0, h = 0;
103 |
104 | MVSTATUS r = MVGigE.MVGetWidth(m_hCam, out w);
105 | if (r != MVSTATUS.MVST_SUCCESS)
106 | {
107 | Util.Notify("取得图像宽度失败");
108 | return;
109 | }
110 |
111 | r = MVGigE.MVGetHeight(m_hCam, out h);
112 | if (r != MVSTATUS.MVST_SUCCESS)
113 | {
114 | Util.Notify("取得图像高度失败");
115 | return;
116 | }
117 | r = MVGigE.MVGetPixelFormat(m_hCam, out m_PixelFormat);
118 | if (r != MVSTATUS.MVST_SUCCESS)
119 | {
120 | Util.Notify("取得图像颜色模式失败");
121 | return;
122 | }
123 | if (m_nWidth != w || m_nHeight != h)
124 | {
125 | m_nWidth = w;
126 | m_nHeight = h;
127 |
128 | if (m_hImage != IntPtr.Zero)
129 | {
130 | MVAPI.MVImage.MVImageRelease(m_hImage);
131 | m_hImage = IntPtr.Zero;
132 | }
133 |
134 | if (m_PixelFormat == MVAPI.MV_PixelFormatEnums.PixelFormat_Mono8)
135 | {
136 | m_hImage = MVAPI.MVImage.MVImageCreate(w, h, 8);
137 | }
138 | else
139 | {
140 | m_hImage = MVAPI.MVImage.MVImageCreate(w, h, 24);
141 | }
142 | }
143 | }
144 |
145 | public override double GainCur
146 | {
147 | get
148 | {
149 | return gainCur;
150 | }
151 |
152 | set
153 | {
154 | try
155 | {
156 | double gainValue = value;
157 |
158 | //判断输入值是否在增益值的范围内
159 | //若输入的值大于最大值则将增益值设置成最大值
160 | if (gainValue > GainMax)
161 | {
162 | gainValue = GainMax;
163 | }
164 |
165 | //若输入的值小于最小值则将增益的值设置成最小值
166 | if (gainValue < GainMin)
167 | {
168 | gainValue = GainMin;
169 | }
170 | MVGigE.MVSetGain(m_hCam, gainValue);
171 |
172 | gainCur = gainValue;
173 | }
174 | catch (Exception ex)
175 | {
176 | Util.WriteLog(this.GetType(), ex);
177 | Util.Notify("相机增益设置异常");
178 | }
179 | }
180 | }
181 |
182 | public override long ShuterCur
183 | {
184 | get
185 | {
186 | return shuterCur;
187 | }
188 |
189 | set
190 | {
191 | try
192 | {
193 | long shutterValue = value;
194 |
195 | //获取当前相机的曝光值、最小值、最大值和单位
196 |
197 | //判断输入值是否在曝光时间的范围内
198 | //若大于最大值则将曝光值设为最大值
199 | if (shutterValue > ShuterMax)
200 | {
201 | shutterValue = ShuterMax;
202 | }
203 | //若小于最小值将曝光值设为最小值
204 | else if (shutterValue < ShuterMin)
205 | {
206 | shutterValue = ShuterMin;
207 | }
208 | //else
209 | //{
210 | // //shutterValue = ShuterMin + (((shutterValue - ShuterMin) / incr) * incr);
211 | //}
212 |
213 | MVGigE.MVSetExposureTime(m_hCam, shutterValue);
214 | shuterCur = shutterValue;
215 | }
216 | catch (Exception ex)
217 | {
218 | Util.WriteLog(this.GetType(), ex);
219 | Util.Notify("相机曝光设置异常");
220 | }
221 | }
222 | }
223 |
224 | public override void Close()
225 | {
226 | try
227 | {
228 | IsLink = false;
229 | // Reset the stopwatch.
230 | //stopWatch.Reset();
231 | MVGigE.MVSetTriggerMode(m_hCam, MVAPI.TriggerModeEnums.TriggerMode_Off);
232 | MVGigE.MVCloseCam(m_hCam);
233 | m_hCam = IntPtr.Zero;
234 | MVGigE.MVTerminateLib();
235 | MVAPI.MVImage.MVImageRelease(m_hImage);
236 | }
237 | catch (Exception ex)
238 | {
239 | Util.WriteLog(this.GetType(), ex);
240 | Util.Notify("相机关闭异常");
241 | }
242 | }
243 |
244 | public override void ContinuousShot()
245 | {
246 | if (m_hCam == IntPtr.Zero)
247 | {
248 | return;
249 | }
250 | try
251 | {
252 | Command = Command.Video;
253 | MVGigE.MVSetTriggerMode(m_hCam, MVAPI.TriggerModeEnums.TriggerMode_On);
254 | MVGigE.MVStartGrab(m_hCam, StreamCBDelegate, IntPtr.Zero);
255 | IsContinuousShot = true;
256 | }
257 | catch (Exception ex)
258 | {
259 | Util.WriteLog(this.GetType(), ex);
260 | Util.Notify("相机连续采集开始异常");
261 | }
262 | }
263 |
264 | public override void ContinuousShotStop()
265 | {
266 | try
267 | {
268 | // Set an enum parameter.
269 | if (m_hCam==IntPtr.Zero)
270 | {
271 | return;
272 | }
273 | MVGigE.MVStopGrab(m_hCam);
274 | IsContinuousShot = false;
275 | IsExtTrigger = false;
276 | Task.Run(() =>
277 | {
278 | ignoreImage = true;
279 | System.Threading.Thread.Sleep(1000);
280 | ignoreImage = false;
281 | });
282 | }
283 | catch (Exception ex)
284 | {
285 | Util.WriteLog(this.GetType(), ex);
286 | Util.Notify("相机连续采集停止异常");
287 | }
288 |
289 | }
290 |
291 | public override void OneShot(Command command)
292 | {
293 | try
294 | {
295 | if (IsContinuousShot || IsExtTrigger)
296 | {
297 | ContinuousShotStop();
298 | }
299 | Command = command;
300 | MVGigE.MVSetTriggerMode(m_hCam, MVAPI.TriggerModeEnums.TriggerMode_Off);
301 | MVGigE.MVStartGrab(m_hCam, StreamCBDelegate, IntPtr.Zero);
302 | }
303 | catch
304 | {
305 | IsLink = false;
306 | Util.Notify("相机软触发异常");
307 | }
308 | }
309 |
310 | public override bool Open()
311 | {
312 | try
313 | {
314 |
315 | ImageCreat();
316 |
317 |
318 |
319 | ContinuousShotStop();//设置为软触发模式
320 |
321 | MVGigE.MVSetStrobeSource(m_hCam, MVAPI.LineSourceEnums.LineSource_ExposureActive);
322 |
323 |
324 | StreamCBDelegate += new MVAPI.MV_SNAPPROC(StreamCB);
325 | MVGigE.MVStartGrab(m_hCam, StreamCBDelegate, IntPtr.Zero);
326 | GetCameraSettingData();
327 |
328 | IsLink = true;
329 | //}
330 | //else
331 | //{
332 | // Util.Notify("无相机连接");
333 | // return false;
334 | //}
335 | }
336 | catch (Exception ex)
337 | {
338 | Util.WriteLog(this.GetType(), ex);
339 | Util.Notify("相机打开出现异常");
340 | throw ex;
341 | }
342 | return true;
343 | }
344 |
345 | public override void Output()
346 | {
347 | throw new NotImplementedException();
348 | }
349 |
350 | public override void SetExtTrigger()
351 | {
352 | throw new NotImplementedException();
353 | }
354 |
355 | protected override void GetCameraSettingData()
356 | {
357 |
358 | try
359 | {
360 | //long max, min, cur;
361 |
362 | MVGigE.MVGetGainRange(m_hCam, out gainMin, out gainMax);
363 | MVGigE.MVGetGain(m_hCam, out gainCur);
364 | gainUnit = "";
365 |
366 | shuterUnit = "us";
367 |
368 |
369 | double pExpMin;
370 | double pExpMax;
371 | MVGigE.MVGetExposureTimeRange(m_hCam, out pExpMin, out pExpMax);
372 |
373 | double pExposuretime;
374 | MVGigE.MVGetExposureTime(m_hCam, out pExposuretime);
375 | shuterMin = (long)pExpMin;
376 | shuterMax = (long)pExpMax;
377 | shuterCur = (long)pExposuretime;
378 |
379 | uint pMin;
380 | uint pMax;
381 | MVGigE.MVGetTriggerDelayRange(m_hCam, out pMin, out pMax);
382 | triggerDelayAbsMin = pMin;
383 | triggerDelayAbsMax = pMax;
384 |
385 | uint pDelay_us;
386 | MVGigE.MVGetTriggerDelay(m_hCam, out pDelay_us);
387 | triggerDelayAbs = pDelay_us;
388 |
389 | lineDebouncerTimeAbsMin = 0;
390 | lineDebouncerTimeAbsMax = 5000;
391 | lineDebouncerTimeAbs = 0;
392 |
393 | }
394 | catch (Exception ex)
395 | {
396 | Util.WriteLog(this.GetType(), ex);
397 | Util.Notify("相机设置信息获取异常");
398 | }
399 | }
400 | }
401 | }
402 |
--------------------------------------------------------------------------------
/Yoga.Camera/MindCamera.cs:
--------------------------------------------------------------------------------
1 | using HalconDotNet;
2 | using MVSDK;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Diagnostics;
6 | using System.Linq;
7 | using System.Text;
8 | using System.Threading;
9 | using System.Threading.Tasks;
10 | using Yoga.Common;
11 | using CameraHandle = System.Int32;
12 |
13 | namespace Yoga.Camera
14 | {
15 | ///
16 | /// 迈徳威视相机
17 | ///
18 | public class MindCamera : CameraBase
19 | {
20 | IntPtr m_Grabber;
21 | CameraHandle m_hCamera;
22 | private bool ignoreImage = false;
23 | //private Stopwatch stopWatch = new Stopwatch();
24 | protected pfnCameraGrabberFrameListener m_FrameListener;
25 | public override double GainCur
26 | {
27 | get
28 | {
29 | return gainCur;
30 | }
31 |
32 | set
33 | {
34 | try
35 | {
36 | double gainValue = value;
37 |
38 | //判断输入值是否在增益值的范围内
39 | //若输入的值大于最大值则将增益值设置成最大值
40 | if (gainValue > GainMax)
41 | {
42 | gainValue = GainMax;
43 | }
44 |
45 | //若输入的值小于最小值则将增益的值设置成最小值
46 | if (gainValue < GainMin)
47 | {
48 | gainValue = GainMin;
49 | }
50 | // Some camera models may have auto functions enabled. To set the gain value to a specific value,
51 | // the Gain Auto function must be disabled first (if gain auto is available).
52 | //g_camera.Parameters[PLCamera.GainAuto].TrySetValue(PLCamera.GainAuto.Off); // Set GainAuto to Off if it is writable.
53 |
54 | MvApi.CameraSetAnalogGain(m_hCamera, (int)gainValue);
55 |
56 | gainCur = gainValue;
57 | }
58 | catch (Exception ex)
59 | {
60 | Util.WriteLog(this.GetType(), ex);
61 | Util.Notify("相机增益设置异常");
62 | }
63 | }
64 | }
65 |
66 | public override long ShuterCur
67 | {
68 | get
69 | {
70 | return shuterCur;
71 | }
72 |
73 | set
74 | {
75 | try
76 | {
77 | long shutterValue = value;
78 |
79 | //获取当前相机的曝光值、最小值、最大值和单位
80 |
81 | //判断输入值是否在曝光时间的范围内
82 | //若大于最大值则将曝光值设为最大值
83 |
84 |
85 | if (shutterValue > ShuterMax)
86 | {
87 | shutterValue = ShuterMax;
88 | }
89 | //若小于最小值将曝光值设为最小值
90 | else if (shutterValue < ShuterMin)
91 | {
92 | shutterValue = ShuterMin;
93 | }
94 |
95 | MvApi.CameraSetExposureTime(m_hCamera, shutterValue);
96 | shuterCur = shutterValue;
97 | }
98 | catch (Exception ex)
99 | {
100 | Util.WriteLog(this.GetType(), ex);
101 | Util.Notify("相机曝光设置异常");
102 | }
103 | }
104 | }
105 |
106 |
107 | public MindCamera(CameraHandle m_hCamera, IntPtr m_Grabber, int index)
108 | {
109 | this.m_hCamera = m_hCamera;
110 | this.m_Grabber = m_Grabber;
111 | this.cameraIndex = index;
112 |
113 | }
114 |
115 | private int CameraGrabberFrameListener(IntPtr Grabber, int Phase, IntPtr pFrameBuffer, ref tSdkFrameHead pFrameHead, IntPtr Context)
116 | {
117 | if (Phase == 0)
118 | {
119 | // RAW数据处理,pFrameBuffer=Raw数据
120 | }
121 | else if (Phase == 1)
122 | {
123 | // 截图前处理,pFrameBuffer=RGB数据
124 | }
125 | else if (Phase == 2)
126 | {
127 | // 显示前处理,pFrameBuffer=RGB数据
128 |
129 | try
130 | {
131 | if (ignoreImage)
132 | {
133 | return 1;
134 | }
135 | //HTuple startTime;
136 | HOperatorSet.CountSeconds(out startTime);
137 | // Reduce the number of displayed images to a reasonable amount if the camera is acquiring images very fast.
138 | //if (!stopWatch.IsRunning || stopWatch.ElapsedMilliseconds > 33)
139 | {
140 | //stopWatch.Restart();
141 |
142 | //if (hPylonImage != null && hPylonImage.IsInitialized())
143 | //{
144 | // hPylonImage.Dispose();
145 | //}
146 | hPylonImage = new HImage();
147 | emImageFormat currentFormat = (emImageFormat)pFrameHead.uiMediaType;
148 | if (currentFormat == emImageFormat.CAMERA_MEDIA_TYPE_MONO8)
149 | {
150 | hPylonImage.GenImage1("byte", pFrameHead.iWidth, pFrameHead.iHeight, pFrameBuffer);
151 | }
152 | else if (currentFormat == emImageFormat.CAMERA_MEDIA_TYPE_RGB8)
153 | {
154 | hPylonImage.GenImageInterleaved(pFrameBuffer,
155 | "rgb",
156 | pFrameHead.iWidth, pFrameHead.iHeight,
157 | -1, "byte",
158 | pFrameHead.iWidth, pFrameHead.iHeight,
159 | 0, 0, -1, 0);
160 | }
161 | else
162 | {
163 | Util.Notify(string.Format("相机{0}编码格式不正确,当前格式{1}", cameraIndex, pFrameHead.uiMediaType));
164 | }
165 | TrigerImageEvent();
166 | }
167 |
168 | }
169 | catch (System.ArgumentException ex)
170 | {
171 | Util.WriteLog(this.GetType(), ex);
172 | Util.Notify(string.Format("相机{0}图像数据包丢失", cameraIndex));
173 | }
174 | catch (Exception ex)
175 | {
176 | Util.WriteLog(this.GetType(), ex);
177 | Util.Notify(string.Format("相机{0}图像数据返回出现异常", cameraIndex));
178 | }
179 |
180 | }
181 |
182 | return 1;
183 | }
184 |
185 | public override void Close()
186 | {
187 |
188 | try
189 | {
190 | IsLink = false;
191 |
192 | // Reset the stopwatch.
193 | //stopWatch.Reset();
194 | if (m_Grabber != IntPtr.Zero)
195 | {
196 | MvApi.CameraGrabber_Destroy(m_Grabber);
197 | }
198 | }
199 | catch (Exception ex)
200 | {
201 | Util.WriteLog(this.GetType(), ex);
202 | Util.Notify("相机关闭异常");
203 | }
204 |
205 |
206 | }
207 |
208 | public override void ContinuousShot()
209 | {
210 | if (m_Grabber == IntPtr.Zero)
211 | {
212 | return;
213 | }
214 | try
215 | {
216 | Command = Command.Video;
217 | MvApi.CameraSetTriggerMode(m_hCamera, (int)emSdkSnapMode.CONTINUATION);
218 | CameraSdkStatus suatus = MvApi.CameraGrabber_StartLive(m_Grabber);
219 | if (suatus != CameraSdkStatus.CAMERA_STATUS_SUCCESS)
220 | {
221 | Util.Notify("相机连续采集开始异常");
222 | }
223 | IsContinuousShot = true;
224 | }
225 | catch (Exception ex)
226 | {
227 | Util.WriteLog(this.GetType(), ex);
228 | Util.Notify("相机连续采集开始异常");
229 | }
230 | }
231 |
232 | public override void ContinuousShotStop()
233 | {
234 | try
235 | {
236 | // Set an enum parameter.
237 | if (m_Grabber == IntPtr.Zero)
238 | {
239 | return;
240 | }
241 | CameraSdkStatus status;
242 | CameraSdkStatus st = MvApi.CameraSetExtTrigSignalType(m_hCamera, 1);//上升沿触发
243 | status = MvApi.CameraSetTriggerMode(m_hCamera, (int)emSdkSnapMode.SOFT_TRIGGER);
244 | IsContinuousShot = false;
245 | IsExtTrigger = false;
246 | Task.Run(() =>
247 | {
248 | ignoreImage = true;
249 | Thread.Sleep(1000);
250 | ignoreImage = false;
251 | });
252 | }
253 | catch (Exception ex)
254 | {
255 | Util.WriteLog(this.GetType(), ex);
256 | Util.Notify("相机连续采集停止异常");
257 | }
258 | }
259 |
260 | public override void OneShot(Command command)
261 | {
262 | try
263 | {
264 | if (IsContinuousShot || IsExtTrigger)
265 | {
266 | ContinuousShotStop();
267 | }
268 | Command = command;
269 | CameraSdkStatus status;
270 | //status = MvApi.CameraSetTriggerMode(m_hCamera, (int)emSdkSnapMode.SOFT_TRIGGER);
271 | status = MvApi.CameraSoftTrigger(m_hCamera);
272 | }
273 | catch
274 | {
275 | IsLink = false;
276 | Util.Notify("相机软触发异常");
277 | }
278 | }
279 | public enum emCameraGPIOMode
280 | {
281 | IOMODE_TRIG_INPUT = 0, //触发输入
282 | IOMODE_STROBE_OUTPUT, //闪光灯输出
283 | IOMODE_GP_INPUT, //通用型输入
284 | IOMODE_GP_OUTPUT, //通用型输出
285 | IOMODE_PWM_OUTPUT, //PWM型输出
286 | }
287 | public override bool Open()
288 | {
289 | try
290 | {
291 | m_FrameListener = new pfnCameraGrabberFrameListener(CameraGrabberFrameListener);
292 | //图像镜像设置
293 | MvApi.CameraSetMirror(m_hCamera, 0, 0);
294 | MvApi.CameraSetMirror(m_hCamera, 1, 0);
295 | MvApi.CameraGrabber_SetFrameListener(m_Grabber, m_FrameListener, IntPtr.Zero);
296 | ContinuousShotStop();
297 | GetCameraSettingData();
298 | MvApi.CameraGrabber_StartLive(m_Grabber);
299 | MvApi.CameraSetOutPutIOMode(m_hCamera, 0, (int)emCameraGPIOMode.IOMODE_GP_OUTPUT);
300 | MvApi.CameraSetIOState(m_hCamera, 0, 1);
301 | // Reset the stopwatch used to reduce the amount of displayed images. The camera may acquire images faster than the images can be displayed
302 | //stopWatch.Reset();
303 | IsLink = true;
304 | }
305 | catch (Exception ex)
306 | {
307 | Util.WriteLog(this.GetType(), ex);
308 | Util.Notify("相机打开出现异常");
309 | throw ex;
310 | }
311 | return true;
312 | }
313 | private object lockObj1 = new object();
314 |
315 | private bool inOutPut = false;
316 | public override void Output()
317 | {
318 | if (inOutPut)
319 | {
320 | Util.Notify("报警输出中当前输出忽略");
321 | return;
322 | }
323 | //
324 | Task.Run(() =>
325 | {
326 | lock (lockObj1)
327 | {
328 | inOutPut = true;
329 |
330 | MvApi.CameraSetIOState(m_hCamera, 0, 0);
331 | Thread.Sleep((int)outLineTime);
332 | MvApi.CameraSetIOState(m_hCamera, 0, 1);
333 | //g_camera.Parameters[PLCamera.timer].TrySetValue(PLCamera.LineSource.Timer1Active.);
334 | Util.Notify("相机报警输出完成");
335 | inOutPut = false;
336 | }
337 | });
338 | }
339 |
340 | public override void SetExtTrigger()
341 | {
342 | try
343 | {
344 |
345 | CameraSdkStatus st= MvApi.CameraSetExtTrigSignalType(m_hCamera, 1);//下降沿触发
346 | st=MvApi.CameraSetTriggerMode(m_hCamera, (int)emSdkSnapMode.EXTERNAL_TRIGGER);
347 |
348 |
349 | st = MvApi.CameraSetTriggerDelayTime(m_hCamera, (uint)triggerDelayAbs);
350 |
351 | //m_pOperator.SetEnumValue("ExposureAuto", 0);
352 |
353 | //m_pOperator.SetFloatValue("TriggerDelay", (float)triggerDelayAbs);
354 |
355 | st = MvApi.CameraSetExtTrigJitterTime(m_hCamera, (uint)LineDebouncerTimeAbs);
356 | //m_pOperator.SetIntValue("LineDebouncerTime", (uint)LineDebouncerTimeAbs);
357 | Command = Command.ExtTrigger;
358 | IsExtTrigger = true;
359 | }
360 | catch (Exception ex)
361 | {
362 | Util.WriteLog(this.GetType(), ex);
363 | Util.Notify("相机外触发设置异常");
364 | }
365 | }
366 |
367 | protected override void GetCameraSettingData()
368 | {
369 | try
370 | {
371 | tSdkCameraCapbility cap;
372 | MvApi.CameraGetCapability(m_hCamera, out cap);
373 |
374 | if (cap.sIspCapacity.bMonoSensor == 1)
375 | {
376 | MvApi.CameraSetIspOutFormat(m_hCamera, (uint)emImageFormat.CAMERA_MEDIA_TYPE_MONO8);
377 | }
378 | else
379 | {
380 |
381 | MvApi.CameraSetIspOutFormat(m_hCamera, (uint)emImageFormat.CAMERA_MEDIA_TYPE_RGB8);
382 | }
383 | //long max, min, cur;
384 | gainMin = cap.sExposeDesc.uiAnalogGainMin;
385 | gainMax = cap.sExposeDesc.uiAnalogGainMax;
386 | int piAnalogGain = 0;
387 | MvApi.CameraGetAnalogGain(m_hCamera, ref piAnalogGain);
388 |
389 | gainCur = piAnalogGain;
390 | gainUnit = "";
391 |
392 | shuterUnit = "us";
393 |
394 | shuterMin = cap.sExposeDesc.uiExposeTimeMin;
395 | shuterMax = cap.sExposeDesc.uiExposeTimeMax;
396 | double pfLineTime = 0;
397 | MvApi.CameraGetExposureLineTime(m_hCamera, ref pfLineTime);
398 |
399 | shuterCur = (long)pfLineTime;
400 |
401 |
402 | triggerDelayAbsMin = 0;
403 | triggerDelayAbsMax = 1000000;
404 | uint puDelayTimeUs = 0;
405 | MvApi.CameraGetTriggerDelayTime(m_hCamera, ref puDelayTimeUs);
406 |
407 | triggerDelayAbs = puDelayTimeUs;
408 |
409 | lineDebouncerTimeAbsMin = 0;
410 | lineDebouncerTimeAbsMax = 5000;
411 | lineDebouncerTimeAbs = 0;
412 |
413 | }
414 | catch (Exception ex)
415 | {
416 | Util.WriteLog(this.GetType(), ex);
417 | Util.Notify("相机设置信息获取异常");
418 | }
419 | }
420 | }
421 | }
422 |
--------------------------------------------------------------------------------
/Yoga.Camera/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // 有关程序集的一般信息由以下
6 | // 控制。更改这些特性值可修改
7 | // 与程序集关联的信息。
8 | [assembly: AssemblyTitle("Camera")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("HYCONN")]
12 | [assembly: AssemblyProduct("Camera")]
13 | [assembly: AssemblyCopyright("Copyright © HYCONN 2018")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | //将 ComVisible 设置为 false 将使此程序集中的类型
18 | //对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型,
19 | //请将此类型的 ComVisible 特性设置为 true。
20 | [assembly: ComVisible(false)]
21 |
22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
23 | [assembly: Guid("306083da-d250-4b6d-a487-f684f4330c0d")]
24 |
25 | // 程序集的版本信息由下列四个值组成:
26 | //
27 | // 主版本
28 | // 次版本
29 | // 生成号
30 | // 修订号
31 | //
32 | //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
33 | // 方法是按如下所示使用“*”: :
34 | [assembly: AssemblyVersion("1.1.*")]
35 | //[assembly: AssemblyVersion("1.0.0.0")]
36 | //[assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Yoga.Camera/Yoga.Camera.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {306083DA-D250-4B6D-A487-F684F4330C0D}
8 | Library
9 | Properties
10 | Yoga.Camera
11 | Yoga.Camera
12 | v4.5.2
13 | 512
14 |
15 |
16 | true
17 | ..\..\bin\x64\Debug\
18 | DEBUG;TRACE
19 | full
20 | x64
21 | prompt
22 | MinimumRecommendedRules.ruleset
23 |
24 |
25 | ..\..\bin\x64\Release\
26 | TRACE
27 | true
28 | pdbonly
29 | x64
30 | prompt
31 | MinimumRecommendedRules.ruleset
32 |
33 |
34 | true
35 | ..\..\bin\x86\Debug\
36 | DEBUG;TRACE
37 | full
38 | x86
39 | prompt
40 | MinimumRecommendedRules.ruleset
41 |
42 |
43 | ..\..\bin\x86\Release\
44 | TRACE
45 | true
46 | pdbonly
47 | x86
48 | prompt
49 | MinimumRecommendedRules.ruleset
50 |
51 |
52 |
53 | ..\..\..\..\..\..\Program Files\MVTec\HALCON-13.0\bin\dotnet35\halcondotnet.dll
54 |
55 |
56 |
57 | ..\..\..\..\..\..\Program Files (x86)\MVS\Development\Dlls\win32\MvCameraControl.Net.dll
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 | False
71 | ..\..\..\..\..\..\Program Files (x86)\Common Files\IC Imaging Control 3.4\bin\v4.0.30319\x86\TIS.Imaging.ICImagingControl34.dll
72 |
73 |
74 | ..\..\..\VisionMix\bin\x86\Debug\Yoga.Common.dll
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 | Form
89 |
90 |
91 | frmCameraSetting.cs
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 | frmCameraSetting.cs
107 |
108 |
109 |
110 |
117 |
--------------------------------------------------------------------------------
/Yoga.Camera/frmCameraSetting.Designer.cs:
--------------------------------------------------------------------------------
1 | namespace Yoga.Camera
2 | {
3 | partial class frmCameraSetting
4 | {
5 | ///
6 | /// Required designer variable.
7 | ///
8 | private System.ComponentModel.IContainer components = null;
9 |
10 | ///
11 | /// Clean up any resources being used.
12 | ///
13 | /// true if managed resources should be disposed; otherwise, false.
14 | protected override void Dispose(bool disposing)
15 | {
16 | if (disposing && (components != null))
17 | {
18 | components.Dispose();
19 | }
20 | base.Dispose(disposing);
21 | }
22 |
23 | #region Windows Form Designer generated code
24 |
25 | ///
26 | /// Required method for Designer support - do not modify
27 | /// the contents of this method with the code editor.
28 | ///
29 | private void InitializeComponent()
30 | {
31 | this.trackBarShutter = new System.Windows.Forms.TrackBar();
32 | this.trackBarGain = new System.Windows.Forms.TrackBar();
33 | this.m_lbl_Shutter = new System.Windows.Forms.Label();
34 | this.m_lbl_Gain = new System.Windows.Forms.Label();
35 | this.groupBox3 = new System.Windows.Forms.GroupBox();
36 | this.label4 = new System.Windows.Forms.Label();
37 | this.ImageAngleComboBox = new System.Windows.Forms.ComboBox();
38 | this.btnTestOut = new System.Windows.Forms.Button();
39 | this.btnResetOutLineTime = new System.Windows.Forms.Button();
40 | this.UpDownOutLineTime = new System.Windows.Forms.NumericUpDown();
41 | this.label3 = new System.Windows.Forms.Label();
42 | this.trackBarOutLineTime = new System.Windows.Forms.TrackBar();
43 | this.btnResetLineDebouncerTimeAbs = new System.Windows.Forms.Button();
44 | this.UpDownLineDebouncerTimeAbs = new System.Windows.Forms.NumericUpDown();
45 | this.label2 = new System.Windows.Forms.Label();
46 | this.trackBarLineDebouncerTimeAbs = new System.Windows.Forms.TrackBar();
47 | this.btnResetTriggerDelayAbs = new System.Windows.Forms.Button();
48 | this.UpDownTriggerDelayAbs = new System.Windows.Forms.NumericUpDown();
49 | this.label1 = new System.Windows.Forms.Label();
50 | this.trackBarTriggerDelayAbs = new System.Windows.Forms.TrackBar();
51 | this.btnSave = new System.Windows.Forms.Button();
52 | this.btnResetGain = new System.Windows.Forms.Button();
53 | this.btnResetShutter = new System.Windows.Forms.Button();
54 | this.UpDownGain = new System.Windows.Forms.NumericUpDown();
55 | this.UpDownShutter = new System.Windows.Forms.NumericUpDown();
56 | ((System.ComponentModel.ISupportInitialize)(this.trackBarShutter)).BeginInit();
57 | ((System.ComponentModel.ISupportInitialize)(this.trackBarGain)).BeginInit();
58 | this.groupBox3.SuspendLayout();
59 | ((System.ComponentModel.ISupportInitialize)(this.UpDownOutLineTime)).BeginInit();
60 | ((System.ComponentModel.ISupportInitialize)(this.trackBarOutLineTime)).BeginInit();
61 | ((System.ComponentModel.ISupportInitialize)(this.UpDownLineDebouncerTimeAbs)).BeginInit();
62 | ((System.ComponentModel.ISupportInitialize)(this.trackBarLineDebouncerTimeAbs)).BeginInit();
63 | ((System.ComponentModel.ISupportInitialize)(this.UpDownTriggerDelayAbs)).BeginInit();
64 | ((System.ComponentModel.ISupportInitialize)(this.trackBarTriggerDelayAbs)).BeginInit();
65 | ((System.ComponentModel.ISupportInitialize)(this.UpDownGain)).BeginInit();
66 | ((System.ComponentModel.ISupportInitialize)(this.UpDownShutter)).BeginInit();
67 | this.SuspendLayout();
68 | //
69 | // trackBarShutter
70 | //
71 | this.trackBarShutter.Location = new System.Drawing.Point(194, 20);
72 | this.trackBarShutter.Name = "trackBarShutter";
73 | this.trackBarShutter.Size = new System.Drawing.Size(327, 45);
74 | this.trackBarShutter.TabIndex = 0;
75 | this.trackBarShutter.Scroll += new System.EventHandler(this.trackBarShutter_Scroll);
76 | //
77 | // trackBarGain
78 | //
79 | this.trackBarGain.Location = new System.Drawing.Point(194, 60);
80 | this.trackBarGain.Name = "trackBarGain";
81 | this.trackBarGain.Size = new System.Drawing.Size(327, 45);
82 | this.trackBarGain.TabIndex = 1;
83 | this.trackBarGain.Scroll += new System.EventHandler(this.trackBarGain_Scroll);
84 | //
85 | // m_lbl_Shutter
86 | //
87 | this.m_lbl_Shutter.AutoSize = true;
88 | this.m_lbl_Shutter.Location = new System.Drawing.Point(20, 27);
89 | this.m_lbl_Shutter.Name = "m_lbl_Shutter";
90 | this.m_lbl_Shutter.Size = new System.Drawing.Size(53, 12);
91 | this.m_lbl_Shutter.TabIndex = 13;
92 | this.m_lbl_Shutter.Text = "曝光时间";
93 | //
94 | // m_lbl_Gain
95 | //
96 | this.m_lbl_Gain.AutoSize = true;
97 | this.m_lbl_Gain.Location = new System.Drawing.Point(20, 66);
98 | this.m_lbl_Gain.Name = "m_lbl_Gain";
99 | this.m_lbl_Gain.Size = new System.Drawing.Size(29, 12);
100 | this.m_lbl_Gain.TabIndex = 15;
101 | this.m_lbl_Gain.Text = "增益";
102 | //
103 | // groupBox3
104 | //
105 | this.groupBox3.Controls.Add(this.label4);
106 | this.groupBox3.Controls.Add(this.ImageAngleComboBox);
107 | this.groupBox3.Controls.Add(this.btnTestOut);
108 | this.groupBox3.Controls.Add(this.btnResetOutLineTime);
109 | this.groupBox3.Controls.Add(this.UpDownOutLineTime);
110 | this.groupBox3.Controls.Add(this.label3);
111 | this.groupBox3.Controls.Add(this.trackBarOutLineTime);
112 | this.groupBox3.Controls.Add(this.btnResetLineDebouncerTimeAbs);
113 | this.groupBox3.Controls.Add(this.UpDownLineDebouncerTimeAbs);
114 | this.groupBox3.Controls.Add(this.label2);
115 | this.groupBox3.Controls.Add(this.trackBarLineDebouncerTimeAbs);
116 | this.groupBox3.Controls.Add(this.btnResetTriggerDelayAbs);
117 | this.groupBox3.Controls.Add(this.UpDownTriggerDelayAbs);
118 | this.groupBox3.Controls.Add(this.label1);
119 | this.groupBox3.Controls.Add(this.trackBarTriggerDelayAbs);
120 | this.groupBox3.Controls.Add(this.btnSave);
121 | this.groupBox3.Controls.Add(this.btnResetGain);
122 | this.groupBox3.Controls.Add(this.btnResetShutter);
123 | this.groupBox3.Controls.Add(this.UpDownGain);
124 | this.groupBox3.Controls.Add(this.UpDownShutter);
125 | this.groupBox3.Controls.Add(this.m_lbl_Gain);
126 | this.groupBox3.Controls.Add(this.trackBarGain);
127 | this.groupBox3.Controls.Add(this.m_lbl_Shutter);
128 | this.groupBox3.Controls.Add(this.trackBarShutter);
129 | this.groupBox3.Location = new System.Drawing.Point(12, 12);
130 | this.groupBox3.Name = "groupBox3";
131 | this.groupBox3.Size = new System.Drawing.Size(585, 294);
132 | this.groupBox3.TabIndex = 4;
133 | this.groupBox3.TabStop = false;
134 | this.groupBox3.Text = "基本参数设置";
135 | //
136 | // label4
137 | //
138 | this.label4.AutoSize = true;
139 | this.label4.Location = new System.Drawing.Point(20, 221);
140 | this.label4.Name = "label4";
141 | this.label4.Size = new System.Drawing.Size(53, 12);
142 | this.label4.TabIndex = 58;
143 | this.label4.Text = "图像角度";
144 | //
145 | // ImageAngleComboBox
146 | //
147 | this.ImageAngleComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
148 | this.ImageAngleComboBox.Location = new System.Drawing.Point(119, 218);
149 | this.ImageAngleComboBox.Name = "ImageAngleComboBox";
150 | this.ImageAngleComboBox.Size = new System.Drawing.Size(123, 20);
151 | this.ImageAngleComboBox.TabIndex = 57;
152 | this.ImageAngleComboBox.SelectedIndexChanged += new System.EventHandler(this.ImageAngleComboBox_SelectedIndexChanged);
153 | //
154 | // btnTestOut
155 | //
156 | this.btnTestOut.Location = new System.Drawing.Point(271, 265);
157 | this.btnTestOut.Name = "btnTestOut";
158 | this.btnTestOut.Size = new System.Drawing.Size(114, 23);
159 | this.btnTestOut.TabIndex = 56;
160 | this.btnTestOut.Text = "信号输出测试";
161 | this.btnTestOut.UseVisualStyleBackColor = true;
162 | this.btnTestOut.Click += new System.EventHandler(this.btnTestOut_Click);
163 | //
164 | // btnResetOutLineTime
165 | //
166 | this.btnResetOutLineTime.BackColor = System.Drawing.SystemColors.Control;
167 | this.btnResetOutLineTime.ForeColor = System.Drawing.SystemColors.ControlText;
168 | this.btnResetOutLineTime.Location = new System.Drawing.Point(522, 177);
169 | this.btnResetOutLineTime.Name = "btnResetOutLineTime";
170 | this.btnResetOutLineTime.Size = new System.Drawing.Size(57, 26);
171 | this.btnResetOutLineTime.TabIndex = 55;
172 | this.btnResetOutLineTime.Text = "重置";
173 | this.btnResetOutLineTime.UseVisualStyleBackColor = false;
174 | this.btnResetOutLineTime.Click += new System.EventHandler(this.btnResetOutLineTime_Click);
175 | //
176 | // UpDownOutLineTime
177 | //
178 | this.UpDownOutLineTime.Location = new System.Drawing.Point(119, 180);
179 | this.UpDownOutLineTime.Maximum = new decimal(new int[] {
180 | 6,
181 | 0,
182 | 0,
183 | 0});
184 | this.UpDownOutLineTime.Minimum = new decimal(new int[] {
185 | 1,
186 | 0,
187 | 0,
188 | 0});
189 | this.UpDownOutLineTime.Name = "UpDownOutLineTime";
190 | this.UpDownOutLineTime.Size = new System.Drawing.Size(80, 21);
191 | this.UpDownOutLineTime.TabIndex = 54;
192 | this.UpDownOutLineTime.Value = new decimal(new int[] {
193 | 1,
194 | 0,
195 | 0,
196 | 0});
197 | this.UpDownOutLineTime.ValueChanged += new System.EventHandler(this.UpDownOutLineTime_ValueChanged);
198 | //
199 | // label3
200 | //
201 | this.label3.AutoSize = true;
202 | this.label3.Location = new System.Drawing.Point(20, 183);
203 | this.label3.Name = "label3";
204 | this.label3.Size = new System.Drawing.Size(77, 12);
205 | this.label3.TabIndex = 53;
206 | this.label3.Text = "输出延时(ms)";
207 | //
208 | // trackBarOutLineTime
209 | //
210 | this.trackBarOutLineTime.Location = new System.Drawing.Point(194, 180);
211 | this.trackBarOutLineTime.Name = "trackBarOutLineTime";
212 | this.trackBarOutLineTime.Size = new System.Drawing.Size(327, 45);
213 | this.trackBarOutLineTime.TabIndex = 52;
214 | this.trackBarOutLineTime.Scroll += new System.EventHandler(this.trackBarOutLineTime_Scroll);
215 | //
216 | // btnResetLineDebouncerTimeAbs
217 | //
218 | this.btnResetLineDebouncerTimeAbs.BackColor = System.Drawing.SystemColors.Control;
219 | this.btnResetLineDebouncerTimeAbs.ForeColor = System.Drawing.SystemColors.ControlText;
220 | this.btnResetLineDebouncerTimeAbs.Location = new System.Drawing.Point(522, 136);
221 | this.btnResetLineDebouncerTimeAbs.Name = "btnResetLineDebouncerTimeAbs";
222 | this.btnResetLineDebouncerTimeAbs.Size = new System.Drawing.Size(57, 26);
223 | this.btnResetLineDebouncerTimeAbs.TabIndex = 51;
224 | this.btnResetLineDebouncerTimeAbs.Text = "重置";
225 | this.btnResetLineDebouncerTimeAbs.UseVisualStyleBackColor = false;
226 | this.btnResetLineDebouncerTimeAbs.Click += new System.EventHandler(this.btnResetLineDebouncerTimeAbs_Click);
227 | //
228 | // UpDownLineDebouncerTimeAbs
229 | //
230 | this.UpDownLineDebouncerTimeAbs.Location = new System.Drawing.Point(119, 140);
231 | this.UpDownLineDebouncerTimeAbs.Maximum = new decimal(new int[] {
232 | 6,
233 | 0,
234 | 0,
235 | 0});
236 | this.UpDownLineDebouncerTimeAbs.Minimum = new decimal(new int[] {
237 | 1,
238 | 0,
239 | 0,
240 | 0});
241 | this.UpDownLineDebouncerTimeAbs.Name = "UpDownLineDebouncerTimeAbs";
242 | this.UpDownLineDebouncerTimeAbs.Size = new System.Drawing.Size(80, 21);
243 | this.UpDownLineDebouncerTimeAbs.TabIndex = 50;
244 | this.UpDownLineDebouncerTimeAbs.Value = new decimal(new int[] {
245 | 1,
246 | 0,
247 | 0,
248 | 0});
249 | this.UpDownLineDebouncerTimeAbs.ValueChanged += new System.EventHandler(this.UpDownLineDebouncerTimeAbs_ValueChanged);
250 | //
251 | // label2
252 | //
253 | this.label2.AutoSize = true;
254 | this.label2.Location = new System.Drawing.Point(20, 144);
255 | this.label2.Name = "label2";
256 | this.label2.Size = new System.Drawing.Size(77, 12);
257 | this.label2.TabIndex = 49;
258 | this.label2.Text = "触发防抖(us)";
259 | //
260 | // trackBarLineDebouncerTimeAbs
261 | //
262 | this.trackBarLineDebouncerTimeAbs.Location = new System.Drawing.Point(194, 144);
263 | this.trackBarLineDebouncerTimeAbs.Name = "trackBarLineDebouncerTimeAbs";
264 | this.trackBarLineDebouncerTimeAbs.Size = new System.Drawing.Size(327, 45);
265 | this.trackBarLineDebouncerTimeAbs.TabIndex = 48;
266 | this.trackBarLineDebouncerTimeAbs.Scroll += new System.EventHandler(this.trackBarLineDebouncerTimeAbs_Scroll);
267 | //
268 | // btnResetTriggerDelayAbs
269 | //
270 | this.btnResetTriggerDelayAbs.BackColor = System.Drawing.SystemColors.Control;
271 | this.btnResetTriggerDelayAbs.ForeColor = System.Drawing.SystemColors.ControlText;
272 | this.btnResetTriggerDelayAbs.Location = new System.Drawing.Point(522, 95);
273 | this.btnResetTriggerDelayAbs.Name = "btnResetTriggerDelayAbs";
274 | this.btnResetTriggerDelayAbs.Size = new System.Drawing.Size(57, 26);
275 | this.btnResetTriggerDelayAbs.TabIndex = 47;
276 | this.btnResetTriggerDelayAbs.Text = "重置";
277 | this.btnResetTriggerDelayAbs.UseVisualStyleBackColor = false;
278 | this.btnResetTriggerDelayAbs.Click += new System.EventHandler(this.btnResetTriggerDelayAbs_Click);
279 | //
280 | // UpDownTriggerDelayAbs
281 | //
282 | this.UpDownTriggerDelayAbs.Location = new System.Drawing.Point(119, 100);
283 | this.UpDownTriggerDelayAbs.Maximum = new decimal(new int[] {
284 | 6,
285 | 0,
286 | 0,
287 | 0});
288 | this.UpDownTriggerDelayAbs.Minimum = new decimal(new int[] {
289 | 1,
290 | 0,
291 | 0,
292 | 0});
293 | this.UpDownTriggerDelayAbs.Name = "UpDownTriggerDelayAbs";
294 | this.UpDownTriggerDelayAbs.Size = new System.Drawing.Size(80, 21);
295 | this.UpDownTriggerDelayAbs.TabIndex = 46;
296 | this.UpDownTriggerDelayAbs.Value = new decimal(new int[] {
297 | 1,
298 | 0,
299 | 0,
300 | 0});
301 | this.UpDownTriggerDelayAbs.ValueChanged += new System.EventHandler(this.UpDownTriggerDelayAbs_ValueChanged);
302 | //
303 | // label1
304 | //
305 | this.label1.AutoSize = true;
306 | this.label1.Location = new System.Drawing.Point(20, 105);
307 | this.label1.Name = "label1";
308 | this.label1.Size = new System.Drawing.Size(77, 12);
309 | this.label1.TabIndex = 45;
310 | this.label1.Text = "触发延时(us)";
311 | //
312 | // trackBarTriggerDelayAbs
313 | //
314 | this.trackBarTriggerDelayAbs.Location = new System.Drawing.Point(194, 103);
315 | this.trackBarTriggerDelayAbs.Name = "trackBarTriggerDelayAbs";
316 | this.trackBarTriggerDelayAbs.Size = new System.Drawing.Size(327, 45);
317 | this.trackBarTriggerDelayAbs.TabIndex = 44;
318 | this.trackBarTriggerDelayAbs.Scroll += new System.EventHandler(this.trackBarTriggerDelayAbs_Scroll);
319 | //
320 | // btnSave
321 | //
322 | this.btnSave.Location = new System.Drawing.Point(492, 265);
323 | this.btnSave.Name = "btnSave";
324 | this.btnSave.Size = new System.Drawing.Size(75, 23);
325 | this.btnSave.TabIndex = 43;
326 | this.btnSave.Text = "保存";
327 | this.btnSave.UseVisualStyleBackColor = true;
328 | this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
329 | //
330 | // btnResetGain
331 | //
332 | this.btnResetGain.BackColor = System.Drawing.SystemColors.Control;
333 | this.btnResetGain.ForeColor = System.Drawing.SystemColors.ControlText;
334 | this.btnResetGain.Location = new System.Drawing.Point(522, 54);
335 | this.btnResetGain.Name = "btnResetGain";
336 | this.btnResetGain.Size = new System.Drawing.Size(57, 26);
337 | this.btnResetGain.TabIndex = 42;
338 | this.btnResetGain.Text = "重置";
339 | this.btnResetGain.UseVisualStyleBackColor = false;
340 | this.btnResetGain.Click += new System.EventHandler(this.btnResetGain_Click);
341 | //
342 | // btnResetShutter
343 | //
344 | this.btnResetShutter.BackColor = System.Drawing.SystemColors.Control;
345 | this.btnResetShutter.ForeColor = System.Drawing.SystemColors.ControlText;
346 | this.btnResetShutter.Location = new System.Drawing.Point(522, 13);
347 | this.btnResetShutter.Name = "btnResetShutter";
348 | this.btnResetShutter.Size = new System.Drawing.Size(57, 26);
349 | this.btnResetShutter.TabIndex = 41;
350 | this.btnResetShutter.Text = "重置";
351 | this.btnResetShutter.UseVisualStyleBackColor = false;
352 | this.btnResetShutter.Click += new System.EventHandler(this.btnResetShutter_Click);
353 | //
354 | // UpDownGain
355 | //
356 | this.UpDownGain.Location = new System.Drawing.Point(119, 60);
357 | this.UpDownGain.Maximum = new decimal(new int[] {
358 | 6,
359 | 0,
360 | 0,
361 | 0});
362 | this.UpDownGain.Minimum = new decimal(new int[] {
363 | 1,
364 | 0,
365 | 0,
366 | 0});
367 | this.UpDownGain.Name = "UpDownGain";
368 | this.UpDownGain.Size = new System.Drawing.Size(80, 21);
369 | this.UpDownGain.TabIndex = 33;
370 | this.UpDownGain.Value = new decimal(new int[] {
371 | 1,
372 | 0,
373 | 0,
374 | 0});
375 | this.UpDownGain.ValueChanged += new System.EventHandler(this.UpDownGain_ValueChanged);
376 | //
377 | // UpDownShutter
378 | //
379 | this.UpDownShutter.Increment = new decimal(new int[] {
380 | 100,
381 | 0,
382 | 0,
383 | 0});
384 | this.UpDownShutter.Location = new System.Drawing.Point(119, 20);
385 | this.UpDownShutter.Maximum = new decimal(new int[] {
386 | 6,
387 | 0,
388 | 0,
389 | 0});
390 | this.UpDownShutter.Minimum = new decimal(new int[] {
391 | 1,
392 | 0,
393 | 0,
394 | 0});
395 | this.UpDownShutter.Name = "UpDownShutter";
396 | this.UpDownShutter.Size = new System.Drawing.Size(80, 21);
397 | this.UpDownShutter.TabIndex = 32;
398 | this.UpDownShutter.Value = new decimal(new int[] {
399 | 1,
400 | 0,
401 | 0,
402 | 0});
403 | this.UpDownShutter.ValueChanged += new System.EventHandler(this.UpDownShutter_ValueChanged);
404 | //
405 | // frmCameraSetting
406 | //
407 | this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
408 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
409 | this.ClientSize = new System.Drawing.Size(637, 328);
410 | this.Controls.Add(this.groupBox3);
411 | this.Name = "frmCameraSetting";
412 | this.Text = "相机参数设定";
413 | this.Load += new System.EventHandler(this.frmCameraSetting_Load);
414 | ((System.ComponentModel.ISupportInitialize)(this.trackBarShutter)).EndInit();
415 | ((System.ComponentModel.ISupportInitialize)(this.trackBarGain)).EndInit();
416 | this.groupBox3.ResumeLayout(false);
417 | this.groupBox3.PerformLayout();
418 | ((System.ComponentModel.ISupportInitialize)(this.UpDownOutLineTime)).EndInit();
419 | ((System.ComponentModel.ISupportInitialize)(this.trackBarOutLineTime)).EndInit();
420 | ((System.ComponentModel.ISupportInitialize)(this.UpDownLineDebouncerTimeAbs)).EndInit();
421 | ((System.ComponentModel.ISupportInitialize)(this.trackBarLineDebouncerTimeAbs)).EndInit();
422 | ((System.ComponentModel.ISupportInitialize)(this.UpDownTriggerDelayAbs)).EndInit();
423 | ((System.ComponentModel.ISupportInitialize)(this.trackBarTriggerDelayAbs)).EndInit();
424 | ((System.ComponentModel.ISupportInitialize)(this.UpDownGain)).EndInit();
425 | ((System.ComponentModel.ISupportInitialize)(this.UpDownShutter)).EndInit();
426 | this.ResumeLayout(false);
427 |
428 | }
429 |
430 | #endregion
431 |
432 | private System.Windows.Forms.TrackBar trackBarShutter;
433 | private System.Windows.Forms.TrackBar trackBarGain;
434 | private System.Windows.Forms.Label m_lbl_Shutter;
435 | private System.Windows.Forms.Label m_lbl_Gain;
436 | private System.Windows.Forms.GroupBox groupBox3;
437 | private System.Windows.Forms.NumericUpDown UpDownShutter;
438 | private System.Windows.Forms.NumericUpDown UpDownGain;
439 | private System.Windows.Forms.Button btnResetGain;
440 | private System.Windows.Forms.Button btnResetShutter;
441 | private System.Windows.Forms.Button btnSave;
442 | private System.Windows.Forms.Button btnResetTriggerDelayAbs;
443 | private System.Windows.Forms.NumericUpDown UpDownTriggerDelayAbs;
444 | private System.Windows.Forms.Label label1;
445 | private System.Windows.Forms.TrackBar trackBarTriggerDelayAbs;
446 | private System.Windows.Forms.Button btnResetLineDebouncerTimeAbs;
447 | private System.Windows.Forms.NumericUpDown UpDownLineDebouncerTimeAbs;
448 | private System.Windows.Forms.Label label2;
449 | private System.Windows.Forms.TrackBar trackBarLineDebouncerTimeAbs;
450 | private System.Windows.Forms.Button btnResetOutLineTime;
451 | private System.Windows.Forms.NumericUpDown UpDownOutLineTime;
452 | private System.Windows.Forms.Label label3;
453 | private System.Windows.Forms.TrackBar trackBarOutLineTime;
454 | private System.Windows.Forms.Button btnTestOut;
455 | private System.Windows.Forms.Label label4;
456 | private System.Windows.Forms.ComboBox ImageAngleComboBox;
457 | }
458 | }
--------------------------------------------------------------------------------
/Yoga.Camera/frmCameraSetting.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Diagnostics;
3 | using System.Windows.Forms;
4 |
5 | namespace Yoga.Camera
6 | {
7 | public partial class frmCameraSetting : Form
8 | {
9 | private Camera.CameraBase camera1;
10 | bool settingLocck = false;
11 | public frmCameraSetting(Camera.CameraBase camera)
12 | {
13 | InitializeComponent();
14 | this.camera1 = camera;
15 | }
16 |
17 | private void frmCameraSetting_Load(object sender, EventArgs e)
18 | {
19 | settingLocck = true;
20 | try
21 | {
22 | if (camera1 is Camera.DirectShowCamera)
23 | {
24 | trackBarShutter.Enabled = false;
25 | UpDownShutter.Enabled = false;
26 | btnResetShutter.Enabled = false;
27 |
28 | trackBarGain.Enabled = false;
29 | UpDownGain.Enabled = false;
30 | btnResetGain.Enabled = false;
31 |
32 | trackBarTriggerDelayAbs.Enabled = false;
33 | UpDownTriggerDelayAbs.Enabled = false;
34 | btnResetTriggerDelayAbs.Enabled = false;
35 |
36 | trackBarLineDebouncerTimeAbs.Enabled = false;
37 | UpDownLineDebouncerTimeAbs.Enabled = false;
38 | btnResetLineDebouncerTimeAbs.Enabled = false;
39 |
40 | trackBarOutLineTime.Enabled = false;
41 | UpDownOutLineTime.Enabled = false;
42 | btnResetOutLineTime.Enabled = false;
43 |
44 | }
45 | if (camera1.ShuterCur!=-1)
46 | {
47 | //进度条值放大100倍
48 | trackBarShutter.Minimum = (int)(camera1.ShuterMin);
49 | trackBarShutter.Maximum = (int)(camera1.ShuterMax);
50 |
51 | trackBarShutter.TickFrequency = (trackBarShutter.Maximum - trackBarShutter.Minimum) / 10;
52 |
53 | //设置显示两位有效数字
54 | UpDownShutter.DecimalPlaces = 0;
55 | UpDownShutter.Increment = 100M;
56 | UpDownShutter.Minimum = (int)(camera1.ShuterMin);
57 | UpDownShutter.Maximum = (int)(camera1.ShuterMax);
58 | UpDownShutter.Value = (decimal)(camera1.ShuterCur);
59 | }
60 | else
61 | {
62 | trackBarShutter.Enabled = false;
63 | UpDownShutter.Enabled = false;
64 | btnResetShutter.Enabled = false;
65 | }
66 |
67 | if (camera1.GainCur!=-1)
68 | {
69 | //进度条值与updown相同
70 | trackBarGain.Minimum = (int)(camera1.GainMin);
71 | trackBarGain.Maximum = (int)(camera1.GainMax);
72 |
73 | UpDownGain.Minimum = (int)(camera1.GainMin);
74 | UpDownGain.Maximum = (int)(camera1.GainMax);
75 | UpDownGain.Value = (decimal)(camera1.GainCur);
76 | }
77 | else
78 | {
79 | trackBarGain.Enabled = false;
80 | UpDownGain.Enabled = false;
81 | btnResetGain.Enabled = false;
82 | }
83 | if (camera1.TriggerDelayAbs!=-1)
84 | {
85 | trackBarTriggerDelayAbs.Minimum = (int)camera1.TriggerDelayAbsMin;
86 | trackBarTriggerDelayAbs.Maximum = (int)camera1.TriggerDelayAbsMax;
87 | trackBarTriggerDelayAbs.TickFrequency = (trackBarTriggerDelayAbs.Maximum - trackBarTriggerDelayAbs.Minimum) / 10;
88 |
89 | UpDownTriggerDelayAbs.Minimum = (int)camera1.TriggerDelayAbsMin;
90 | UpDownTriggerDelayAbs.Maximum = (int)camera1.TriggerDelayAbsMax;
91 | UpDownTriggerDelayAbs.Value = (decimal)camera1.TriggerDelayAbs;
92 | }
93 | else
94 | {
95 | trackBarTriggerDelayAbs.Enabled = false;
96 | UpDownTriggerDelayAbs.Enabled = false;
97 | btnResetTriggerDelayAbs.Enabled = false;
98 | }
99 |
100 | if (camera1.LineDebouncerTimeAbs!=-1)
101 | {
102 | trackBarLineDebouncerTimeAbs.Minimum = (int)camera1.LineDebouncerTimeAbsMin;
103 | trackBarLineDebouncerTimeAbs.Maximum = (int)camera1.LineDebouncerTimeAbsMax;
104 | trackBarLineDebouncerTimeAbs.TickFrequency = (trackBarLineDebouncerTimeAbs.Maximum - trackBarLineDebouncerTimeAbs.Minimum) / 10;
105 |
106 | UpDownLineDebouncerTimeAbs.Minimum = (int)camera1.LineDebouncerTimeAbsMin;
107 | UpDownLineDebouncerTimeAbs.Maximum = (int)camera1.LineDebouncerTimeAbsMax;
108 | UpDownLineDebouncerTimeAbs.Value = (decimal)camera1.LineDebouncerTimeAbs;
109 | }
110 | else
111 | {
112 | trackBarLineDebouncerTimeAbs.Enabled = false;
113 | UpDownLineDebouncerTimeAbs.Enabled = false;
114 | btnResetLineDebouncerTimeAbs.Enabled = false;
115 | }
116 |
117 | trackBarOutLineTime.Maximum = 10000;
118 | trackBarOutLineTime.Minimum = 0;
119 | trackBarOutLineTime.TickFrequency = (trackBarOutLineTime.Maximum - trackBarOutLineTime.Minimum) / 10;
120 |
121 |
122 | UpDownOutLineTime.Maximum = 10000;
123 | UpDownOutLineTime.Minimum = 0;
124 | UpDownOutLineTime.Value = (decimal)camera1.OutLineTime;
125 |
126 | ImageAngleComboBox.DataSource = System.Enum.GetNames(typeof(ImageAngle));
127 | ImageAngleComboBox.SelectedIndex = this.ImageAngleComboBox.FindString(camera1.ImageAngle.ToString());
128 |
129 | settingLocck = false;
130 | }
131 | catch (Exception ex)
132 | {
133 | MessageBox.Show("相机参数设置异常:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
134 |
135 | }
136 | }
137 |
138 | private void trackBarShutter_Scroll(object sender, EventArgs e)
139 | {
140 | //获取值
141 | UpDownShutter.Value = trackBarShutter.Value;
142 | UpDownShutter.Refresh();
143 | }
144 |
145 |
146 |
147 | private void trackBarGain_Scroll(object sender, EventArgs e)
148 | {
149 | UpDownGain.Value = trackBarGain.Value;
150 | UpDownGain.Refresh();
151 | }
152 | private void trackBarTriggerDelayAbs_Scroll(object sender, EventArgs e)
153 | {
154 | UpDownTriggerDelayAbs.Value = trackBarTriggerDelayAbs.Value;
155 | UpDownTriggerDelayAbs.Refresh();
156 | }
157 |
158 | private void trackBarLineDebouncerTimeAbs_Scroll(object sender, EventArgs e)
159 | {
160 | UpDownLineDebouncerTimeAbs.Value = trackBarLineDebouncerTimeAbs.Value;
161 | UpDownLineDebouncerTimeAbs.Refresh();
162 | }
163 |
164 | private void trackBarOutLineTime_Scroll(object sender, EventArgs e)
165 | {
166 | UpDownOutLineTime.Value = trackBarOutLineTime.Value;
167 | UpDownOutLineTime.Refresh();
168 | }
169 |
170 |
171 | private void UpDownShutter_ValueChanged(object sender, EventArgs e)
172 | {
173 | trackBarShutter.Value = (int)(UpDownShutter.Value);
174 | if (settingLocck)
175 | {
176 | return;
177 | }
178 |
179 | camera1.ShuterCur = (long)UpDownShutter.Value;
180 | //Debug.WriteLine(string.Format( "调整曝光值记录中 结果值{0}", camera1.ShuterCur));
181 | }
182 |
183 | private void UpDownGain_ValueChanged(object sender, EventArgs e)
184 | {
185 | trackBarGain.Value = (int)UpDownGain.Value;
186 | if (settingLocck)
187 | {
188 | return;
189 | }
190 | camera1.GainCur = (double)UpDownGain.Value;
191 | Debug.WriteLine("调整增益中");
192 | }
193 | private void UpDownTriggerDelayAbs_ValueChanged(object sender, EventArgs e)
194 | {
195 | trackBarTriggerDelayAbs.Value = (int)UpDownTriggerDelayAbs.Value;
196 | if (settingLocck)
197 | {
198 | return;
199 | }
200 | camera1.TriggerDelayAbs = (double)UpDownTriggerDelayAbs.Value;
201 |
202 | }
203 |
204 | private void UpDownLineDebouncerTimeAbs_ValueChanged(object sender, EventArgs e)
205 | {
206 | trackBarLineDebouncerTimeAbs.Value = (int)UpDownLineDebouncerTimeAbs.Value;
207 | if (settingLocck)
208 | {
209 | return;
210 | }
211 | camera1.LineDebouncerTimeAbs = (double)UpDownLineDebouncerTimeAbs.Value;
212 | }
213 |
214 | private void UpDownOutLineTime_ValueChanged(object sender, EventArgs e)
215 | {
216 | trackBarOutLineTime.Value = (int)UpDownOutLineTime.Value;
217 | if (settingLocck)
218 | {
219 | return;
220 | }
221 | camera1.OutLineTime = (double)UpDownOutLineTime.Value;
222 | }
223 | private void btnResetShutter_Click(object sender, EventArgs e)
224 | {
225 | UpDownShutter.Value = (decimal)(camera1.ShotInitValue);
226 | UpDownShutter.Refresh();
227 | }
228 |
229 | private void btnResetGain_Click(object sender, EventArgs e)
230 | {
231 | UpDownGain.Value = (decimal)camera1.GainInitValue;
232 | UpDownGain.Refresh();
233 | }
234 |
235 |
236 |
237 | private void btnResetTriggerDelayAbs_Click(object sender, EventArgs e)
238 | {
239 | UpDownTriggerDelayAbs.Value = (decimal)camera1.TriggerDelayAbsInit;
240 | UpDownTriggerDelayAbs.Refresh();
241 | }
242 |
243 | private void btnResetLineDebouncerTimeAbs_Click(object sender, EventArgs e)
244 | {
245 | UpDownLineDebouncerTimeAbs.Value = (decimal)camera1.LineDebouncerTimeAbsInit;
246 | UpDownLineDebouncerTimeAbs.Refresh();
247 |
248 |
249 | }
250 | private void btnResetOutLineTime_Click(object sender, EventArgs e)
251 | {
252 | UpDownOutLineTime.Value = 1000;
253 | UpDownOutLineTime.Refresh();
254 | }
255 | private void btnSave_Click(object sender, EventArgs e)
256 | {
257 | this.DialogResult = DialogResult.OK;
258 | }
259 |
260 | private void btnTestOut_Click(object sender, EventArgs e)
261 | {
262 | camera1.Output();
263 | }
264 |
265 | private void ImageAngleComboBox_SelectedIndexChanged(object sender, EventArgs e)
266 | {
267 | if (settingLocck)
268 | {
269 | return;
270 | }
271 | camera1.ImageAngle = (ImageAngle)Enum.Parse(typeof(ImageAngle),
272 | ImageAngleComboBox.SelectedItem.ToString(), false);
273 | }
274 | }
275 | }
276 |
--------------------------------------------------------------------------------
/Yoga.Camera/frmCameraSetting.resx:
--------------------------------------------------------------------------------
1 |
2 |
3 |
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 | text/microsoft-resx
110 |
111 |
112 | 2.0
113 |
114 |
115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
116 |
117 |
118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
119 |
120 |
--------------------------------------------------------------------------------