connect to csharp
This commit is contained in:
parent
0261b94820
commit
b88ff96b32
16 changed files with 336 additions and 63 deletions
35
StitchImg/ImageController.cs
Executable file
35
StitchImg/ImageController.cs
Executable file
|
|
@ -0,0 +1,35 @@
|
|||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace StitchImg;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/image")]
|
||||
public class ImageController : ControllerBase
|
||||
{
|
||||
[HttpPost("generate")]
|
||||
public async Task<IActionResult> Generate([FromBody] ImageSpecification specification)
|
||||
{
|
||||
var serverIp = "127.0.0.1";
|
||||
var port = 8080;
|
||||
|
||||
var tcpClient = new TcpClient();
|
||||
await tcpClient.ConnectAsync(serverIp, port);
|
||||
NetworkStream networkStream = tcpClient.GetStream();
|
||||
var writer = new BinaryWriter(networkStream);
|
||||
|
||||
string rect = specification.CanvasRect;
|
||||
byte[] rectBytes = Encoding.UTF8.GetBytes(rect);
|
||||
writer.Write((ushort)rectBytes.Length);
|
||||
writer.Write(rectBytes);
|
||||
writer.Write(specification.CropOffset[0]);
|
||||
writer.Write(specification.CropSize[0]);
|
||||
writer.Write(specification.CropOffset[1]);
|
||||
writer.Write(specification.CropSize[1]);
|
||||
writer.Write(specification.OutputScale);
|
||||
writer.Flush();
|
||||
|
||||
return File( networkStream, "image/png");
|
||||
}
|
||||
}
|
||||
9
StitchImg/ImageSpecification.cs
Executable file
9
StitchImg/ImageSpecification.cs
Executable file
|
|
@ -0,0 +1,9 @@
|
|||
namespace StitchImg;
|
||||
|
||||
public class ImageSpecification
|
||||
{
|
||||
public string CanvasRect { get; set; }
|
||||
public IReadOnlyList<float> CropOffset { get; set; }
|
||||
public IReadOnlyList<float> CropSize { get; set; }
|
||||
public float OutputScale { get; set; }
|
||||
}
|
||||
51
StitchImg/Program.cs
Executable file
51
StitchImg/Program.cs
Executable file
|
|
@ -0,0 +1,51 @@
|
|||
using Microsoft.OpenApi.Models;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddControllers()
|
||||
.AddNewtonsoftJson(options =>
|
||||
{
|
||||
options.SerializerSettings.ContractResolver = new DefaultContractResolver
|
||||
{
|
||||
NamingStrategy = new SnakeCaseNamingStrategy()
|
||||
};
|
||||
});
|
||||
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen(c =>
|
||||
{
|
||||
c.SchemaFilter<SnakeCaseSchemaFilter>();
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.MapControllers();
|
||||
app.Run();
|
||||
|
||||
public class SnakeCaseSchemaFilter : ISchemaFilter
|
||||
{
|
||||
private readonly SnakeCaseNamingStrategy _namingStrategy = new();
|
||||
|
||||
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
|
||||
{
|
||||
if (schema.Properties == null) return;
|
||||
|
||||
var newProps = new Dictionary<string, OpenApiSchema>();
|
||||
foreach (var prop in schema.Properties)
|
||||
{
|
||||
var snakeName = _namingStrategy.GetPropertyName(prop.Key, false);
|
||||
newProps[snakeName] = prop.Value;
|
||||
}
|
||||
|
||||
schema.Properties = newProps;
|
||||
}
|
||||
}
|
||||
41
StitchImg/Properties/launchSettings.json
Executable file
41
StitchImg/Properties/launchSettings.json
Executable file
|
|
@ -0,0 +1,41 @@
|
|||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:53404",
|
||||
"sslPort": 44379
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5031",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "https://localhost:7060;http://localhost:5031",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
19
StitchImg/StitchImg.csproj
Executable file
19
StitchImg/StitchImg.csproj
Executable file
|
|
@ -0,0 +1,19 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="DotImaging.BitmapInterop" Version="5.2.0" />
|
||||
<PackageReference Include="DotImaging.Image" Version="5.2.0" />
|
||||
<PackageReference Include="DotImaging.IO" Version="5.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.15"/>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
8
StitchImg/appsettings.Development.json
Executable file
8
StitchImg/appsettings.Development.json
Executable file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
9
StitchImg/appsettings.json
Executable file
9
StitchImg/appsettings.json
Executable file
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue