From adf066e9aac3977a122cee8d687d49d231d2e0c4 Mon Sep 17 00:00:00 2001 From: manuelbl Date: Sun, 13 Jan 2019 17:55:31 +0100 Subject: [PATCH] QR code as SVG --- dotnet/QrCodeGenerator/QrCode.cs | 52 ++++++++++++++++++++++++--- dotnet/QrCodeGeneratorTest/PngTest.cs | 2 +- dotnet/QrCodeGeneratorTest/SvgTest.cs | 45 +++++++++++++++++++++++ 3 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 dotnet/QrCodeGeneratorTest/SvgTest.cs diff --git a/dotnet/QrCodeGenerator/QrCode.cs b/dotnet/QrCodeGenerator/QrCode.cs index 7f6d4d0..dd1282b 100644 --- a/dotnet/QrCodeGenerator/QrCode.cs +++ b/dotnet/QrCodeGenerator/QrCode.cs @@ -28,6 +28,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Drawing.Imaging; +using System.Text; namespace Io.Nayuki.QrCodeGen { @@ -360,10 +361,10 @@ namespace Io.Nayuki.QrCodeGen /// - /// Returns a raster image depicting this QR Code, with the specified module scale and border modules. + /// Returns a bitmap (raster image) depicting this QR Code, with the specified module scale and border modules. /// /// - /// For example, ToImage(scale: 10, border: 4) means to pad the QR Code with 4 white + /// For example, ToBitmap(scale: 10, border: 4) means to pad the QR Code with 4 white /// border modules on all four sides, and use 10×10 pixels to represent each module. /// The resulting image only contains the hex colors 000000 and FFFFFF. /// @@ -372,7 +373,7 @@ namespace Io.Nayuki.QrCodeGen /// a new image representing this QR Code, with padding and scaling /// Thrown if the scale is 0 or negative, if the border is negative /// or if the resulting image is wider than 32,768 pixels - public Bitmap ToImage(int scale, int border) + public Bitmap ToBitmap(int scale, int border) { if (scale <= 0) { @@ -390,7 +391,7 @@ namespace Io.Nayuki.QrCodeGen throw new ArgumentOutOfRangeException(nameof(scale), "Scale or border too large"); } - Bitmap bitmap = new Bitmap(dim, dim, PixelFormat.Format24bppRgb); + var bitmap = new Bitmap(dim, dim, PixelFormat.Format24bppRgb); // simple and inefficient for (var y = 0; y < dim; y++) @@ -406,6 +407,49 @@ namespace Io.Nayuki.QrCodeGen } + /// + /// Returns a string of SVG code for an image depicting this QR Code, with the specified number of border modules. + /// + /// + /// The string always uses Unix newlines (\n), regardless of the platform. + /// + /// the number of border modules to add, which must be non-negative + /// a string representing this QR Code as an SVG XML document + /// Thrown if the border is negative + public string ToSvgString(int border) + { + if (border < 0) + { + throw new ArgumentOutOfRangeException(nameof(border), "Border must be non-negative"); + } + + int dim = Size + border * 2; + var sb = new StringBuilder() + .Append("\n") + .Append("\n") + .Append($"\n") + .Append("\t\n") + .Append("\t\n") + .Append("\n") + .ToString(); + } + #endregion diff --git a/dotnet/QrCodeGeneratorTest/PngTest.cs b/dotnet/QrCodeGeneratorTest/PngTest.cs index d65041a..6ffa250 100644 --- a/dotnet/QrCodeGeneratorTest/PngTest.cs +++ b/dotnet/QrCodeGeneratorTest/PngTest.cs @@ -33,7 +33,7 @@ namespace Io.Nayuki.QrCodeGen.Test private void PngImage() { var qrCode = EncodeText("The quick brown fox jumps over the lazy dog", Ecc.High); - using (var bitmap = qrCode.ToImage(3, 4)) + using (var bitmap = qrCode.ToBitmap(3, 4)) { Assert.Equal(135, bitmap.Width); Assert.Equal(135, bitmap.Height); diff --git a/dotnet/QrCodeGeneratorTest/SvgTest.cs b/dotnet/QrCodeGeneratorTest/SvgTest.cs new file mode 100644 index 0000000..7e804d7 --- /dev/null +++ b/dotnet/QrCodeGeneratorTest/SvgTest.cs @@ -0,0 +1,45 @@ +/* + * QR Code generator library (.NET) + * + * Copyright (c) Project Nayuki. (MIT License) + * https://www.nayuki.io/page/qr-code-generator-library + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * - The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * - The Software is provided "as is", without warranty of any kind, express or + * implied, including but not limited to the warranties of merchantability, + * fitness for a particular purpose and noninfringement. In no event shall the + * authors or copyright holders be liable for any claim, damages or other + * liability, whether in an action of contract, tort or otherwise, arising from, + * out of or in connection with the Software or the use or other dealings in the + * Software. + */ + +using System.IO; +using System.Text; +using Xunit; +using static Io.Nayuki.QrCodeGen.QrCode; + +namespace Io.Nayuki.QrCodeGen.Test +{ + public class SvgTest + { + [Fact] + private void SvgImage() + { + var qrCode = EncodeText("At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.", Ecc.Medium); + string svg = qrCode.ToSvgString(0); + + Assert.StartsWith("