From bdeb116e14011668362df5ae3f63b6cf0f362871 Mon Sep 17 00:00:00 2001
From: kougyokugentou <41278462+kougyokugentou@users.noreply.github.com>
Date: Fri, 22 Jan 2021 21:35:23 -0800
Subject: [PATCH] Add code to take photo from camera, needs fine-tuning
---
frmChildCrud.Designer.cs | 132 ++++++++++++++++++++++++---------------
frmChildCrud.cs | 86 ++++++++++++++++++++++++-
frmChildCrud.resx | 58 +++++++++++------
3 files changed, 202 insertions(+), 74 deletions(-)
diff --git a/frmChildCrud.Designer.cs b/frmChildCrud.Designer.cs
index 538a057..ab993fb 100644
--- a/frmChildCrud.Designer.cs
+++ b/frmChildCrud.Designer.cs
@@ -34,9 +34,11 @@
System.Windows.Forms.Label raceLabel;
System.Windows.Forms.Label genderLabel;
System.Windows.Forms.Label photoLabel;
- System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmChildCrud));
System.Windows.Forms.Label addressLabel;
+ System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmChildCrud));
+ this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.firstNameTextBox = new System.Windows.Forms.TextBox();
+ this.childBindingSource = new System.Windows.Forms.BindingSource(this.components);
this.lastNameTextBox = new System.Windows.Forms.TextBox();
this.raceTextBox = new System.Windows.Forms.TextBox();
this.genderComboBox = new System.Windows.Forms.ComboBox();
@@ -49,29 +51,30 @@
this.PhoneNumber = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.EmailAddress = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.btnAddGuardian = new System.Windows.Forms.Button();
- this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.btnEditGuardian = new System.Windows.Forms.Button();
this.btnDeleteGuardian = new System.Windows.Forms.Button();
- this.btnPhotoFromCam = new System.Windows.Forms.Button();
+ this.btnStartCam = new System.Windows.Forms.Button();
this.btnPhotoFromDisk = new System.Windows.Forms.Button();
this.btnSave = new System.Windows.Forms.Button();
this.photoPictureBox = new System.Windows.Forms.PictureBox();
this.btnCancel = new System.Windows.Forms.Button();
this.idNumericUpDown = new System.Windows.Forms.NumericUpDown();
this.openFileDialogPhotoFromDisk = new System.Windows.Forms.OpenFileDialog();
- this.childBindingSource = new System.Windows.Forms.BindingSource(this.components);
this.frmChildCrudBindingSource = new System.Windows.Forms.BindingSource(this.components);
+ this.photoPanel = new System.Windows.Forms.Panel();
+ this.btnTakePhoto = new System.Windows.Forms.Button();
firstNameLabel = new System.Windows.Forms.Label();
lastNameLabel = new System.Windows.Forms.Label();
raceLabel = new System.Windows.Forms.Label();
genderLabel = new System.Windows.Forms.Label();
photoLabel = new System.Windows.Forms.Label();
addressLabel = new System.Windows.Forms.Label();
+ ((System.ComponentModel.ISupportInitialize)(this.childBindingSource)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dgvGuardians)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.photoPictureBox)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.idNumericUpDown)).BeginInit();
- ((System.ComponentModel.ISupportInitialize)(this.childBindingSource)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.frmChildCrudBindingSource)).BeginInit();
+ this.photoPanel.SuspendLayout();
this.SuspendLayout();
//
// firstNameLabel
@@ -119,6 +122,31 @@
photoLabel.TabIndex = 21;
photoLabel.Text = "photo:";
//
+ // addressLabel
+ //
+ addressLabel.AutoSize = true;
+ addressLabel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
+ addressLabel.ImageIndex = 1;
+ addressLabel.ImageList = this.imageList1;
+ addressLabel.Location = new System.Drawing.Point(12, 165);
+ addressLabel.Name = "addressLabel";
+ addressLabel.Size = new System.Drawing.Size(84, 17);
+ addressLabel.TabIndex = 9;
+ addressLabel.Text = " Address:";
+ //
+ // imageList1
+ //
+ this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
+ this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
+ this.imageList1.Images.SetKeyName(0, "AddButton_16x.png");
+ this.imageList1.Images.SetKeyName(1, "Address_16x.png");
+ this.imageList1.Images.SetKeyName(2, "Camera_16x.png");
+ this.imageList1.Images.SetKeyName(3, "Cancel_16x.png");
+ this.imageList1.Images.SetKeyName(4, "DeleteUser_16x.png");
+ this.imageList1.Images.SetKeyName(5, "Edit_16x.png");
+ this.imageList1.Images.SetKeyName(6, "SaveClose_16x.png");
+ this.imageList1.Images.SetKeyName(7, "OpenfileDialog_16x.png");
+ //
// firstNameTextBox
//
this.firstNameTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.childBindingSource, "FirstName", true));
@@ -127,6 +155,10 @@
this.firstNameTextBox.Size = new System.Drawing.Size(100, 22);
this.firstNameTextBox.TabIndex = 2;
//
+ // childBindingSource
+ //
+ this.childBindingSource.DataSource = typeof(GreatHomeChildcare.Models.Child);
+ //
// lastNameTextBox
//
this.lastNameTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.childBindingSource, "LastName", true));
@@ -243,19 +275,6 @@
this.btnAddGuardian.UseVisualStyleBackColor = true;
this.btnAddGuardian.Click += new System.EventHandler(this.btnAddGuardian_Click);
//
- // imageList1
- //
- this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
- this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
- this.imageList1.Images.SetKeyName(0, "AddButton_16x.png");
- this.imageList1.Images.SetKeyName(1, "Address_16x.png");
- this.imageList1.Images.SetKeyName(2, "Camera_16x.png");
- this.imageList1.Images.SetKeyName(3, "Cancel_16x.png");
- this.imageList1.Images.SetKeyName(4, "DeleteUser_16x.png");
- this.imageList1.Images.SetKeyName(5, "Edit_16x.png");
- this.imageList1.Images.SetKeyName(6, "SaveClose_16x.png");
- this.imageList1.Images.SetKeyName(7, "OpenfileDialog_16x.png");
- //
// btnEditGuardian
//
this.btnEditGuardian.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
@@ -282,25 +301,25 @@
this.btnDeleteGuardian.UseVisualStyleBackColor = true;
this.btnDeleteGuardian.Click += new System.EventHandler(this.btnDeleteGuardian_Click);
//
- // btnPhotoFromCam
+ // btnStartCam
//
- this.btnPhotoFromCam.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
- this.btnPhotoFromCam.ImageIndex = 2;
- this.btnPhotoFromCam.ImageList = this.imageList1;
- this.btnPhotoFromCam.Location = new System.Drawing.Point(325, 204);
- this.btnPhotoFromCam.Name = "btnPhotoFromCam";
- this.btnPhotoFromCam.Size = new System.Drawing.Size(146, 42);
- this.btnPhotoFromCam.TabIndex = 18;
- this.btnPhotoFromCam.Text = "From Camera";
- this.btnPhotoFromCam.UseVisualStyleBackColor = true;
- this.btnPhotoFromCam.Click += new System.EventHandler(this.btnPhotoFromCam_Click);
+ this.btnStartCam.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
+ this.btnStartCam.ImageIndex = 2;
+ this.btnStartCam.ImageList = this.imageList1;
+ this.btnStartCam.Location = new System.Drawing.Point(325, 204);
+ this.btnStartCam.Name = "btnStartCam";
+ this.btnStartCam.Size = new System.Drawing.Size(146, 42);
+ this.btnStartCam.TabIndex = 18;
+ this.btnStartCam.Text = "Start Camera";
+ this.btnStartCam.UseVisualStyleBackColor = true;
+ this.btnStartCam.Click += new System.EventHandler(this.btnStartCam_Click);
//
// btnPhotoFromDisk
//
this.btnPhotoFromDisk.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.btnPhotoFromDisk.ImageIndex = 7;
this.btnPhotoFromDisk.ImageList = this.imageList1;
- this.btnPhotoFromDisk.Location = new System.Drawing.Point(323, 263);
+ this.btnPhotoFromDisk.Location = new System.Drawing.Point(325, 263);
this.btnPhotoFromDisk.Name = "btnPhotoFromDisk";
this.btnPhotoFromDisk.Size = new System.Drawing.Size(147, 41);
this.btnPhotoFromDisk.TabIndex = 19;
@@ -324,8 +343,9 @@
// photoPictureBox
//
this.photoPictureBox.DataBindings.Add(new System.Windows.Forms.Binding("Image", this.childBindingSource, "photo", true));
+ this.photoPictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.photoPictureBox.Image = global::GreatHomeChildcare.Properties.Resources.child;
- this.photoPictureBox.Location = new System.Drawing.Point(323, 21);
+ this.photoPictureBox.Location = new System.Drawing.Point(0, 0);
this.photoPictureBox.Name = "photoPictureBox";
this.photoPictureBox.Size = new System.Drawing.Size(156, 161);
this.photoPictureBox.TabIndex = 22;
@@ -355,42 +375,49 @@
this.idNumericUpDown.TabStop = false;
this.idNumericUpDown.Visible = false;
//
- // addressLabel
- //
- addressLabel.AutoSize = true;
- addressLabel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
- addressLabel.ImageIndex = 1;
- addressLabel.ImageList = this.imageList1;
- addressLabel.Location = new System.Drawing.Point(12, 165);
- addressLabel.Name = "addressLabel";
- addressLabel.Size = new System.Drawing.Size(84, 17);
- addressLabel.TabIndex = 9;
- addressLabel.Text = " Address:";
- //
// openFileDialogPhotoFromDisk
//
this.openFileDialogPhotoFromDisk.FileName = "openFileDialog1";
//
- // childBindingSource
- //
- this.childBindingSource.DataSource = typeof(GreatHomeChildcare.Models.Child);
- //
// frmChildCrudBindingSource
//
this.frmChildCrudBindingSource.DataSource = typeof(GreatHomeChildcare.frmChildCrud);
//
+ // photoPanel
+ //
+ this.photoPanel.Controls.Add(this.photoPictureBox);
+ this.photoPanel.Location = new System.Drawing.Point(315, 21);
+ this.photoPanel.Name = "photoPanel";
+ this.photoPanel.Size = new System.Drawing.Size(156, 161);
+ this.photoPanel.TabIndex = 25;
+ //
+ // btnTakePhoto
+ //
+ this.btnTakePhoto.Enabled = false;
+ this.btnTakePhoto.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
+ this.btnTakePhoto.ImageIndex = 2;
+ this.btnTakePhoto.ImageList = this.imageList1;
+ this.btnTakePhoto.Location = new System.Drawing.Point(487, 204);
+ this.btnTakePhoto.Name = "btnTakePhoto";
+ this.btnTakePhoto.Size = new System.Drawing.Size(136, 42);
+ this.btnTakePhoto.TabIndex = 26;
+ this.btnTakePhoto.Text = "Take Photo";
+ this.btnTakePhoto.UseVisualStyleBackColor = true;
+ this.btnTakePhoto.Click += new System.EventHandler(this.btnTakePhoto_Click);
+ //
// frmChildCrud
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(834, 608);
+ this.Controls.Add(this.btnTakePhoto);
+ this.Controls.Add(this.photoPanel);
this.Controls.Add(this.idNumericUpDown);
this.Controls.Add(this.btnCancel);
this.Controls.Add(photoLabel);
- this.Controls.Add(this.photoPictureBox);
this.Controls.Add(this.btnSave);
this.Controls.Add(this.btnPhotoFromDisk);
- this.Controls.Add(this.btnPhotoFromCam);
+ this.Controls.Add(this.btnStartCam);
this.Controls.Add(this.btnDeleteGuardian);
this.Controls.Add(this.btnEditGuardian);
this.Controls.Add(this.btnAddGuardian);
@@ -409,11 +436,12 @@
this.Name = "frmChildCrud";
this.Text = "Child Management : Great Home Childcare";
this.Load += new System.EventHandler(this.frmChildCrud_Load);
+ ((System.ComponentModel.ISupportInitialize)(this.childBindingSource)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dgvGuardians)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.photoPictureBox)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.idNumericUpDown)).EndInit();
- ((System.ComponentModel.ISupportInitialize)(this.childBindingSource)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.frmChildCrudBindingSource)).EndInit();
+ this.photoPanel.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
@@ -432,7 +460,7 @@
private System.Windows.Forms.Button btnAddGuardian;
private System.Windows.Forms.Button btnEditGuardian;
private System.Windows.Forms.Button btnDeleteGuardian;
- private System.Windows.Forms.Button btnPhotoFromCam;
+ private System.Windows.Forms.Button btnStartCam;
private System.Windows.Forms.Button btnPhotoFromDisk;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.Button btnSave;
@@ -446,5 +474,7 @@
private System.Windows.Forms.DataGridViewTextBoxColumn PhoneNumber;
private System.Windows.Forms.DataGridViewTextBoxColumn EmailAddress;
private System.Windows.Forms.OpenFileDialog openFileDialogPhotoFromDisk;
+ private System.Windows.Forms.Panel photoPanel;
+ private System.Windows.Forms.Button btnTakePhoto;
}
}
\ No newline at end of file
diff --git a/frmChildCrud.cs b/frmChildCrud.cs
index c89f206..2a2011a 100644
--- a/frmChildCrud.cs
+++ b/frmChildCrud.cs
@@ -5,9 +5,15 @@ using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
+using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using GreatHomeChildcare.Models;
+using OpenCvSharp;
+using OpenCvSharp.Extensions;
+
+//Refs:
+// https://ourcodeworld.com/articles/read/761/how-to-take-snapshots-with-the-web-camera-with-c-sharp-using-the-opencvsharp-library-in-winforms
namespace GreatHomeChildcare
{
@@ -18,6 +24,46 @@ namespace GreatHomeChildcare
//Global instance of the SqliteDataAccess object.
SqliteDataAccess SqliteDataAccess = new SqliteDataAccess();
+ Child child = new Child();
+
+ //Smile, you're on candid camera
+ VideoCapture capture;
+ Mat frame;
+ Bitmap image;
+ private Thread camera;
+ bool isCameraRunning = false;
+
+ #region camera
+ //https://ourcodeworld.com/articles/read/761/how-to-take-snapshots-with-the-web-camera-with-c-sharp-using-the-opencvsharp-library-in-winforms
+ // Declare required methods
+ private void CaptureCamera()
+ {
+ camera = new Thread(new ThreadStart(CaptureCameraCallback));
+ camera.Start();
+ }
+
+ private void CaptureCameraCallback()
+ {
+ frame = new Mat();
+ capture = new VideoCapture(0);
+ capture.Open(0);
+
+ if (capture.IsOpened())
+ {
+ while (isCameraRunning)
+ {
+
+ capture.Read(frame);
+ image = BitmapConverter.ToBitmap(frame);
+ if (photoPictureBox.Image != null)
+ {
+ photoPictureBox.Image.Dispose();
+ }
+ photoPictureBox.Image = image;
+ }
+ }
+ }
+ #endregion
enum Gender
{
@@ -61,7 +107,7 @@ namespace GreatHomeChildcare
*/
private void LoadChild(int child_id_in)
{
- Child child = SqliteDataAccess.GetChildByID(child_id_in);
+ child = SqliteDataAccess.GetChildByID(child_id_in);
Gender genderOut;
// sanity check, though it shouldn't be needed...
@@ -101,9 +147,43 @@ namespace GreatHomeChildcare
Close();
}
- private void btnPhotoFromCam_Click(object sender, EventArgs e)
+ private void btnStartCam_Click(object sender, EventArgs e)
{
- MessageBox.Show("From cam");
+ if (btnStartCam.Text.StartsWith("Start"))
+ {
+ btnTakePhoto.Enabled = true;
+ CaptureCamera();
+ btnStartCam.Text = "Stop Camera";
+ isCameraRunning = true;
+ }
+ else
+ {
+ btnTakePhoto.Enabled = false;
+ capture.Release();
+ btnStartCam.Text = "Start Camera";
+ isCameraRunning = false;
+ }
+ }
+
+ private void btnTakePhoto_Click(object sender, EventArgs e)
+ {
+ if (isCameraRunning)
+ {
+ // Take snapshot of the current image generate by OpenCV in the Picture Box
+ Bitmap snapshot = new Bitmap(photoPictureBox.Image);
+
+ //TODO: Do something with the new snapshot, desired state is to stick
+ //whatever image is there on "take photo".
+
+ // Save in some directory
+ // in this example, we'll generate a random filename e.g 47059681-95ed-4e95-9b50-320092a3d652.png
+ // snapshot.Save(@"C:\Users\sdkca\Desktop\mysnapshot.png", ImageFormat.Png);
+ //snapshot.Save(string.Format(@"C:\Users\sdkca\Desktop\{0}.png", Guid.NewGuid()), ImageFormat.Png);
+ }
+ else
+ {
+ MessageBox.Show("Cannot take picture without starting the camera!", "Great Home Childcare", MessageBoxButtons.OK, MessageBoxIcon.None);
+ }
}
private void btnPhotoFromDisk_Click(object sender, EventArgs e)
diff --git a/frmChildCrud.resx b/frmChildCrud.resx
index 7c182a7..342573f 100644
--- a/frmChildCrud.resx
+++ b/frmChildCrud.resx
@@ -132,23 +132,8 @@
False
-
- 17, 17
-
-
- True
-
-
- True
-
-
- True
-
-
- True
-
-
- True
+
+ False
205, 17
@@ -158,7 +143,7 @@
AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w
LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0
ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAACO
- DgAAAk1TRnQBSQFMAgEBCAEAAYABAAGAAQABEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo
+ DgAAAk1TRnQBSQFMAgEBCAEAAZgBAAGYAQABEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo
AwABQAMAATADAAEBAQABCAYAAQwYAAGAAgABgAMAAoABAAGAAwABgAEAAYABAAKAAgADwAEAAcAB3AHA
AQAB8AHKAaYBAAEzBQABMwEAATMBAAEzAQACMwIAAxYBAAMcAQADIgEAAykBAANVAQADTQEAA0IBAAM5
AQABgAF8Af8BAAJQAf8BAAGTAQAB1gEAAf8B7AHMAQABxgHWAe8BAAHWAucBAAGQAakBrQIAAf8BMwMA
@@ -223,8 +208,41 @@
AeMBwwX/Ae8B9ws=
-
- False
+
+ 17, 17
+
+
+ 17, 17
+
+
+ True
+
+
+ True
+
+
+ True
+
+
+ True
+
+
+ True
+
+
+ True
+
+
+ True
+
+
+ True
+
+
+ True
+
+
+ True
581, 17