/// <summary> /// Generates a QR code with the automatically defined version and specified error correction level, pixel scaling, color, background color, and quiet zone from the input data and returns the QR code image as an array of bytes. /// </summary> /// <param name="data">The user data for encoding.</param> /// <param name="encoding">The QR code encoding.</param> /// <param name="levelCorrection">The level of error correction.</param> /// <param name="scaleMultiplier">The pixel scaling of the resulting QR code image.</param> /// <param name="foregroundColor">The QR code color.</param> /// <param name="backgroundColor">The background color.</param> /// <param name="hasQuietZones">Defines whether the QR code has a "quiet zone".</param> /// <returns>byte[]</returns> public static byte[] GetQRCode(string data, QRCodeEncodingMethod encoding, QRCodeErrorCorrection levelCorrection, int scaleMultiplier, Color foregroundColor, Color backgroundColor, bool hasQuietZones = true) { return(GetQRCode(data, encoding, levelCorrection, QRCodesUtils.GetMinVersionForBinaryMode(data, levelCorrection), scaleMultiplier, foregroundColor, backgroundColor, hasQuietZones)); }
public void TestGetMinVersionOfQRCodeForBinaryMode() { QRCodesUtils.GetMinVersionForBinaryMode("test", QRCodeErrorCorrection.M) .Should().Be(1); QRCodesUtils.GetMinVersionForBinaryMode(string.Concat(Enumerable.Repeat("test ", 12)), QRCodeErrorCorrection.Q) .Should().Be(5); QRCodesUtils.GetMinVersionForBinaryMode(string.Concat(Enumerable.Repeat("test ", 12)), QRCodeErrorCorrection.H) .Should().Be(7); Assert.Throws <InvalidOperationException>(() => QRCodesUtils.GetMinVersionForBinaryMode(string.Concat(Enumerable.Repeat("test ", 10000)), QRCodeErrorCorrection.L)); }
/// <summary> /// Renders a QR code on bitmap from the encoded data and returns an array of bytes. /// </summary> /// <param name="data">The user data for encoding.</param> /// <param name="encoding">The QR code encoding.</param> /// <param name="levelCorrection">The level of error correction.</param> /// <param name="version">The QR code version.</param> /// <param name="scaleMultiplier">The pixel scaling of the resulting QR code image.</param> /// <param name="foregroundColor">The QR code color.</param> /// <param name="backgroundColor">The background color.</param> /// <param name="quietZone">The size of the quiet zone.</param> /// <returns>byte[]</returns> internal static byte[] GetQRCodeImage(string data, QRCodeEncodingMethod encoding, QRCodeErrorCorrection levelCorrection, QRCodeVersion version, int scaleMultiplier, Color foregroundColor, Color backgroundColor, int quietZone = 4) { if (!Enum.IsDefined(typeof(QRCodeVersion), version)) { throw new ArgumentOutOfRangeException(); } // Only the binary method is implemented if (encoding != QRCodeEncodingMethod.Binary) { throw new NotImplementedException(); } int numVersion; if (version == QRCodeVersion.Automatic) { numVersion = (int)QRCodesUtils.GetMinVersionForBinaryMode(data, levelCorrection); } else { numVersion = (int)version; } var numberMask = 4; // Get the image data array byte[] dataBinary = QRCodesEncoder.EncodeQRCodeData(data, numVersion, levelCorrection); // Create a bitmap for the QR code var bitmapSource = CreateBitmap(dataBinary, numVersion, numberMask, levelCorrection, new SolidBrush(foregroundColor), new SolidBrush(backgroundColor), quietZone); // Pixel scaling var bitmap = QRCodesUtils.Scale(bitmapSource, scaleMultiplier); // Save the resulting image in the PNG format var memStream = new MemoryStream(); bitmap.Save(memStream, System.Drawing.Imaging.ImageFormat.Png); memStream.Flush(); var qrData = memStream.ToArray(); memStream.Close(); return(qrData); }
public void TestInputParamsValidation_GetMinVersionOfQRCodeForBinaryMode(string text, int correction, bool shouldThrowArgumentException, string caseName) { if (shouldThrowArgumentException) { ((Action)call).Should().ThrowExactly <ArgumentException>(); } else { call(); } Assert.True(true, caseName); void call() { QRCodesUtils.GetMinVersionForBinaryMode(text, (QRCodeErrorCorrection)correction); } }