mirror of https://github.com/ivanch/tcc.git
terminando .NET
This commit is contained in:
parent
f8acb38f99
commit
2390f8fb8d
|
@ -1,3 +1,4 @@
|
|||
.vs
|
||||
bin
|
||||
obj
|
||||
obj
|
||||
*.png
|
|
@ -4,7 +4,7 @@ using TCC.Services;
|
|||
namespace TCC.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("")]
|
||||
[Route("image")]
|
||||
public class ImageController : ControllerBase
|
||||
{
|
||||
private ImageService ImageService { get; set; }
|
||||
|
@ -16,13 +16,13 @@ namespace TCC.Controllers
|
|||
}
|
||||
|
||||
[HttpPost("blur")]
|
||||
public async Task<IActionResult> BlurImage()
|
||||
public async Task<IActionResult> BlurImage([FromQuery] int radius)
|
||||
{
|
||||
MemoryStream mstream = new MemoryStream();
|
||||
await HttpContext.Request.Body.CopyToAsync(mstream);
|
||||
mstream.Position = 0;
|
||||
|
||||
var result = ImageService.BoxBlurImage(mstream, 5);
|
||||
var result = ImageService.BoxBlurImage(mstream, radius);
|
||||
|
||||
var blurredImageStream = new MemoryStream();
|
||||
result.Write(blurredImageStream);
|
||||
|
@ -31,5 +31,38 @@ namespace TCC.Controllers
|
|||
return File(blurredImageStream, "image/png");
|
||||
}
|
||||
|
||||
[HttpGet("load-image")]
|
||||
public async Task<IActionResult> GetSimpleImage()
|
||||
{
|
||||
var result = ImageService.GetSimpleImage();
|
||||
|
||||
var imageStream = new MemoryStream();
|
||||
result.Write(imageStream);
|
||||
imageStream.Position = 0;
|
||||
|
||||
return File(imageStream, "image/png");
|
||||
}
|
||||
|
||||
[HttpGet("load-big-image")]
|
||||
public async Task<IActionResult> GetBigImage()
|
||||
{
|
||||
var result = ImageService.GetBigImage();
|
||||
|
||||
var imageStream = new MemoryStream();
|
||||
result.Write(imageStream);
|
||||
imageStream.Position = 0;
|
||||
|
||||
return File(imageStream, "image/png");
|
||||
}
|
||||
|
||||
[HttpPost("save-big-image")]
|
||||
public async Task<IActionResult> SaveBigImage()
|
||||
{
|
||||
MemoryStream mstream = new MemoryStream();
|
||||
await HttpContext.Request.Body.CopyToAsync(mstream);
|
||||
mstream.Position = 0;
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
using ImageMagick;
|
||||
|
||||
namespace tcc_app
|
||||
{
|
||||
public static class ImageHelper
|
||||
{
|
||||
public static MagickImage SimpleImage;
|
||||
public static MagickImage BigImage;
|
||||
|
||||
static ImageHelper()
|
||||
{
|
||||
SimpleImage = new MagickImage("simpleimage.png");
|
||||
BigImage = new MagickImage("bigimage.png");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
using Microsoft.AspNetCore.Http.Features;
|
||||
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
||||
using TCC.Services;
|
||||
|
||||
namespace TCC
|
||||
|
@ -12,6 +14,12 @@ namespace TCC
|
|||
|
||||
builder.Services.AddControllers();
|
||||
|
||||
builder.Services.Configure<KestrelServerOptions>(options =>
|
||||
{
|
||||
options.Limits.MaxRequestBodySize = int.MaxValue; // if don't set default value is: 30 MB
|
||||
});
|
||||
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.MapControllers();
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using ImageMagick;
|
||||
using tcc_app;
|
||||
|
||||
namespace TCC.Services
|
||||
{
|
||||
|
@ -10,6 +11,14 @@ namespace TCC.Services
|
|||
{
|
||||
var image = new MagickImage(imageStream);
|
||||
var blurredImage = new MagickImage(image);
|
||||
|
||||
blurredImage = BoxBlurImageSeparable(image, blurredImage, radius, 0);
|
||||
blurredImage = BoxBlurImageSeparable(image, blurredImage, 0, radius);
|
||||
return blurredImage;
|
||||
}
|
||||
|
||||
private MagickImage BoxBlurImageSeparable(MagickImage image, MagickImage blurredImage, int radiusX, int radiusY)
|
||||
{
|
||||
var pixels = image.GetPixels();
|
||||
var blurredPixels = blurredImage.GetPixelsUnsafe();
|
||||
|
||||
|
@ -20,9 +29,9 @@ namespace TCC.Services
|
|||
|
||||
long rTotal = 0, gTotal = 0, bTotal = 0;
|
||||
int pixelCount = 0;
|
||||
for (int offsetY = -radius; offsetY <= radius; offsetY++)
|
||||
for (int offsetY = -radiusY; offsetY <= radiusY; offsetY++)
|
||||
{
|
||||
for (int offsetX = -radius; offsetX <= radius; offsetX++)
|
||||
for (int offsetX = -radiusX; offsetX <= radiusX; offsetX++)
|
||||
{
|
||||
int newX = x + offsetX;
|
||||
int newY = y + offsetY;
|
||||
|
@ -45,5 +54,22 @@ namespace TCC.Services
|
|||
|
||||
return blurredImage;
|
||||
}
|
||||
|
||||
public void SaveImage(Stream fileStream)
|
||||
{
|
||||
var file = File.Create("image.png");
|
||||
fileStream.CopyToAsync(file);
|
||||
file.Close();
|
||||
}
|
||||
|
||||
public MagickImage GetSimpleImage()
|
||||
{
|
||||
return ImageHelper.SimpleImage;
|
||||
}
|
||||
|
||||
public MagickImage GetBigImage()
|
||||
{
|
||||
return ImageHelper.BigImage;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue