128 lines
4.6 KiB
C#
128 lines
4.6 KiB
C#
using System.Collections.Generic;
|
|
using System.Drawing.Imaging;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Threading.Tasks;
|
|
using Church.Net.Entity;
|
|
using Church.Net.Utility;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using QRCoder;
|
|
using SixLabors.Fonts;
|
|
using SixLabors.ImageSharp;
|
|
using SixLabors.ImageSharp.Drawing.Processing;
|
|
using SixLabors.ImageSharp.Formats;
|
|
using SixLabors.ImageSharp.Processing;
|
|
using WebAPI.Logics.Interface;
|
|
|
|
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
|
|
|
|
namespace WebAPI.Controllers
|
|
{
|
|
[Route("[controller]/[action]")]
|
|
[ApiController]
|
|
public class BestController : ApiControllerBase<HappinessBEST>
|
|
{
|
|
private readonly ICrudLogic<HappinessBEST> logic;
|
|
private readonly ICrudLogic<HappinessGroup> groupLogic;
|
|
private readonly ICrudLogic<HappinessWeek> weekLogic;
|
|
|
|
public BestController(
|
|
ICrudLogic<HappinessBEST> logic,
|
|
ICrudLogic<HappinessGroup> groupLogic,
|
|
ICrudLogic<HappinessWeek> weekLogic
|
|
|
|
) : base(logic)
|
|
{
|
|
this.logic = logic;
|
|
this.groupLogic = groupLogic;
|
|
this.weekLogic = weekLogic;
|
|
}
|
|
|
|
// GET api/<BestController>/5
|
|
public override Task<IEnumerable<HappinessBEST>> GetAll()
|
|
{
|
|
return base.GetAll();
|
|
}
|
|
|
|
public override Task<HappinessBEST> GetById(string id)
|
|
{
|
|
return Task<HappinessBEST>.Run(() => {
|
|
var best = logic.GetById(id);
|
|
best.HappinessGroup = groupLogic.GetById(best.GroupId);
|
|
best.HappinessGroup.Weeks = weekLogic.GetAll(w => w.GroupId == best.GroupId).OrderBy(w=>w.SEQ).ToList();
|
|
return best;
|
|
});
|
|
}
|
|
|
|
[HttpGet()]
|
|
public async Task<IActionResult> GetInvitationQRcode(string id)
|
|
{
|
|
|
|
QRCodeGenerator gen = new QRCodeGenerator();
|
|
QRCodeGenerator qrGenerator = new QRCodeGenerator();
|
|
QRCodeData qrCodeData = qrGenerator.CreateQrCode($"https://happiness.tours/invitation/{id}", QRCodeGenerator.ECCLevel.Q);
|
|
QRCode qrCode = new QRCode(qrCodeData);
|
|
var qrCodeImage = qrCode.GetGraphic(3);
|
|
string qrCodeImagePath = "/App_Data/ScaneMeQrCode.png";
|
|
|
|
|
|
var backgroundBitmap = SixLabors.ImageSharp.Image.Load(qrCodeImagePath);
|
|
//string qrCodeImagePath = Environment.GetEnvironmentVariable("AppData");
|
|
//HttpContext.Current.Server.MapPath("~/App_Data/");
|
|
//var fullPath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data/ScaneMeQrCode.png");
|
|
//System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data/yourXmlFile.xml");
|
|
|
|
int positionLeft = 0;
|
|
int positionTop = 0;
|
|
var best = logic.GetById(id);
|
|
if (best != null)
|
|
{
|
|
using (var memoryStream = new MemoryStream())
|
|
{
|
|
//fileStream.CopyTo(memoryStream);
|
|
var image = Superimpose(best.Name, backgroundBitmap, qrCodeImage, 10, 32);
|
|
image.Scalling(75);
|
|
image.SaveAsPng(memoryStream);
|
|
byte[] byteImage = memoryStream.ToArray();
|
|
return File(byteImage, "image/png");
|
|
}
|
|
}
|
|
|
|
return this.NotFound();
|
|
}
|
|
private Font arialFont;
|
|
[NonAction]
|
|
public Image Superimpose(string bestName, Image largeBmp, Image smallBmp, int? x = null, int? y = null)
|
|
{
|
|
FontCollection collection = new();
|
|
FontFamily family = collection.Add("/App_Data/arial.ttf");
|
|
arialFont = family.CreateFont(12, FontStyle.Italic);
|
|
//Graphics g = Graphics.FromImage(largeBmp);
|
|
//g.CompositingMode = CompositingMode.SourceOver;
|
|
//smallBmp.MakeTransparent();
|
|
int margin = 5;
|
|
if (!x.HasValue)
|
|
{
|
|
x = largeBmp.Width - smallBmp.Width - margin;
|
|
}
|
|
if (!y.HasValue)
|
|
{
|
|
y = largeBmp.Height - smallBmp.Height - margin;
|
|
}
|
|
var scale = 0.8;
|
|
var scaleWidth = (int)(smallBmp.Width * scale);
|
|
var scaleHeight = (int)(smallBmp.Height * scale);
|
|
|
|
|
|
largeBmp.Mutate(x => x.DrawText(bestName, arialFont, Color.Black, new PointF(10, 10)));
|
|
|
|
smallBmp.Scalling(80);
|
|
|
|
largeBmp.Mutate(ctx => ctx.DrawImage(smallBmp, new Point(x.Value, y.Value),1f));
|
|
return largeBmp;
|
|
}
|
|
|
|
}
|
|
}
|