-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathProgram.cs
More file actions
160 lines (144 loc) · 5.85 KB
/
Program.cs
File metadata and controls
160 lines (144 loc) · 5.85 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using Dynamsoft.Core;
using Dynamsoft.CVR;
using Dynamsoft.DBR;
using Dynamsoft.License;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net;
using System.Text.Json;
using System.Threading.Tasks;
namespace ServerSideBarcodeDecoder
{
[ApiController]
public class DecodeController : ControllerBase
{
[HttpPost("decode_barcode")]
public async Task<IActionResult> UploadData()
{
if (Request.ContentType != null)
{
byte[] fileBytes;
if (Request.ContentType.StartsWith("multipart/form-data"))
{
var form = await Request.ReadFormAsync();
var file = form.Files["file"];
if (file != null)
{
using var ms = new MemoryStream();
await file.CopyToAsync(ms);
fileBytes = ms.ToArray();
}
else
{
return BadRequest("No file found in multipart data.");
}
}
else if (Request.ContentType == "application/json")
{
using var ms = new MemoryStream();
await Request.Body.CopyToAsync(ms);
if (TryParseJson(ms.ToArray(), out var doc)
&& doc.RootElement.TryGetProperty("data", out var dataElement)
&& dataElement.TryGetBytesFromBase64(out byte[]? bytes))
{
fileBytes = bytes;
}
else
{
return BadRequest("Invalid JSON");
}
}
else
{
using var ms = new MemoryStream();
await Request.Body.CopyToAsync(ms);
fileBytes = ms.ToArray();
}
(int errorCode, string errorString, List<string> texts) = Program.DecodeBarcodes(fileBytes);
if (errorCode != (int)EnumErrorCode.EC_OK)
{
return BadRequest(new { error_string = errorString, error_code = errorCode });
}
else
{
return Ok(texts);
}
}
return BadRequest("Unsupported content type.");
}
private bool TryParseJson(byte[] bytes, [NotNullWhen(true)] out JsonDocument? document)
{
var reader = new Utf8JsonReader(bytes);
return JsonDocument.TryParseValue(ref reader, out document);
}
}
internal class Program
{
internal static (int, string, List<string>) DecodeBarcodes(byte[] fileBytes)
{
CaptureVisionRouter cvRouter = new CaptureVisionRouter();
CapturedResult result = cvRouter.Capture(fileBytes, PresetTemplate.PT_READ_BARCODES);
int errorCode = 0;
string errorString = string.Empty;
if (result.GetErrorCode() != (int)EnumErrorCode.EC_OK && result.GetErrorCode() != (int)EnumErrorCode.EC_UNSUPPORTED_JSON_KEY_WARNING)
{
errorCode = result.GetErrorCode();
errorString = result.GetErrorString();
Console.WriteLine($"Error: {errorCode}, {errorString}");
}
else
{
errorCode = (int)EnumErrorCode.EC_OK;
errorString = "Successful.";
}
List<string> texts = new List<string>();
DecodedBarcodesResult? barcodeResult = result.GetDecodedBarcodesResult();
if (barcodeResult == null || barcodeResult.GetItems().Length == 0)
{
Console.WriteLine("No barcode detected.");
}
else
{
BarcodeResultItem[] items = barcodeResult.GetItems();
Console.WriteLine($"Decoded {items.Length} barcodes.");
foreach (var item in items)
texts.Add(item.GetText());
}
return (errorCode, errorString, texts);
}
public static void Main(string[] args)
{
System.IO.Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
int errorCode = 1;
string errorMsg;
// Initialize license.
// You can request and extend a trial license from https://www.dynamsoft.com/customer/license/trialLicense?product=dbr&utm_source=samples&package=dotnet
// The string 'DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9' here is a free public trial license. Note that network connection is required for this license to work.
errorCode = LicenseManager.InitLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", out errorMsg);
if (errorCode != (int)EnumErrorCode.EC_OK && errorCode != (int)EnumErrorCode.EC_UNSUPPORTED_JSON_KEY_WARNING)
{
Console.WriteLine("License initialization failed: ErrorCode: " + errorCode + ", ErrorString: " + errorMsg);
}
else
{
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(options =>
{
options.Listen(IPAddress.Parse("127.0.0.1"), 8080);
});
builder.Services.AddControllers();
var app = builder.Build();
app.MapControllers();
app.Run();
}
Console.WriteLine("Press Enter to quit...");
Console.ReadLine();
}
}
}