Files
SharpTesseract/TestVideo/Form1.cs
2022-10-19 16:14:02 +02:00

150 lines
4.9 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.OCR;
using Emgu.CV.Structure;
using System.IO;
namespace TestVideo
{
public partial class Form1 : Form
{
public Screen[] screens;
List<string> screenNames;
Extraction extraction;
int currentZoneIndex;
List<Zone.ZoneType> zoneTypes;
bool creatingZone;
Point[] newZonePoints;
public Form1()
{
InitializeComponent();
screens = Screen.AllScreens;
extraction = new Extraction(screens[0]);
currentZoneIndex = -1;
screenNames = new List<string>();
foreach (Screen scr in screens)
{
screenNames.Add(scr.DeviceName);
}
lsbScreens.DataSource = screenNames;
zoneTypes = new List<Zone.ZoneType>();
foreach (object type in Enum.GetValues(typeof(Zone.ZoneType)))
{
zoneTypes.Add((Zone.ZoneType)type);
}
lsbZoneTypes.DataSource = zoneTypes;
newZonePoints = new Point[2] {new Point(-1,-1), new Point(-1,-1)};
}
private void btnStart_Click(object sender, EventArgs e)
{
if (tmrScreenshots.Enabled)
{
tmrScreenshots.Enabled = false;
btnStart.Text = "Start recording";
}
else
{
tmrScreenshots.Enabled = true;
btnStart.Text = "Stop recording";
}
}
public void RefreshUi()
{
lsbZones.DataSource = extraction.GetZonesNames();
extraction.DrawZones();
//extraction.UpdateZones();
pbxInput.Image = extraction.ScreenPreview;
}
private void tmrScreenshots_Tick(object sender, EventArgs e)
{
extraction.UpdateZones();
pbxInput.Image = extraction.ScreenPreview;
if(currentZoneIndex != -1)
tbxResult.Text = extraction.GetZoneRawText(currentZoneIndex);
//This is to prevent Ram from exploding
GC.Collect();
}
private void lsbScreens_SelectedIndexChanged(object sender, EventArgs e)
{
extraction.ChangeScreen(screens[lsbScreens.SelectedIndex]);
pbxInput.Image = extraction.ScreenPreview;
}
private void pbxInput_Click(object sender, EventArgs e)
{
if (creatingZone)
{
//Convert coordinates to image coordinates
Rectangle screenSize = extraction.CurrentScreen.Bounds;
float xRatio = (float)screenSize.Width / (float)pbxInput.Width;
float yRatio = (float)screenSize.Height / (float)pbxInput.Height;
Point tmpPoint = pbxInput.PointToClient(MousePosition);
Point newPoint = new Point(Convert.ToInt32((float)tmpPoint.X * xRatio),Convert.ToInt32((float)tmpPoint.Y * yRatio));
if (newZonePoints[0] == new Point(-1,-1))
{
//Its the first point
newZonePoints[0] = newPoint;
btnCreateZone.Text = "Creating Zone 1/2 points selected";
}
else
{
//Its the second point
newZonePoints[1] = newPoint;
btnCreateZone.Text = "Create Zone";
Zone.ZoneType newZoneType = zoneTypes[lsbZoneTypes.SelectedIndex];
Random rnd = new Random();
Point p1 = newZonePoints[0];
Point p2 = newZonePoints[1];
Size newSize = new Size(Math.Max(p1.X,p2.X) - Math.Min(p1.X, p2.X),Math.Max(p1.Y,p2.Y) - Math.Min(p1.Y, p2.Y));
extraction.AddZone(new Rectangle(newZonePoints[0],newSize),newZoneType,"Coucou");
newZonePoints = new Point[2] { new Point(-1, -1), new Point(-1, -1) };
creatingZone = false;
RefreshUi();
}
}
}
private void btnCreateZone_Click(object sender, EventArgs e)
{
creatingZone = !creatingZone;
if (creatingZone)
{
btnCreateZone.Text = "Creating Zone 0/2 points selected";
}
else
{
btnCreateZone.Text = "Create Zone";
}
}
private void lsbZones_SelectedIndexChanged(object sender, EventArgs e)
{
if (extraction.GetZonesNames().Any() && lsbZones.SelectedIndex >= 0)
{
currentZoneIndex = lsbZones.SelectedIndex;
}
}
}
}