Skip to content

Commit

Permalink
Bugfix/rotate (#26)
Browse files Browse the repository at this point in the history
* fixing swagger

* Rotate now works as it should :)
  • Loading branch information
Gaardsholt authored Mar 18, 2021
1 parent b8269cc commit 349b39e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 23 deletions.
3 changes: 1 addition & 2 deletions barcoder/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

namespace barcoder.Controllers
{
[Route("Api")]
[ApiController]
public class ApiController : ControllerBase
{
[HttpGet]
[HttpGet("Api")]
public Dictionary<string, int> GetTypes()
{
var allowedBarcodes = MultiFormatWriter.SupportedWriters.ToList();
Expand Down
5 changes: 2 additions & 3 deletions barcoder/Controllers/HexController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

namespace barcoder.Controllers
{
[Route("hex.png")]
[ApiController]
public class HexController : ControllerBase
{

[HttpGet("")]
[HttpGet("hex.png")]
public IActionResult Get(string color, string border, int width = 300, int height = 300)
{
try
Expand All @@ -18,7 +17,7 @@ public IActionResult Get(string color, string border, int width = 300, int heigh
var borderColor = ColorTranslator.FromHtml($"#{border}");

var image = new Bitmap(width, height);

using (Graphics g = Graphics.FromImage(image))
{
g.Clear(backgroundColor);
Expand Down
2 changes: 1 addition & 1 deletion barcoder/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public IActionResult Index()
}


[Route("Mobilepay")]
[HttpGet("Mobilepay")]
public IActionResult Mobilepay()
{
return View();
Expand Down
78 changes: 61 additions & 17 deletions barcoder/Controllers/ImageController.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
using Microsoft.AspNetCore.Mvc;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using ZXing;
using ZXing.QrCode.Internal;
using ZXing.Rendering;

namespace barcoder.Controllers
{
[Route("image.png")]
[ApiController]
public class ImageController : ControllerBase
{
[HttpGet]

[HttpGet("image.png")]
public IActionResult Get(string text, BarcodeFormat type, string logo, int width = 300, int height = 30, int rotate = 0)
{
try
Expand Down Expand Up @@ -50,7 +50,7 @@ public IActionResult Get(string text, BarcodeFormat type, string logo, int width

private static byte[] Bitmap2Byte(Bitmap bm)
{
var image = (System.Drawing.Image)bm;
var image = (Image)bm;
using (var ms = new MemoryStream())
{
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
Expand All @@ -67,7 +67,7 @@ private static Bitmap AddLogoToBarcode(Bitmap bm, string url)
var deltaWidth = bm.Width - overlay.Width;

var g = Graphics.FromImage(bm);
g.DrawImage(overlay, new System.Drawing.Point(deltaWidth / 2, deltaHeigth / 2));
g.DrawImage(overlay, new Point(deltaWidth / 2, deltaHeigth / 2));
return bm;
}

Expand All @@ -81,7 +81,6 @@ private static Bitmap DownloadImageAsBitmap(string url)
return overlay;
}


public static Bitmap ScaleImage(Bitmap bmp, int maxWidth, int maxHeight)
{
var ratioX = (double)maxWidth / bmp.Width;
Expand All @@ -99,20 +98,65 @@ public static Bitmap ScaleImage(Bitmap bmp, int maxWidth, int maxHeight)
return newImage;
}

private static Bitmap RotateImage(Bitmap bm, float rotate)
private Bitmap RotateImage(Bitmap bm, float angle)
{
using (Graphics g = Graphics.FromImage(bm))
// Make a Matrix to represent rotation by this angle.
var rotate_at_origin = new Matrix();
rotate_at_origin.Rotate(angle);

// Rotate the image's corners to see how big it will be after rotation.
PointF[] points =
{
new PointF(0, 0),
new PointF(bm.Width, 0),
new PointF(bm.Width, bm.Height),
new PointF(0, bm.Height),
};
rotate_at_origin.TransformPoints(points);


var result = CreateBitMapThatFits(points);

// Create the real rotation transformation.
var rotate_at_center = new Matrix();
rotate_at_center.RotateAt(angle, new PointF(result.Width / 2f, result.Height / 2f));

// Draw the image onto the new bitmap rotated.
using (Graphics gr = Graphics.FromImage(result))
{
// Set the rotation point to the center in the matrix
g.TranslateTransform(bm.Width / 2, bm.Height / 2);
// Rotate
g.RotateTransform(rotate);
// Restore rotation point in the matrix
g.TranslateTransform(-bm.Width / 2, -bm.Height / 2);
// Draw the image on the bitmap
g.DrawImage(bm, new System.Drawing.Point(0, 0));
gr.InterpolationMode = InterpolationMode.High;
gr.Clear(bm.GetPixel(0, 0));

gr.Transform = rotate_at_center;

// Draw the image centered on the bitmap.
int x = (result.Width - bm.Width) / 2;
int y = (result.Height - bm.Height) / 2;
gr.DrawImage(bm, x, y);
}
return bm;

return result;
}

private static Bitmap CreateBitMapThatFits(PointF[] points)
{
var xmin = points[0].X;
var xmax = xmin;
var ymin = points[0].Y;
var ymax = ymin;
foreach (PointF point in points)
{
if (xmin > point.X) xmin = point.X;
if (xmax < point.X) xmax = point.X;
if (ymin > point.Y) ymin = point.Y;
if (ymax < point.Y) ymax = point.Y;
}

int wid = (int)Math.Round(xmax - xmin);
int hgt = (int)Math.Round(ymax - ymin);
var result = new Bitmap(wid, hgt);

return result;
}

}
Expand Down

0 comments on commit 349b39e

Please sign in to comment.