-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
129 lines (112 loc) · 4.29 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System.Drawing;
using System.Drawing.Imaging;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Reflection.Metadata;
using System.Runtime.InteropServices;
using Tesseract;
namespace GetCustomerNameFromPrinter_sPlanWindow
{
internal class Program
{
static async Task Main(string[] args)
{
if (args.Length < 3)
{
Console.WriteLine("Usage: ConsoleApp.exe" + args[2] + args[1] + args[0]);
Console.ReadKey();
return;
}
string referenceNum = args[2];
string amount = args[1];
string customerName = GetTextFromScreen(args[0]);
var check = new Check
{
ReferenceNum = double.Parse(referenceNum),
Amount = decimal.Parse(amount),
CustomerName = customerName
};
await PostCheckAsync(check);
}
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public static string GetTextFromScreen(string args)
{
System.Drawing.Rectangle bounds;
IntPtr hWnd = (IntPtr)Convert.ToInt32(args);
RECT rect;
GetWindowRect(hWnd, out rect);
bounds = new System.Drawing.Rectangle(rect.Left + 460, rect.Top + 88, 400, 17);
byte[] byteFile;
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(new System.Drawing.Point(bounds.Left, bounds.Top), System.Drawing.Point.Empty, bounds.Size);
}
ImageConverter converter = new ImageConverter();
byteFile = (byte[])converter.ConvertTo(bitmap, typeof(byte[]));
//using(Image image = Image.FromStream(new
// MemoryStream(byteFile)))
//{
// image.Save(@"C:\Users\Jeff\Downloads\output.png", System.Drawing.Imaging.ImageFormat.Png);
//}
}
string dataPath = @"C:\Users\Jeff\source\repos\GetCustomerNameFromPrinter'sPlanWindow\tessdata\";
using (var engine = new TesseractEngine(dataPath, "eng", EngineMode.Default))
{
using (var img = Pix.LoadFromMemory(byteFile))
{
using (var page = engine.Process(img))
{
string text = page.GetText();
return DelimitString(text);
}
}
}
}
public static string DelimitString(string str)
{
int newlineIndex = str.IndexOf('\n');
if (newlineIndex != -1)
{
return str.Substring(0, newlineIndex);
}
return str;
}
static async Task PostCheckAsync(Check check)
{
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri("http://localhost:6428/api/Checks/"); // Replace with your API's URL
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
var response = await httpClient.PostAsJsonAsync("", check);
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Check created successfully!");
}
else
{
Console.WriteLine($"Failed to create the check. Status code: {response.StatusCode}");
Console.ReadKey();
// Handle error cases here
}
}
}
}
public class Check
{
public double ReferenceNum { get; set; }
public decimal Amount { get; set; }
public string CustomerName { get; set; }
}
}