AddUserSecrets, RenameByDateTaken and BlurHash

This commit is contained in:
2023-05-21 18:11:26 -07:00
parent aec9d0a55d
commit 514637b9c6
70 changed files with 1128 additions and 193 deletions

45
BlurHash/BlurHash.csproj Normal file
View File

@ -0,0 +1,45 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<OutputType>library</OutputType>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<PackageId>Phares.View.by.Distance.BlurHash</PackageId>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>7.0.101.1</Version>
<Authors>Mike Phares</Authors>
<Company>Phares</Company>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
<PropertyGroup>
<IsWindows Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'">true</IsWindows>
<IsOSX Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))' == 'true'">true</IsOSX>
<IsLinux Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' == 'true'">true</IsLinux>
</PropertyGroup>
<PropertyGroup Condition="'$(IsWindows)'=='true'">
<DefineConstants>Windows</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(IsOSX)'=='true'">
<DefineConstants>OSX</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(IsLinux)'=='true'">
<DefineConstants>Linux</DefineConstants>
</PropertyGroup>
<ItemGroup Condition="'$(RuntimeIdentifier)' == 'browser-wasm'">
<SupportedPlatform Include="browser" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
<PackageReference Include="System.Text.Json" Version="7.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Shared\View-by-Distance.Shared.csproj" />
<ProjectReference Include="..\BlurHash.System.Drawing.Common\BlurHash.System.Drawing.Common.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,52 @@
using System.Drawing;
using System.Text;
using View_by_Distance.Shared.Models.Methods;
namespace View_by_Distance.BlurHash.Models;
public class BlurHasher : IBlurHasher
{
string IBlurHasher.Encode(Image image) =>
#pragma warning disable CA1416
image.Width < image.Height ? Encode(image, 4, 3) : Encode(image, 3, 4);
#pragma warning restore CA1416
string IBlurHasher.EncodeAndSave(Image image, string directory) =>
#pragma warning disable CA1416
image.Width < image.Height ? EncodeAndSave(image, 4, 3, 300, 190, directory) : EncodeAndSave(image, 3, 4, 300, 190, directory);
#pragma warning restore CA1416
string IBlurHasher.Encode(Image image, int x, int y) =>
Encode(image, x, y);
string IBlurHasher.EncodeAndSave(Image image, int x, int y, string directory) =>
EncodeAndSave(image, x, y, 300, 190, directory);
internal static string Encode(Image image, int x, int y) =>
System.Drawing.BlurHash.BlurHasher.Encode(image, x, y);
public string EncodeAndSave(Image image, int x, int y, int width, int height, string directory)
{
string result;
int actualByte;
result = System.Drawing.BlurHash.BlurHasher.Encode(image, x, y);
byte[] blurHashBytes = Encoding.UTF8.GetBytes(result);
using Bitmap actualImage = (Bitmap)System.Drawing.BlurHash.BlurHasher.Decode(result, width, height);
string joined = string.Join(string.Empty, blurHashBytes.Select(l => l.ToString("000")));
string fileName = Path.Combine(directory, $"{x}x{y}-{width}x{height}-{joined}.png");
if (!File.Exists(fileName))
{
using FileStream fileStream = new(fileName, FileMode.CreateNew);
#pragma warning disable CA1416
actualImage.Save(fileStream, System.Drawing.Imaging.ImageFormat.Png);
#pragma warning restore CA1416
_ = fileStream.Seek(0, SeekOrigin.Begin);
actualByte = fileStream.ReadByte();
while (actualByte > -1)
actualByte = fileStream.ReadByte();
}
return result;
}
}