Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/DecoderTest/DecoderTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.4" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/DecoderTest/SmokeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void SmokeTest()
[Test]
public void DecoderTest()
{
QRCodeDecoderLibrary.QRDecoder decoder = new ();
QRCodeDecoderLibrary.QRDecoder decoder = new();
var result = decoder.ImageDecoder(SixLabors.ImageSharp.Image.Load("pass.png"));

Assert.IsNotNull(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ public static IServiceCollection AddQRCodeDecoder(this IServiceCollection servic
{
return services.AddTransient<QRDecoder>();
}

public static IServiceCollection AddQRCodeDecoderFactory(this IServiceCollection services)
{
return services.AddSingleton<IQRDecoderFactory, QRDecoderFactory>();
}
}
}
28 changes: 28 additions & 0 deletions src/QRCodeDecoderLibrary/DependencyInjection/QRDecoderFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace QRCodeDecoderLibrary
{
public interface IQRDecoderFactory
{
QRDecoder CreateQRDecoder();
}

public class QRDecoderFactory : IQRDecoderFactory
{
private readonly IServiceProvider _serviceProvider;

public QRDecoderFactory(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

public QRDecoder CreateQRDecoder()
{
var logger = _serviceProvider.GetRequiredService<ILogger<QRDecoder>>();
return new QRDecoder(logger);
}
}
}
182 changes: 91 additions & 91 deletions src/QRCodeDecoderLibrary/Finder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
//
// QR Code Library
//
Expand Down Expand Up @@ -45,105 +45,105 @@

namespace QRCodeDecoderLibrary
{
/// <summary>
/// QR code finder class
/// </summary>
internal class Finder
{
// horizontal scan
internal int Row;
internal int Col1;
internal int Col2;
internal double HModule;
/// <summary>
/// QR code finder class
/// </summary>
internal class Finder
{
// horizontal scan
internal int Row;
internal int Col1;
internal int Col2;
internal double HModule;

// vertical scan
internal int Col;
internal int Row1;
internal int Row2;
internal double VModule;
// vertical scan
internal int Col;
internal int Row1;
internal int Row2;
internal double VModule;

internal double Distance;
internal double ModuleSize;
internal double Distance;
internal double ModuleSize;

/// <summary>
/// Constructor during horizontal scan
/// </summary>
internal Finder
(
int Row,
int Col1,
int Col2,
double HModule
)
{
this.Row = Row;
this.Col1 = Col1;
this.Col2 = Col2;
this.HModule = HModule;
Distance = double.MaxValue;
return;
}
/// <summary>
/// Constructor during horizontal scan
/// </summary>
internal Finder
(
int Row,
int Col1,
int Col2,
double HModule
)
{
this.Row = Row;
this.Col1 = Col1;
this.Col2 = Col2;
this.HModule = HModule;
Distance = double.MaxValue;
return;
}

/// <summary>
/// Match during vertical scan
/// </summary>
internal void Match
(
int Col,
int Row1,
int Row2,
double VModule
)
{
// test if horizontal and vertical are not related
if(Col < Col1 || Col >= Col2 || Row < Row1 || Row >= Row2) return;
/// <summary>
/// Match during vertical scan
/// </summary>
internal void Match
(
int Col,
int Row1,
int Row2,
double VModule
)
{
// test if horizontal and vertical are not related
if (Col < Col1 || Col >= Col2 || Row < Row1 || Row >= Row2) return;

// Module sizes must be about the same
if(Math.Min(HModule, VModule) < Math.Max(HModule, VModule) * QRDecoder.MODULE_SIZE_DEVIATION) return;
// Module sizes must be about the same
if (Math.Min(HModule, VModule) < Math.Max(HModule, VModule) * QRDecoder.MODULE_SIZE_DEVIATION) return;

// calculate distance
double DeltaX = Col - 0.5 * (Col1 + Col2);
double DeltaY = Row - 0.5 * (Row1 + Row2);
double Delta = Math.Sqrt(DeltaX * DeltaX + DeltaY * DeltaY);
// calculate distance
double DeltaX = Col - 0.5 * (Col1 + Col2);
double DeltaY = Row - 0.5 * (Row1 + Row2);
double Delta = Math.Sqrt(DeltaX * DeltaX + DeltaY * DeltaY);

// distance between two points must be less than 2 pixels
if(Delta > QRDecoder.HOR_VERT_SCAN_MAX_DISTANCE) return;
// distance between two points must be less than 2 pixels
if (Delta > QRDecoder.HOR_VERT_SCAN_MAX_DISTANCE) return;

// new result is better than last result
if(Delta < Distance)
{
this.Col = Col;
this.Row1 = Row1;
this.Row2 = Row2;
this.VModule = VModule;
ModuleSize = 0.5 * (HModule + VModule);
Distance = Delta;
}
return;
}
// new result is better than last result
if (Delta < Distance)
{
this.Col = Col;
this.Row1 = Row1;
this.Row2 = Row2;
this.VModule = VModule;
ModuleSize = 0.5 * (HModule + VModule);
Distance = Delta;
}
return;
}

/// <summary>
/// Horizontal and vertical scans overlap
/// </summary>
internal bool Overlap
(
Finder Other
)
{
return Other.Col1 < Col2 && Other.Col2 >= Col1 && Other.Row1 < Row2 && Other.Row2 >= Row1;
}
/// <summary>
/// Horizontal and vertical scans overlap
/// </summary>
internal bool Overlap
(
Finder Other
)
{
return Other.Col1 < Col2 && Other.Col2 >= Col1 && Other.Row1 < Row2 && Other.Row2 >= Row1;
}

/// <summary>
/// Finder to string
/// </summary>
public override string ToString()
{
if(Distance == double.MaxValue)
{
return string.Format("Finder: Row: {0}, Col1: {1}, Col2: {2}, HModule: {3:0.00}", Row, Col1, Col2, HModule);
}
/// <summary>
/// Finder to string
/// </summary>
public override string ToString()
{
if (Distance == double.MaxValue)
{
return string.Format("Finder: Row: {0}, Col1: {1}, Col2: {2}, HModule: {3:0.00}", Row, Col1, Col2, HModule);
}

return string.Format("Finder: Row: {0}, Col: {1}, Module: {2:0.00}, Distance: {3:0.00}", Row, Col, ModuleSize, Distance);
}
}
return string.Format("Finder: Row: {0}, Col: {1}, Module: {2:0.00}, Distance: {3:0.00}", Row, Col, ModuleSize, Distance);
}
}
}
2 changes: 1 addition & 1 deletion src/QRCodeDecoderLibrary/QRCodeDecoderLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.3" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.4" />
<PackageReference Include="Stef.Validation" Version="0.1.1" />
</ItemGroup>

Expand Down
8 changes: 5 additions & 3 deletions src/QRCodeDecoderLibrary/QRDecoder.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;

using Microsoft.Extensions.Logging;

using QRCodeSharedLibrary;
using Stef.Validation;

using SixLabors.ImageSharp.Processing;

using Stef.Validation;

namespace QRCodeDecoderLibrary
{
public class QRDecoder : StaticTables
Expand Down
1 change: 1 addition & 0 deletions src/QRCodeDecoderLibrary/ReedSolomon.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;

using QRCodeSharedLibrary;

namespace QRCodeDecoderLibrary
Expand Down
4 changes: 4 additions & 0 deletions src/QRCodeSharedLibrary/QRCodeSharedLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
<PackageId>QRCodeShared</PackageId>
<PackageLicenseUrl>https://www.codeproject.com/info/cpol10.aspx</PackageLicenseUrl>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.4" />
</ItemGroup>
</Project>