Skip to content

Added support for Portable Class Library (PCL) and UWP #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Sep 10, 2016
Merged
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,4 @@ pip-log.txt
# Xamarin
*.userprefs

QRCoder/PortabilityAnalysis.html
1 change: 1 addition & 0 deletions MyGet.bat
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ if not "%PackageVersion%" == "" (
mkdir Build
mkdir Build\lib
mkdir Build\lib\net40
mkdir Build\lib\netcore45

%NuGet% pack "QRCoder\QRCoder.nuspec" -NoPackageAnalysis -verbosity detailed -o Build -Version %version% -p Configuration="%config%"
81 changes: 81 additions & 0 deletions QRCoder/BitmapByteQRCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QRCoder
{

// ReSharper disable once InconsistentNaming
public class BitmapByteQRCode : AbstractQRCode<byte[]>, IDisposable
{
public BitmapByteQRCode(QRCodeData data) : base(data) { }


public override byte[] GetGraphic(int pixelsPerModule)
{
var sideLength = this.QrCodeData.ModuleMatrix.Count * pixelsPerModule;

var moduleDark = new byte[] {0x00, 0x00, 0x00};
var moduleLight = new byte[] { 0xFF, 0xFF, 0xFF};

List<byte> bmp = new List<byte>();

//header
bmp.AddRange(new byte[] { 0x42, 0x4D, 0x4C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00 });

//width
bmp.AddRange(IntTo4Byte(sideLength));
//height
bmp.AddRange(IntTo4Byte(sideLength));

//header end
bmp.AddRange(new byte[] { 0x01, 0x00, 0x18, 0x00 });

//draw qr code
for (var x = sideLength-1; x >= 0; x = x - pixelsPerModule)
{
for (int pm = 0; pm < pixelsPerModule; pm++)
{
for (var y = 0; y < sideLength; y = y + pixelsPerModule)
{
var module =
this.QrCodeData.ModuleMatrix[(x + pixelsPerModule)/pixelsPerModule - 1][(y + pixelsPerModule)/pixelsPerModule - 1];
for (int i = 0; i < pixelsPerModule; i++)
{
bmp.AddRange(module ? moduleDark : moduleLight);
}
}
if (sideLength%4 != 0)
{
for (int i = 0; i < sideLength%4; i++)
{
bmp.Add(0x00);
}
}
}
}

//finalize with terminator
bmp.AddRange(new byte[] { 0x00, 0x00 });

return bmp.ToArray();
}
private byte[] IntTo4Byte(int inp)
{
byte[] bytes = new byte[2];
unchecked
{
bytes[1] = (byte)(inp >> 8);
bytes[0] = (byte)(inp);
}
return bytes;
}

public void Dispose()
{
this.QrCodeData = null;
}
}
}
81 changes: 69 additions & 12 deletions QRCoder/QRCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Collections;
using System.Reflection;
using System.Runtime.InteropServices;

namespace QRCoder
{
Expand Down Expand Up @@ -207,10 +208,22 @@ public static void AddQuietZone(ref QRCodeData qrCode)
}
}

private static string ReverseString(string inp)
{
string newStr = string.Empty;
if (inp.Length > 0)
{
for (int i = inp.Length - 1; i >= 0; i--)
newStr += inp[i];
}
return newStr;
}

public static void PlaceVersion(ref QRCodeData qrCode, string versionStr)
{
var size = qrCode.ModuleMatrix.Count;
var vStr = new string(versionStr.Reverse().ToArray());

var vStr = ReverseString(versionStr);

for (var x = 0; x < 6; x++)
{
Expand All @@ -225,7 +238,7 @@ public static void PlaceVersion(ref QRCodeData qrCode, string versionStr)
public static void PlaceFormat(ref QRCodeData qrCode, string formatStr)
{
var size = qrCode.ModuleMatrix.Count;
var fStr = new string(formatStr.Reverse().ToArray());
var fStr = ReverseString(formatStr);
var modules = new[,] { { 8, 0, size - 1, 8 }, { 8, 1, size - 2, 8 }, { 8, 2, size - 3, 8 }, { 8, 3, size - 4, 8 }, { 8, 4, size - 5, 8 }, { 8, 5, size - 6, 8 }, { 8, 7, size - 7, 8 }, { 8, 8, size - 8, 8 }, { 7, 8, 8, size - 7 }, { 5, 8, 8, size - 6 }, { 4, 8, 8, size - 5 }, { 3, 8, 8, size - 4 }, { 2, 8, 8, size - 3 }, { 1, 8, 8, size - 2 }, { 0, 8, 8, size - 1 } };
for (var i = 0; i < 15; i++)
{
Expand All @@ -235,6 +248,7 @@ public static void PlaceFormat(ref QRCodeData qrCode, string formatStr)
qrCode.ModuleMatrix[p2.Y][p2.X] = fStr[i] == '1';
}
}


public static int MaskCode(ref QRCodeData qrCode, int version, ref List<Rectangle> blockedModules, ECCLevel eccLevel)
{
Expand All @@ -243,7 +257,14 @@ public static int MaskCode(ref QRCodeData qrCode, int version, ref List<Rectangl

var size = qrCode.ModuleMatrix.Count;

foreach (var pattern in typeof(MaskPattern).GetMethods())

#if NET40
var methods = typeof (MaskPattern).GetMethods();
#else
var methods = typeof (MaskPattern).GetTypeInfo().DeclaredMethods;
#endif

foreach (var pattern in methods)
{
if (pattern.Name.Length == 8 && pattern.Name.Substring(0, 7) == "Pattern")
{
Expand Down Expand Up @@ -286,7 +307,15 @@ public static int MaskCode(ref QRCodeData qrCode, int version, ref List<Rectangl
}
}

var patterMethod = typeof(MaskPattern).GetMethods().First(x => x.Name == patternName);


#if NET40
var patterMethod = typeof(MaskPattern).GetMethods().First(x => x.Name == patternName);
#else
var patterMethod = typeof(MaskPattern).GetTypeInfo().GetDeclaredMethod(patternName);
#endif


for (var x = 0; x < size; x++)
{
for (var y = 0; y < size; y++)
Expand All @@ -306,7 +335,10 @@ public static void PlaceDataWords(ref QRCodeData qrCode, string data, ref List<R
var size = qrCode.ModuleMatrix.Count;
var up = true;
var datawords = new Queue<bool>();
data.ToList().ForEach(x => datawords.Enqueue(x != '0'));
for (int i = 0; i< data.Length; i++)
{
datawords.Enqueue(data[i] != '0');
}
for (var x = size - 1; x >= 0; x = x - 2)
{
if (x == 6)
Expand Down Expand Up @@ -701,14 +733,29 @@ private int GetVersion(int length, EncodingMode encMode, ECCLevel eccLevel)

private EncodingMode GetEncodingFromPlaintext(string plainText)
{
if (plainText.All(c => "0123456789".Contains(c)))
if (StringAll(plainText, "0123456789"))
return EncodingMode.Numeric;
else if (plainText.All(c => this.alphanumEncTable.Contains(c)))
else if (StringAll(plainText, this.alphanumEncTable.ToString()))
return EncodingMode.Alphanumeric;
else
return EncodingMode.Byte;
}

private bool StringAll(string input, string charGroupIn)
{

bool res = true;
foreach (var c in input)
{
if (!charGroupIn.Contains(c.ToString()))
{
res = false;
break;
}
}
return res;
}

private Polynom CalculateMessagePolynom(string bitString)
{
var messagePol = new Polynom();
Expand Down Expand Up @@ -743,8 +790,8 @@ private Polynom CalculateGeneratorPolynom(int numEccWords)
}

private List<string> BinaryStringToBitBlockList(string bitString)
{
return bitString.ToList().Select((x, i) => new { Index = i, Value = x })
{
return new List<char>(bitString.ToCharArray()).Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index / 8)
.Select(x => String.Join("", x.Select(v => v.Value.ToString()).ToArray()))
.ToList();
Expand Down Expand Up @@ -819,7 +866,8 @@ private bool IsUtf8(EncodingMode encoding, string plainText)
private bool IsValidISO(string input)
{
var bytes = Encoding.GetEncoding("ISO-8859-1").GetBytes(input);
var result = Encoding.GetEncoding("ISO-8859-1").GetString(bytes);
//var result = Encoding.GetEncoding("ISO-8859-1").GetString(bytes);
var result = Encoding.GetEncoding("ISO-8859-1").GetString(bytes,0,bytes.Length);
return String.Equals(input, result);
}

Expand Down Expand Up @@ -990,7 +1038,12 @@ private static int ShrinkAlphaExp(int alphaExp)
private void CreateAlphanumEncDict()
{
this.alphanumEncDict = new Dictionary<char, int>();
this.alphanumEncTable.ToList().Select((x, i) => new { Chr = x, Index = i }).ToList().ForEach(x => this.alphanumEncDict.Add(x.Chr, x.Index));
//this.alphanumEncTable.ToList().Select((x, i) => new { Chr = x, Index = i }).ToList().ForEach(x => this.alphanumEncDict.Add(x.Chr, x.Index));
var resList = this.alphanumEncTable.ToList().Select((x, i) => new { Chr = x, Index = i }).ToList();
foreach (var res in resList)
{
this.alphanumEncDict.Add(res.Chr, res.Index);
}
}

private void CreateAlignmentPatternTable()
Expand Down Expand Up @@ -1279,7 +1332,11 @@ public Polynom()
public override string ToString()
{
var sb = new StringBuilder();
this.PolyItems.ForEach(x => sb.Append("a^" + x.Coefficient + "*x^" + x.Exponent + " + "));
//this.PolyItems.ForEach(x => sb.Append("a^" + x.Coefficient + "*x^" + x.Exponent + " + "));
foreach (var polyItem in this.PolyItems)
{
sb.Append("a^" + polyItem.Coefficient + "*x^" + polyItem.Exponent + " + ");
}

return sb.ToString().TrimEnd(new[] { ' ', '+' });
}
Expand Down
5 changes: 4 additions & 1 deletion QRCoder/QRCoder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DefineConstants>TRACE;DEBUG;NET40</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -28,6 +29,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand All @@ -46,6 +48,7 @@
<ItemGroup>
<Compile Include="AbstractQRCode.cs" />
<Compile Include="Base64QRCode.cs" />
<Compile Include="BitmapByteQRCode.cs" />
<Compile Include="PayloadGenerator.cs" />
<Compile Include="QRCodeData.cs" />
<Compile Include="QRCode.cs" />
Expand Down
56 changes: 56 additions & 0 deletions QRCoder/QRCoderProject.Portable.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<MinimumVisualStudioVersion>10.0</MinimumVisualStudioVersion>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{B2709734-9F32-4750-9CEC-10C5915B23FB}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>QRCoder</RootNamespace>
<AssemblyName>QRCoder</AssemblyName>
<DefaultLanguage>en-US</DefaultLanguage>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TargetFrameworkProfile>Profile111</TargetFrameworkProfile>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<!-- A reference to the entire .NET Framework is automatically included -->
<None Include="project.json" />
</ItemGroup>
<ItemGroup>
<Compile Include="AbstractQRCode.cs" />
<Compile Include="BitmapByteQRCode.cs" />
<Compile Include="PayloadGenerator.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="QRCodeData.cs" />
<Compile Include="QRCodeGenerator.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
8 changes: 8 additions & 0 deletions QRCoderDemoUWP/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Application
x:Class="QRCoderDemoUWP.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:QRCoderDemoUWP"
RequestedTheme="Light">

</Application>
Loading