feat: add barcodes below ISBN

This commit is contained in:
Oliver Booth 2023-12-14 22:09:17 +00:00
parent 70c7cfe8d2
commit 95b3ac9695
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5
5 changed files with 38 additions and 4 deletions

View File

@ -1,3 +1,9 @@
using NetBarcode;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Processing;
using Type = System.Type;
namespace OliverBooth.Data.Web;
/// <summary>
@ -16,4 +22,16 @@ internal sealed class Book : IBook
/// <inheritdoc />
public string Title { get; }
}
public string GetBarcode()
{
var barcode = new Barcode(Isbn, NetBarcode.Type.EAN13);
using var image = barcode.GetImage();
int width = image.Width;
int height = image.Height;
image.Mutate(i => i.Pad(width + 10, height + 10, Color.White));
image.Mutate(i => i.Resize(i.GetCurrentSize() / 4 * 3));
return image.ToBase64String(PngFormat.Instance);
}
}

View File

@ -1,3 +1,5 @@
using SixLabors.ImageSharp;
namespace OliverBooth.Data.Web;
/// <summary>
@ -28,4 +30,11 @@ public interface IBook
/// </summary>
/// <value>The title of the book.</value>
string Title { get; }
/// <summary>
/// Generates the barcode for this book.
/// </summary>
/// <returns>The EAN-13 barcode encoded as PNG in Base64.</returns>
/// <remarks>This value should be disposed.</remarks>
string GetBarcode();
}

View File

@ -2,6 +2,7 @@ FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
RUN apt install -y libc6-dev libgdiplus
FROM node:20-alpine as build-deps
WORKDIR /src

View File

@ -18,6 +18,7 @@
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="8.0.0"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="8.0.0"/>
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="8.0.0"/>
<PackageReference Include="NetBarcode" Version="1.7.0"/>
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="7.0.0"/>
<PackageReference Include="Serilog" Version="3.1.1"/>
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0"/>

View File

@ -1,5 +1,10 @@
@page
@using NetBarcode
@using OliverBooth.Data.Web
@using SixLabors.ImageSharp
@using SixLabors.ImageSharp.Formats.Png
@using SixLabors.ImageSharp.Processing
@using Type = NetBarcode.Type
@model OliverBooth.Pages.Books
@{
ViewData["Title"] = "Reading List";
@ -33,7 +38,7 @@
@book.Title.Trim()
</td>
<td>@book.Author.Trim()</td>
<td>@book.Isbn.Trim()</td>
<td style="font-family: monospace">@book.Isbn.Trim()<br><img src="@book.GetBarcode()" alt="@book.Isbn"></td>
</tr>
}
</tbody>
@ -58,7 +63,7 @@
@book.Title.Trim()
</td>
<td>@book.Author.Trim()</td>
<td>@book.Isbn.Trim()</td>
<td style="font-family: monospace">@book.Isbn.Trim()<br><img src="@book.GetBarcode()" alt="@book.Isbn"></td>
</tr>
}
</tbody>
@ -83,7 +88,7 @@
@book.Title.Trim()
</td>
<td>@book.Author.Trim()</td>
<td>@book.Isbn.Trim()</td>
<td style="font-family: monospace">@book.Isbn.Trim()<br><img src="@book.GetBarcode()" alt="@book.Isbn"></td>
</tr>
}
</tbody>