Files
OCR_TEST/OCR_tester/Zone.cs
T

145 lines
4.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace OCR_tester
{
public class Zone
{
protected Bitmap FullImage;
private List<Zone> _zones;
private List<Window> _windows;
private string _name;
protected Rectangle _bounds;
public List<Zone> Zones { get => _zones; protected set => _zones = value; }
public List<Window> Windows { get => _windows; protected set => _windows = value; }
public Rectangle Bounds { get => _bounds; private set => _bounds = value; }
public Bitmap ZoneImage
{
get
{
Bitmap sample = new Bitmap(Bounds.Width, Bounds.Height);
Graphics g = Graphics.FromImage(sample);
g.DrawImage(FullImage, new Rectangle(0, 0, sample.Width, sample.Height), Bounds, GraphicsUnit.Pixel);
return sample;
}
}
public string Name { get => _name; set => _name = value; }
public Zone(Image fullImage, Rectangle bounds)
{
FullImage = (Bitmap)fullImage;
Init(bounds);
}
public Zone(Bitmap fullImage, Rectangle bounds)
{
FullImage = fullImage;
Init(bounds);
}
private void Init(Rectangle bounds)
{
Bounds = bounds;
Zones = new List<Zone>();
Windows = new List<Window>();
}
public void AddZone(Rectangle bounds)
{
if (Fits(bounds))
Zones.Add(new Zone(ZoneImage, bounds));
}
public virtual void AddWindow(Rectangle bounds)
{
if (Fits(bounds))
Windows.Add(new Window(ZoneImage, bounds));
}
/// <summary>
/// Checks if the given Rectangle fits in the current zone
/// </summary>
/// <param name="InputRectangle">The Rectangle you want to check the fittment</param>
/// <returns></returns>
protected bool Fits(Rectangle inputRectangle)
{
if (inputRectangle.X + inputRectangle.Width > Bounds.Width || inputRectangle.Y + inputRectangle.Height > Bounds.Height || inputRectangle.X < 0 || inputRectangle.Y < 0)
{
return false;
}
else
{
return true;
}
}
public virtual Bitmap Draw()
{
Image img = ZoneImage;
Graphics g = Graphics.FromImage(img);
foreach (Window w in Windows)
{
g.DrawRectangle(Pens.Blue, w.Bounds);
}
return (Bitmap)img;
}
public virtual string ToJSON()
{
string result = "";
result += "\"" + Name + "\":{" + Environment.NewLine;
result += "\t" + "\"x\":" + Bounds.X + "," + Environment.NewLine;
result += "\t" + "\"y\":" + Bounds.Y + "," + Environment.NewLine;
result += "\t" + "\"width\":" + Bounds.Width + "," + Environment.NewLine;
result += "\t" + "\"height\":" + Bounds.Height;
if (Windows.Count != 0)
{
result += "," +Environment.NewLine;
result += "\t" + "\"Windows\":["+Environment.NewLine;
result += "\t\t{" + Environment.NewLine;
int Wcount = 0;
foreach (Window w in Windows)
{
result += "\t\t" + w.ToJSON();
Wcount++;
if (Wcount != Windows.Count)
result += ",";
}
result += "\t\t}"+Environment.NewLine;
result += "\t" + "]"+Environment.NewLine;
}
else
{
result += Environment.NewLine;
}
if (Zones.Count != 0)
{
result += "," + Environment.NewLine;
result += "\t" + "\"Zones\":[" + Environment.NewLine;
result += "\t\t{" + Environment.NewLine;
int Zcount = 0;
//foreach (Zone z in Zones)
//{
result += "\t\t" + Zones[0].ToJSON();
Zcount++;
if (Zcount != Zones.Count)
//result += ",";
//}
result += "\t\t}" + Environment.NewLine;
result += "\t" + "]" + Environment.NewLine;
}
else
{
result += Environment.NewLine;
}
result += "}";
return result;
}
}
}