mirror of
http://git.xinwangdao.com/cnnc-embedded-parts-detect/detect.git
synced 2025-06-24 05:24:12 +08:00
162 lines
5.9 KiB
C#
162 lines
5.9 KiB
C#
using detect.gui.Models.Entities;
|
|
using detect.gui.Services.Detect;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Routing;
|
|
using Splat;
|
|
using System.IO;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text.Encodings.Web;
|
|
using System.Text.Json;
|
|
using System.Text.Unicode;
|
|
using SystemDrawing = System.Drawing;
|
|
using detect.gui.Models;
|
|
using Tesseract;
|
|
|
|
namespace detect.gui.Api.System;
|
|
|
|
public class DetectTaskApi
|
|
{
|
|
public DetectTaskApi(IEndpointRouteBuilder? webApp)
|
|
{
|
|
// all
|
|
webApp?.MapGet("/v1/data/task/all", () => Locator.Current.GetService<DetectTaskService>()!.ListAll());
|
|
|
|
// search
|
|
webApp?.MapGet("/v1/data/task/search", ([FromQuery] string? name, [FromQuery]int pageNum = 1, [FromQuery]int pageSize = 10) =>
|
|
Locator.Current.GetService<DetectTaskService>()!.Search(name, pageNum, pageSize));
|
|
|
|
// id
|
|
webApp?.MapGet("/v1/data/task/{id:long}", ([FromRoute] long id) =>
|
|
Locator.Current.GetService<DetectTaskService>()!.ListById(id));
|
|
|
|
// add
|
|
webApp?.MapPost("/v1/data/task/", ([FromBody] DetectTaskEntity entity) =>
|
|
Locator.Current.GetService<DetectTaskService>()!.AddData(entity));
|
|
|
|
// update
|
|
webApp?.MapPut("/v1/data/task/", ([FromBody] DetectTaskEntity entity) =>
|
|
Locator.Current.GetService<DetectTaskService>()!.UpdateData(entity));
|
|
|
|
// delete
|
|
webApp?.MapDelete("/v1/data/task/{id:long}", ([FromRoute] long id) =>
|
|
Locator.Current.GetService<DetectTaskService>()!.DeleteById(id));
|
|
|
|
// ocr
|
|
webApp?.MapPost("/v1/data/task/ocr/", ([FromBody] string base64ImageString) => ImportImageOCR(base64ImageString));
|
|
}
|
|
|
|
private ApiResponse<string?> ImportImageOCR(string base64ImageString)
|
|
{
|
|
var fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
|
|
fileName = GetImage(base64ImageString, fileName);
|
|
var result = "";
|
|
using (var ocrEngine = new TesseractEngine(@".", "chi_sim", EngineMode.Default))
|
|
{
|
|
using (var img = Pix.LoadFromFile(fileName))
|
|
{
|
|
using (var page = ocrEngine.Process(img))
|
|
{
|
|
var text = page.GetText().Replace("\n\n", "");
|
|
var start = text.IndexOf('\n');
|
|
if (start < 0) return new ApiResponse<string?>(0, "success", result);
|
|
text = text.Substring(start).Replace("”", "").Replace("|", "");
|
|
while (text.Contains(" "))
|
|
{
|
|
text = text.Replace(" ", " ");
|
|
}
|
|
|
|
var lines = text.Split('\n');
|
|
var data = new List<dynamic>();
|
|
foreach (var line in lines)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(line)) continue;
|
|
var items = line.Split(" ");
|
|
try
|
|
{
|
|
data.Add(new
|
|
{
|
|
code = items[1],
|
|
type = items[2],
|
|
x = items[3],
|
|
y = items[4],
|
|
center = items[5],
|
|
w = items[2].Split("x")[0],
|
|
h = items[2].Split("x")[1],
|
|
});
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
|
|
if (data.Count > 0)
|
|
result = JsonSerializer.Serialize(data,
|
|
new JsonSerializerOptions
|
|
{ WriteIndented = true, Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) });
|
|
}
|
|
}
|
|
}
|
|
return new ApiResponse<string?>(0, "success", result);
|
|
}
|
|
|
|
private string GetImage(string base64ImageString, string fileName)
|
|
{
|
|
var imageType = SystemDrawing.Imaging.ImageFormat.Gif;
|
|
if (base64ImageString.IndexOf("data:image/jpeg", StringComparison.Ordinal) >= 0)
|
|
{
|
|
imageType = SystemDrawing.Imaging.ImageFormat.Jpeg;
|
|
base64ImageString = base64ImageString.Replace("data:image/jpeg;base64,", "");
|
|
fileName += ".jpg";
|
|
}
|
|
|
|
if (base64ImageString.IndexOf("data:image/png", StringComparison.Ordinal) >= 0)
|
|
{
|
|
imageType = SystemDrawing.Imaging.ImageFormat.Png;
|
|
base64ImageString = base64ImageString.Replace("data:image/png;base64,", "");
|
|
fileName += ".png";
|
|
}
|
|
|
|
if (base64ImageString.IndexOf("data:image/bmp", StringComparison.Ordinal) >= 0)
|
|
{
|
|
imageType = SystemDrawing.Imaging.ImageFormat.Bmp;
|
|
base64ImageString = base64ImageString.Replace("data:image/bmp;base64,", "");
|
|
fileName += ".bmp";
|
|
}
|
|
|
|
if (Equals(imageType, SystemDrawing.Imaging.ImageFormat.Gif))
|
|
{
|
|
return "";
|
|
}
|
|
|
|
try
|
|
{
|
|
var imageBytes = Convert.FromBase64String(base64ImageString.Replace("data:image/jpeg;base64,", ""));
|
|
using var ms = new MemoryStream(imageBytes);
|
|
var image = SystemDrawing.Image.FromStream(ms);
|
|
using (var bmpStream = new MemoryStream())
|
|
{
|
|
image.Save(bmpStream, imageType);
|
|
var bmpBytes = bmpStream.ToArray();
|
|
File.WriteAllBytes(fileName, bmpBytes);
|
|
}
|
|
|
|
image.Dispose();
|
|
return fileName;
|
|
}
|
|
catch
|
|
{
|
|
return "";
|
|
}
|
|
}
|
|
|
|
// class ParamEntity
|
|
// {
|
|
// public string? code { get; set; }
|
|
// public string? type { get; set; }
|
|
// public string? x { get; set; }
|
|
// public string? y { get; set; }
|
|
// public string? center { get; set; }
|
|
// }
|
|
} |