GreatHomeChildcare-2/frmChildCrud.cs

215 lines
6.6 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
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
{
public partial class frmChildCrud : Form
{
const string DEFAULT_PIC_TAG = "DefaultPic";
const string CUSTOM_PIC_TAG = "dickpic";
//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
{
Female = 0,
Male = 1
}
public frmChildCrud()
{
InitializeComponent();
}
/* Event handler on form load.
* Reads the child_id from the admin form if the update button
* was selected on the admin form.
* INPUTS: child_id from the admin form
* OUTPUTS: void
*/
private void frmChildCrud_Load(object sender, EventArgs e)
{
FillGenderComboBox();
int child_id = frmAdminForm.child_id;
//If the update button was selected.
if (child_id > 0)
{
LoadChild(child_id);
}
}
private void FillGenderComboBox()
{
genderComboBox.Items.Add(Gender.Female);
genderComboBox.Items.Add(Gender.Male);
}
/* Load an existing child onto the form for update/delete operations.
* INPUT: integer child_id
* OUTPUT: data to screen
*/
private void LoadChild(int child_id_in)
{
child = SqliteDataAccess.GetChildByID(child_id_in);
Gender genderOut;
// sanity check, though it shouldn't be needed...
if (child == null)
return;
//Load the child data into the form.
idNumericUpDown.Value = child.id;
firstNameTextBox.Text = child.FirstName;
lastNameTextBox.Text = child.LastName;
raceTextBox.Text = child.race;
//Load the gender combo box based from enum value.
//ref: chuck costarella
Enum.TryParse<Gender>(child.gender, out genderOut);
genderComboBox.SelectedItem = genderOut;
addressTextBox.Text = child.address;
photoPictureBox.Image = (child.photo != null) ? ImageWrangler.ByteArrayToImage(child.photo) : Properties.Resources.child;
LoadGuardiansForChild(child);
}
private void LoadGuardiansForChild(Child child_in)
{
dgvGuardians.Rows.Clear();
List<Guardian> guardians = SqliteDataAccess.GetGuardiansByChild(child_in);
foreach (Guardian g in guardians)
{
dgvGuardians.Rows.Add(g.id, g.LastName, g.FirstName, g.PhoneNumber, g.EmailAddress);
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
Close();
}
private void btnStartCam_Click(object sender, EventArgs e)
{
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)
{
MessageBox.Show("From disk");
}
private void btnAddGuardian_Click(object sender, EventArgs e)
{
MessageBox.Show("Add guardian");
}
private void btnEditGuardian_Click(object sender, EventArgs e)
{
MessageBox.Show("Edit Guardian");
}
private void btnDeleteGuardian_Click(object sender, EventArgs e)
{
MessageBox.Show("Delete Guardian");
}
private void btnSave_Click(object sender, EventArgs e)
{
MessageBox.Show("Save and close");
}
}
}