|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- using AForge.Controls;
- using AForge.Video.DirectShow;
-
- using System;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
-
-
- namespace Common.system
- {
- public static class CameraHelper
- {
- private static FilterInfoCollection _cameraDevices;
- private static VideoCaptureDevice div = null;
- private static VideoSourcePlayer sourcePlayer = new VideoSourcePlayer();
- private static bool _isDisplay = false;
-
- private static bool isSet = false;
-
-
-
-
- public static FilterInfoCollection CameraDevices
- {
- get => _cameraDevices;
- set => _cameraDevices = value;
- }
-
-
-
-
- public static bool IsDisplay
- {
- get => _isDisplay;
- set => _isDisplay = value;
- }
-
-
-
-
- public static VideoSourcePlayer SourcePlayer
- {
- get => sourcePlayer;
- set
- {
- if (_isDisplay)
- {
- sourcePlayer = value;
- isSet = true;
- }
-
- }
- }
-
-
-
- public static void UpdateCameraDevices()
- {
- _cameraDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
- }
-
-
-
-
-
- public static bool SetCameraDevice(int index)
- {
- if (!isSet)
- {
- _isDisplay = false;
- }
-
- if (_cameraDevices.Count <= 0 || index < 0)
- {
- return false;
- }
-
- if (index > _cameraDevices.Count - 1)
- {
- return false;
- }
-
- div = new VideoCaptureDevice(_cameraDevices[index].MonikerString);
- sourcePlayer.VideoSource = div;
- div.Start();
- sourcePlayer.Start();
- return true;
- }
-
-
-
-
-
-
- public static string CaptureImage(string filePath, string fileName = null)
- {
- if (sourcePlayer.VideoSource == null)
- {
- return null;
- }
-
- if (!Directory.Exists(filePath))
- {
- Directory.CreateDirectory(filePath);
- }
- try
- {
- Image bitmap = sourcePlayer.GetCurrentVideoFrame();
- if (bitmap != null)
- {
- if (fileName == null)
- {
- fileName = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
- }
-
- string fullPath = Path.Combine(filePath, fileName + "-cap.jpg");
-
- System.Drawing.Bitmap objNewPic = new System.Drawing.Bitmap(bitmap, 172, 124);
- objNewPic.Save(fullPath, ImageFormat.Jpeg);
-
- bitmap.Dispose();
- objNewPic.Dispose();
- return fullPath;
- }
- return null;
- }
- catch (Exception)
- {
-
- return null;
- }
- }
-
-
-
- public static void CloseDevice()
- {
- if (div != null && div.IsRunning)
- {
- sourcePlayer.Stop();
- div.SignalToStop();
- div = null;
- _cameraDevices = null;
- }
- }
- }
- }
|