private void Worker_DoWork(object sender, DoWorkEventArgs e) { int image = 0; while (camIsGood) { if (progState == PROG_STATE.CAPTURED) { Thread.Sleep(4); continue; } FSDKCam.GrabFrame(cameraHandle, ref image); var cimg = new FSDK.CImage(image); worker.ReportProgress(0, cimg.Copy().ToCLRImage()); if (progState == PROG_STATE.CAPTURING) { var imgPath = Path.GetFullPath(Path.Combine("Captures", Guid.NewGuid().ToString() + ".jpg")); cimg.Save(imgPath); worker.ReportProgress(1, imgPath); } cimg.Dispose(); FSDK.FreeImage(image); GC.Collect(); Thread.Sleep(16); } FSDKCam.CloseVideoCamera(cameraHandle); }
void webCam() { if (pictureBox1.InvokeRequired) { pictureBox1.Invoke(new MethodInvoker(webCam));//new ThreadStart(webCam)); return; } pictureBox1.Width = Fitems.CamX; pictureBox1.Height = Fitems.CamY; int cameraHandle = Fitems.cameraN; FSDK.TFacePosition facePosition = new FSDK.TFacePosition(); while (!needClose) { Int32 imageHandle = 0; if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(cameraHandle, ref imageHandle)) // grab the current frame from the camera { Application.DoEvents(); continue; } IntPtr hbitmapHandle = IntPtr.Zero; // to store the HBITMAP handle FSDK.SaveImageToHBitmap(imageHandle, ref hbitmapHandle); Image frameImage = Image.FromHbitmap(hbitmapHandle); Graphics gr = Graphics.FromImage(frameImage); fileName = (DateTime.Now.ToLongTimeString() + "__" + DateTime.Now.ToShortDateString()).Replace("/", "-").Replace(" ", "_").Replace(":", "-") + (new Random().Next(new Random().Next(100), new Random().Next(100, 1000))).ToString(); if (FSDK.FSDKE_OK == FSDK.DetectFace(imageHandle, ref facePosition)) { gr.DrawRectangle(Pens.Purple, facePosition.xc - facePosition.w / 2, facePosition.yc - facePosition.w / 2, facePosition.w, facePosition.w); byte[] tempd = new byte[FSDK.TemplateSize]; FSDK.GetFaceTemplateInRegion(imageHandle, ref facePosition, out tempd); // temp = System.Text.Encoding.ASCII.GetString(tempd); timer1.Enabled = true; Fitems.intruder_img = frameImage; if (check_reg(tempd)) { timer1.Enabled = false; timer2.Enabled = false; sp.Stop(); // if (Fitems.intruder_img != null) Fitems.intruder_img.Dispose(); alarm_int = 0; StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; gr.DrawString(_name + " | " + _email, new System.Drawing.Font("Candara", 18), new System.Drawing.SolidBrush(System.Drawing.Color.Purple), facePosition.xc, facePosition.yc + facePosition.w * 0.55f, format); fileName += _name; Fitems.user_name = _name; Fitems.user_id = _userid; res((Bitmap)frameImage); Fitems.uimg = frameImage; Form sForm = new successForm(); needClose = true; this.AddOwnedForm(sForm); sForm.Show(); this.Enabled = false; sp.Stop(); this.Hide(); //return; } //gp = ppp; // timer3.Enabled = true; //Bitmap ppp = (Bitmap)frameImage; //this.Invoke(new System.Threading.ParameterizedThreadStart(res), new object[] { ppp }); //new System.Threading.Thread(new System.Threading.ParameterizedThreadStart()); //res(ppp); //System.Threading.Thread tr = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(res(ppp)),1); // _first_tick++; } else { timer1.Enabled = false; alarm_int = 0; timer2.Enabled = false; sp.Stop(); } try { pictureBox1.Image = frameImage; } catch (ArgumentException argex) { // MessageBox.Show(argex.Message + "\n" + argex.StackTrace); } FSDK.FreeImage(imageHandle); // delete the FSDK image handle DeleteObject(hbitmapHandle); // delete the HBITMAP object GC.Collect(); // collect the garbage after the deletion // make UI controls accessible Application.DoEvents(); } // FSDKCam.CloseVideoCamera(cameraHandle); // FSDKCam.FinalizeCapturing(); }
/* void getStreams() * { * * try * { * HttpWebRequest cam = (HttpWebRequest)WebRequest.Create(new Home().location); * WebResponse resp = cam.GetResponse(); * Stream resps = resp.GetResponseStream(); * * // AsyncCallback ds = new AsyncCallback(callb); * int read = 0, total = 0; * byte[] buff = new byte[7000000]; * //object state = new char[32]; * //IAsyncResult bmpdl = resps.BeginRead(buff,0,3000,ds,state); * while ((read = resps.Read(buff, total, 1000)) != 0) * { * total += read; * } * Bitmap bmp = (Bitmap)Bitmap.FromStream(new MemoryStream(buff, 0, total, false)); * //(Bitmap)(Bitmap.FromStream(resps)); * //(Bitmap)Bitmap.FromStream(new MemoryStream(buff, 0, total, false)); * //new MemoryStream(buff, 0, total)); * resps.Flush(); * recFace(bmp); * resp.Close(); * bmp.Dispose(); * resps.Close(); * resps.Dispose(); * } * catch (HttpListenerException ht) * { * held = true; * MessageBox.Show(ht.Message + " listener exception"); * Application.Exit(); * _no_error = true; * } * catch (ArgumentException ar) * { * held = true; * MessageBox.Show(ar.TargetSite.ToString() + " arg except"); * _no_error = true; * } * catch (IOException ioe) * { * held = true; * MessageBox.Show(ioe.Message + "ioexception"); * _no_error = true; * } * catch (WebException webex) * { * held = true; * MessageBox.Show(webex.Message + "web exception"); * _no_error = true; * } * if (_no_error) * { * canClose = true; * Application.Exit(); * Application.Restart(); * } * }*/ public void recFace(Bitmap ppp) { try { // IntPtr ff = IntPtr.Zero;//ppp.GetHbitmap(); // FSDK.SaveImageToHBitmap(cimg, ref ff); //FSDK.LoadImageFromHBitmap(ref cimg, ff); //FSDK.SetFaceDetectionParameters(false, false, 500); //FSDK.SetFaceDetectionThreshold(2); FSDK.TFacePosition facePosition = new FSDK.TFacePosition(); IntPtr hbitmapHandle = IntPtr.Zero; FSDK.SaveImageToHBitmap(cimg, ref hbitmapHandle); Image ccimg = Image.FromHbitmap(hbitmapHandle); Graphics gr = Graphics.FromImage(ccimg); fileName = (DateTime.Now.ToLongTimeString() + "__" + DateTime.Now.ToShortDateString()).Replace("/", "-").Replace(" ", "_").Replace(":", "-") + (new Random().Next(new Random().Next(100), new Random().Next(100, 1000))).ToString() + "__"; if (FSDK.FSDKE_OK == FSDK.DetectFace(cimg, ref facePosition)) { gr.DrawRectangle(Pens.LightBlue, facePosition.xc - facePosition.w / 2, facePosition.yc - facePosition.w / 2, facePosition.w, facePosition.w); byte[] tempd = new byte[FSDK.TemplateSize]; FSDK.GetFaceTemplateInRegion(cimg, ref facePosition, out tempd); temp = System.Text.Encoding.ASCII.GetString(tempd); if (check_reg(tempd)) { StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; gr.DrawString(_name + " | " + _email, new System.Drawing.Font("Candara", 18), new System.Drawing.SolidBrush(System.Drawing.Color.LightBlue), facePosition.xc, facePosition.yc + facePosition.w * 0.55f, format); fileName += _name; } //gp = ppp; // timer3.Enabled = true; //res(ppp); _first_tick++; } else { if (_first_tick < 20 && _first_tick != 0) { _first_tick++; if (_first_tick == 19) { _first_tick = 0; } res(ppp); } } if (!held) { // pictureBox1.Height = ccimg.Height; // pictureBox1.Width = ccimg.Width; pictureBox1.Image = ccimg; } FSDK.FreeImage(cimg); DeleteObject(hbitmapHandle); GC.Collect(); Application.DoEvents(); // new System.Threading.Thread(new System.Threading.ThreadStart(getStreams)).Start(); } catch (NullReferenceException nulr) { MessageBox.Show(nulr.Message); canClose = true; Application.Exit(); Application.Restart(); } catch (ExternalException exp) { } catch (AccessViolationException acs) { } }
public void recFace(object pppp) { Bitmap ppp = (Bitmap)pppp; if (label1.InvokeRequired) { label1.Invoke(new System.Threading.ParameterizedThreadStart(recFace), new object[] { pppp }); return; } bool error = false; try { IntPtr ff = ppp.GetHbitmap(); FSDK.LoadImageFromHBitmap(ref cimg, ff); FSDK.SetFaceDetectionParameters(false, false, 500); FSDK.SetFaceDetectionThreshold(1); FSDK.TFacePosition facePosition = new FSDK.TFacePosition(); IntPtr hbitmapHandle = IntPtr.Zero; FSDK.SaveImageToHBitmap(cimg, ref hbitmapHandle); Image ccimg = Image.FromHbitmap(hbitmapHandle); Graphics gr = Graphics.FromImage(ccimg); if (FSDK.FSDKE_OK == FSDK.DetectFace(cimg, ref facePosition)) { gr.DrawRectangle(Pens.LightBlue, facePosition.xc - facePosition.w / 2, facePosition.yc - facePosition.w / 2, facePosition.w, facePosition.w); byte[] tempd = new byte[FSDK.TemplateSize]; FSDK.GetFaceTemplateInRegion(cimg, ref facePosition, out tempd); temp = System.Text.Encoding.ASCII.GetString(tempd); if (check_reg(tempd)) { timer2.Enabled = false; StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; gr.DrawString(_name + " | " + _matric + " | " + _room, new System.Drawing.Font("Candara", 18), new System.Drawing.SolidBrush(System.Drawing.Color.LightBlue), facePosition.xc, facePosition.yc + facePosition.w * 0.55f, format); } else { timer2.Enabled = true; } } else { timer2.Enabled = false; label1.Text = "Please Focus on the camera"; } pictureBox1.Height = ccimg.Height; pictureBox1.Width = ccimg.Width; pictureBox1.Image = ccimg; FSDK.FreeImage(cimg); DeleteObject(hbitmapHandle); GC.Collect(); Application.DoEvents(); } catch (NullReferenceException nul) { timer1.Enabled = false; timer1.Dispose(); MessageBox.Show(nul.ToString()); error = true; } catch (ExternalException exer) { } catch (AccessViolationException acs) { } finally { if (error) { canClose = true; Application.Exit(); Application.Restart(); } } }
void webCam() { try { if (pictureBox1.InvokeRequired) { pictureBox1.Invoke(new ThreadStart(webCam)); return; } } catch (Exception) { // webCam(); } try { pictureBox1.Width = Fitems.CamX; pictureBox1.Height = Fitems.CamY; int cameraHandle = Fitems.cameraN; FSDK.TFacePosition facePosition = new FSDK.TFacePosition(); while (!needClose) { Int32 imageHandle = 0; if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(cameraHandle, ref imageHandle)) // grab the current frame from the camera { Application.DoEvents(); continue; } IntPtr hbitmapHandle = IntPtr.Zero; // to store the HBITMAP handle FSDK.SaveImageToHBitmap(imageHandle, ref hbitmapHandle); Image frameImage = Image.FromHbitmap(hbitmapHandle); Graphics gr = Graphics.FromImage(frameImage); fileName = (DateTime.Now.ToLongTimeString() + "__" + DateTime.Now.ToShortDateString()).Replace("/", "-").Replace(" ", "_").Replace(":", "-") + (new Random().Next(new Random().Next(100), new Random().Next(100, 1000))).ToString(); if (FSDK.FSDKE_OK == FSDK.DetectFace(imageHandle, ref facePosition)) { gr.DrawRectangle(Pens.Orange, facePosition.xc - facePosition.w / 2, facePosition.yc - facePosition.w / 2, facePosition.w, facePosition.w); byte[] tempd = new byte[FSDK.TemplateSize]; FSDK.GetFaceTemplateInRegion(imageHandle, ref facePosition, out tempd); drawEyes(new object[] { imageHandle, facePosition, gr }); tempFace = tempd; isFace = true; if (reg) { try { button2.Enabled = true; } catch (InvalidOperationException inv) { } if (check_reg(tempd)) { StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; gr.DrawString(_name + " | " + _email, new System.Drawing.Font("Candara", 18), new System.Drawing.SolidBrush(System.Drawing.Color.Orange), facePosition.xc, facePosition.yc + facePosition.w * 0.55f, format); fileName += _name; } } } else { isFace = false; try { button2.Enabled = false; } catch (InvalidOperationException inv) { } } //recFace(ppp); pictureBox1.Image = frameImage; FSDK.FreeImage(imageHandle); // delete the FSDK image handle DeleteObject(hbitmapHandle); // delete the HBITMAP object GC.Collect(); // collect the garbage after the deletion // make UI controls accessible Application.DoEvents(); } } catch (Exception) { } }