C# Remoting İle Webcam Sorunu
-
C# da remoting ile bi proje yaptım en son web cam ekledim fakat webcamin her görüntü aldığında düştüğü vide_NewFrame eventi sonsuza kadar döndüğü için Client kısmından alınan Bitmap imajlarına erişemiyorum. event sonsuz döngüye giriyor yani. kod şöyle :
public bool WebCamStart(int DeviceIndex) { if (DeviceExist) { videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); //string myDevice = videoDevices[0].Name; videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString); videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame); CloseVideoSource(); videoSource.DesiredFrameSize = new Size(640, 480); //videoSource.DesiredFrameRate = 10; videoSource.Start(); return true; } else return false; } public Bitmap lastImg; private void video_NewFrame(object sender, NewFrameEventArgs eventArgs) { Bitmap img = (Bitmap)eventArgs.Frame.Clone(); //in executes infinitely when execution comes here and i cant reach from Cliend side... } public string getFPS() { return videoSource.FramesReceived.ToString(); } public void CloseVideoSource() { if (!(videoSource == null)) if (videoSource.IsRunning) { videoSource.SignalToStop(); videoSource.Stop(); videoSource = null; } } public string getCamList() { string result = "No Device Found"; try { videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); //comboBox1.Items.Clear(); if (videoDevices.Count == 0) throw new ApplicationException(); DeviceExist = true; foreach (FilterInfo device in videoDevices) { //comboBox1.Items.Add(device.Name); result = device.Name; return result; } //comboBox1.SelectedIndex = 0; //make dafault to first cam } catch (ApplicationException) { DeviceExist = false; //comboBox1.Items.Add("No capture device on your system"); return "No capture device on your system"; } return result; }// ve client kısmından böyle çekmek istiyorum fakat olmuyor çünkü Remote classına her gidişte değişken e null değeri atanıyor.
private void timerWebCam_Tick(object sender, EventArgs e) { //lblFPS.Text ="Device Running... " + remObj.getFPS() + " FPS"; pictureBox1.Image = remObj.lastImg; } -
hocam tek amacın webcam den capture yapıp tek resim mi almak
-
yok tek resim değil webcam görüntüsünü sürekli olarak almam gerekiyor
-
mantık olarak bir problem yok.. sonsuz döngü olayı da yok, sürekli veri aktarımı var zaten eventin amacı da bu. bir şey olduğunda tetikleniyor.
kodda dikkatimi çeken bir yer var
//videoSource.DesiredFrameRate = 10;bunu commente almışsın bunu açmalısın ya da değeri arttırmalısın. çünkü hangi aralıklarda değer alacağını belirtiyor. 10 u milisaniye cinsinden alırsak 10 milisaniyede bir event tetikleniyordur. bunu 1000 yaparsan 1 saniyede 1 event tetiklenir. Senin yapacağın iş eventin parametresindeki eventsargs da (orada zaten nasıl alacağın belli bu image i pictureboxda gösterebilirsin, kilitlenmemesi için multi thread yapısı kullanman gerekiyor.
-
up
-
15imde yazdığım ratımın webcam işlemleri içn kullandığım classı, çalışıyor
sonradan gelen edit: ne saçma sapan kod yazıyomuşum lan
DeviceRunning =="true"?true:false nedir amk[Serializable] public partial class CameraDevice { //priv private IntPtr Handle; private Bitmap _LastCaptured; private Size s; private string Run; private string DeviceRunning; //public private int _ID; public int ID { get { return _ID; } } private string _Name; public string Name { get { return _Name; } } private string _Version; public string Version { get { return _Version; } } private bool _IsRunning; public bool IsRunning { get { return _IsRunning; } set { if (_IsRunning == value) return; if (value) StartDevice(new Size(320, 240)); else StopDevice(); _IsRunning = value; } } public delegate void WebCamEventHandler(Bitmap bmp); public event WebCamEventHandler WebCamDataReceived; public static List GetAvalaibleCameraDevices() { List Devices = new List(); for (int i = 0; i < 10; i++) { IntPtr ptr = Marshal.AllocHGlobal(80); IntPtr ptr2 = Marshal.AllocHGlobal(80); if (!capGetDriverDescription(i, ptr, 80, ptr2, 80)) return Devices; CameraDevice dev = new CameraDevice(i, Marshal.PtrToStringAnsi(ptr), Marshal.PtrToStringAnsi(ptr2)); if (dev.StartDevice()) { dev.StopDevice(); Devices.Add(dev); } } return Devices; } private CameraDevice(int id, string name, string version) { this._ID = id; this._Name = name; this._Version = version; } public bool StartDevice() { return StartDevice(new Size(320, 240)); } public bool StartDevice(Size s) { if (_IsRunning) return true; Run = "true"; ThreadPool.QueueUserWorkItem(ThreadPerformer); while (DeviceRunning == null) Thread.Sleep(10); return _IsRunning = DeviceRunning == "true" ? true : false; } public void StopDevice() { Run = "false"; while (DeviceRunning != null) Thread.Sleep(1); } public Bitmap TakePhoto(Size s) { StartDevice(s); Bitmap bmap; while ((bmap = GetCurrentImage()) == null) Thread.Sleep(1); StopDevice(); return bmap; } public Bitmap GetCurrentImage() { return this._LastCaptured; } public byte[] GetCurrentImageBytes() { Bitmap bmp; if ((bmp = GetCurrentImage()) == null) return new byte[0]; using (MemoryStream mStream = new MemoryStream()) { bmp.Save(mStream, ImageFormat.Jpeg); byte[] Array = new byte[mStream.Length]; mStream.Position = 0; mStream.Read(Array, 0, Array.Length); return Array; } } private void ThreadPerformer(object nothing) { Handle = capCreateCaptureWindowA(null, 0, 0, 0, s.Width, s.Height, IntPtr.Zero, 0); if ((DeviceRunning = (1 == SendMessage(Handle, WM_CAP_CONNECT, this.ID, 0) && 1 == SendMessage(Handle, WM_CAP_SET_PREVIEW, 0, 0)).ToString().ToLower()) != "true") return; while (Run == "true") { IntPtr ptr; do { SendMessage(Handle, WM_CAP_GET_FRAME, 0, 0); SendMessage(Handle, WM_CAP_COPY, 0, 0); OpenClipboard(IntPtr.Zero); } while (IntPtr.Zero == (ptr = GetClipboardData(2))); _LastCaptured = Bitmap.FromHbitmap(ptr); if (WebCamDataReceived != null) WebCamDataReceived(_LastCaptured); CloseClipboard(); Thread.Sleep(1); } SendMessage(Handle, WM_CAP_DISCONNECT, 0, 0); this.Handle = IntPtr.Zero; this._IsRunning = false; this.DeviceRunning = null; } }public partial class CameraDevice { //Constants for api private const int WM_USER = 1024; private const int WM_CAP_CONNECT = 1034; private const int WM_CAP_DISCONNECT = 1035; private const int WM_CAP_GET_FRAME = 1084; private const int WM_CAP_COPY = 1054; private const int WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 5; private const int WM_CAP_SET_SCALE = (WM_CAP_START + 53); private const int WM_CAP_SET_CALLBACK_FRAME = (WM_CAP_START + 5); private const int WM_CAP_START = WM_USER; private const int WM_CAP_SET_CALLBACK_VIDEOSTREAM = (WM_CAP_START + 6); private const int WM_CAP_SET_PREVIEW = WM_CAP_START + 50; //Extern methods [DllImport("user32", EntryPoint = "SendMessage")] private static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); [DllImport("avicap32.dll", EntryPoint = "capCreateCaptureWindowA")] private static extern IntPtr capCreateCaptureWindowA(string lpszWindowName, int dwStyle, int X, int Y, int nWidth, int nHeight, IntPtr hwndParent, int nID); [DllImport("avicap32.dll", EntryPoint = "capGetDriverDescriptionA", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] private static extern bool capGetDriverDescription(int dwDriverIndex, IntPtr lpszName, int cbName, IntPtr lpszVer, int cbVer); [DllImport("user32.dll", SetLastError = true)] private static extern bool OpenClipboard(IntPtr hWndNewOwner); [DllImport("user32.dll")] private static extern IntPtr GetClipboardData(uint uFormat); [DllImport("user32.dll", SetLastError = true)] private static extern bool CloseClipboard(); }nessaj tarafından 26/Kas/12 19:44 tarihinde düzenlenmiştir -
abovvv fedooo ne yapmış la bu. resimleri al al sonra bunları video yap her resim 2-5 mb olsun sonra bir iki dakkalık video olsun sana yarım gb. bide onları sıkıştırman mp4 filan yapman lazım.
da böyle bir şeye neden ihtiyaç duydun güvenlik kamerası filanmı yapıyon :):):)
