// Creates a variety of QR Codes that exercise different features of the library, and writes each one to file. private static void DoVarietyDemo() { QrCode qr; // Numeric mode encoding (3.33 bits per digit) qr = QrCode.EncodeText("314159265358979323846264338327950288419716939937510", Ecc.Medium); WritePng(qr.ToImage(13, 1), "pi-digits-QR.png"); // Alphanumeric mode encoding (5.5 bits per character) qr = QrCode.EncodeText("DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/", Ecc.High); WritePng(qr.ToImage(10, 2), "alphanumeric-QR.png"); // Unicode text as UTF-8 qr = QrCode.EncodeText("こんにちwa、世界! αβγδ", Ecc.Quartitle); WritePng(qr.ToImage(10, 3), "unicode-QR.png"); // Moderately large QR Code using longer text (from Lewis Carroll's Alice in Wonderland) qr = QrCode.EncodeText( "Alice was beginning to get very tired of sitting by her sister on the bank, " + "and of having nothing to do: once or twice she had peeped into the book her sister was reading, " + "but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice " + "'without pictures or conversations?' So she was considering in her own mind (as well as she could, " + "for the hot day made her feel very sleepy and stupid), whether the pleasure of making a " + "daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly " + "a White Rabbit with pink eyes ran close by her.", Ecc.High); WritePng(qr.ToImage(6, 10), "alice-wonderland-QR.png"); }
public void BitmapColorImageTest() { var qrCode = QrCode.EncodeText("The quick brown fox jumps over the lazy dog", QrCode.Ecc.High); var fg = Color.Red; var bg = Color.Black; using (var bitmap = qrCode.ToBitmap(3, 0, fg, bg)) { var containsFg = false; var containsBg = false; //Start from bottom right for (int i = bitmap.Height - 1; i >= 0 && !(containsFg && containsBg); i--) { for (int j = bitmap.Width - 1; j >= 0 && !(containsFg && containsBg); j--) { var pixel = bitmap.GetPixel(j, i).ToArgb(); if (pixel == fg.ToArgb()) { containsFg = true; } else if (pixel == bg.ToArgb()) { containsBg = true; } } } Assert.True(containsFg); Assert.True(containsBg); } }
public override void Process(TagHelperContext context, TagHelperOutput output) { if (string.IsNullOrWhiteSpace(Data) || Border < 0) { output.SuppressOutput(); return; } var qrCode = QrCode.EncodeText(Data, _configuration.ErrorCorrectionLevel); if (_configuration.Format == QrCodeFormat.Svg) { var svg = qrCode.ToSvgString(Border); output.Content.AppendHtml(svg); } else { if (Scale <= 0) { output.SuppressOutput(); return; } var bitmap = qrCode.ToImage(Scale, Border); using var ms = new MemoryStream(); bitmap.Save(ms, ImageFormat.Png); var base64 = Convert.ToBase64String(ms.ToArray()); output.Content.AppendHtml($"<img src=\"data:image/png;base64,{base64}\"/>"); } output.TagName = null; }
public static string createQRCode(string sceneName, string host) { NameBasedGenerator uuidCreator = new NameBasedGenerator(HashType.SHA1); ImageFormat imageFormat = ImageFormat.Png; string markerUUID = uuidCreator.GenerateGuid(sceneName + DateTime.Now).ToString(); string url = host + "/QRCode/Open?uuid="; string content = url + markerUUID; QrCode qr = QrCode.EncodeText(content, QrCode.Ecc.Medium); string dir = Directory.GetCurrentDirectory(); string filename = markerUUID + "." + imageFormat.ToString(); var path = Path.Combine( dir, "static", "content", "marker", filename); using (var stream = new FileStream(path, FileMode.Create)) using (var bitmap = qr.ToBitmap(40, 1)) { bitmap.Save(stream, imageFormat); } return(filename); }
/// <summary> /// Draws the QR code to the specified graphics context (canvas). The QR code will /// always be 46 mm by 46 mm. /// </summary> /// <param name="graphics">The graphics context.</param> /// <param name="offsetX">The x offset.</param> /// <param name="offsetY">The y offset.</param> internal void Draw(ICanvas graphics, double offsetX, double offsetY) { QrCode qrCode = QrCode.EncodeText(_embeddedText, QrCode.Ecc.Medium); bool[,] modules = CopyModules(qrCode); ClearSwissCrossArea(modules); int modulesPerSide = modules.GetLength(0); graphics.SetTransformation(offsetX, offsetY, 0, Size / modulesPerSide / 25.4 * 72, Size / modulesPerSide / 25.4 * 72); graphics.StartPath(); DrawModulesPath(graphics, modules); graphics.FillPath(0); graphics.SetTransformation(offsetX, offsetY, 0, 1, 1); // Swiss cross graphics.StartPath(); graphics.AddRectangle(20, 20, 6, 6); graphics.FillPath(0); const double barWidth = 7 / 6.0; const double barLength = 35 / 9.0; graphics.StartPath(); graphics.AddRectangle(23 - barWidth / 2, 23 - barLength / 2, barWidth, barLength); graphics.AddRectangle(23 - barLength / 2, 23 - barWidth / 2, barLength, barWidth); graphics.FillPath(0xffffff); }
internal static void QrCodeWithImage() { var text = "https://github.com/manuelbl/QrCodeGenerator"; var filename = "qr-code-with-image.png"; var logoFilename = "heart.png"; const float logoWidth = 0.15f; // logo will have 15% the width of the QR code var qr = QrCode.EncodeText(text, QrCode.Ecc.Medium); using (var bitmap = qr.ToBitmap(scale: 10, border: 4)) using (var logo = Image.Load(logoFilename)) { // resize logo var w = (int)Math.Round(bitmap.Width * logoWidth); logo.Mutate(logo => logo.Resize(w, 0)); // draw logo in center var topLeft = new Point((bitmap.Width - logo.Width) / 2, (bitmap.Height - logo.Height) / 2); bitmap.Mutate(img => img.DrawImage(logo, topLeft, 1)); // save as PNG bitmap.SaveAsPng(filename); } Console.WriteLine($"The QR code has been saved as {Path.GetFullPath(filename)}"); }
private void RegenerateQr() { var qr = QrCode.EncodeText(SourceText.Text, QrCode.Ecc.Medium); var bitmap = qr.ToBitmap(scale: 10, border: 1); //bitmap.Save("qr-code.png", ImageFormat.Png); QrPicture.Image = bitmap; }
private byte[] GetBase64QrCodeData(string url) { var qrCode = QrCode.EncodeText(url, Options.GetQrCodeErrorCorrectionLevel()); var img = qrCode.ToBitmap(Options.QrCodeScale, Options.QrCodeBorderSize); MemoryStream stream = new MemoryStream(); img.Save(stream, ImageFormat.Bmp); return(stream.ToArray()); }
// Creates a single QR code, then writes it to an SVG file. private static void DoBasicDemo() { const string text = "Hello, world!"; // User-supplied Unicode text var errCorLvl = QrCode.Ecc.Low; // Error correction level var qr = QrCode.EncodeText(text, errCorLvl); // Make the QR code symbol SaveAsSvg(qr, "hello-world-QR.svg", border: 4); // Save as SVG }
private void CopyButton_Click(object sender, RoutedEventArgs e) { // put the QR code on the clipboard as a bitmap var qrCode = QrCode.EncodeText(_text, ErrorCorrection); var bitmap = QrCodeDrawing.CreateBitmapImage(qrCode, 20, BorderWidth); var dataObject = new DataObject(); dataObject.SetData(DataFormats.Bitmap, bitmap); Clipboard.SetDataObject(dataObject); }
public ActionResult <byte[]> GenerateSvg([FromQuery(Name = "text")] string text, [FromQuery(Name = "ecc")] int?ecc, [FromQuery(Name = "border")] int?borderWidth) { ecc = Math.Clamp(ecc ?? 1, 0, 3); borderWidth = Math.Clamp(borderWidth ?? 3, 0, 999999); var qrCode = QrCode.EncodeText(text, errorCorrectionLevels[(int)ecc]); byte[] svg = Encoding.UTF8.GetBytes(qrCode.ToSvgString((int)borderWidth)); return(new FileContentResult(svg, "image/svg+xml; charset=utf-8")); }
// Create a QR code and save it as a PNG. internal static void Main() { var text = "Hello, world!"; var filename = "hello-world-QR.png"; var qr = QrCode.EncodeText(text, QrCode.Ecc.Medium); // Create the QR code symbol qr.SaveAsPng(filename, scale: 10, border: 4); Console.WriteLine($"The QR code has been saved as {Path.GetFullPath(filename)}"); }
public ActionResult <byte[]> GeneratePng([FromQuery(Name = "text")] string text, [FromQuery(Name = "ecc")] int?ecc, [FromQuery(Name = "border")] int?borderWidth) { ecc = Math.Clamp(ecc ?? 1, 0, 3); borderWidth = Math.Clamp(borderWidth ?? 3, 0, 999999); var qrCode = QrCode.EncodeText(text, errorCorrectionLevels[(int)ecc]); byte[] png = qrCode.ToPng(20, (int)borderWidth); return(new FileContentResult(png, "image/png")); }
// Creates QR Codes with manually specified segments for better compactness. private static void DoSegmentDemo() { QrCode qr; List <QrSegment> segs; // Illustration "silver" var silver0 = "THE SQUARE ROOT OF 2 IS 1."; var silver1 = "41421356237309504880168872420969807856967187537694807317667973799"; qr = QrCode.EncodeText(silver0 + silver1, Ecc.Low); WritePng(qr.ToImage(10, 3), "sqrt2-monolithic-QR.png"); segs = new List <QrSegment>() { QrSegment.MakeAlphanumeric(silver0), QrSegment.MakeNumeric(silver1) }; qr = QrCode.EncodeSegments(segs, Ecc.Low); WritePng(qr.ToImage(10, 3), "sqrt2-segmented-QR.png"); // Illustration "golden" var golden0 = "Golden ratio φ = 1."; var golden1 = "6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374"; var golden2 = "......"; qr = QrCode.EncodeText(golden0 + golden1 + golden2, Ecc.Low); WritePng(qr.ToImage(8, 5), "phi-monolithic-QR.png"); segs = new List <QrSegment>() { QrSegment.MakeBytes(Encoding.UTF8.GetBytes(golden0)), QrSegment.MakeNumeric(golden1), QrSegment.MakeAlphanumeric(golden2) }; qr = QrCode.EncodeSegments(segs, Ecc.Low); WritePng(qr.ToImage(8, 5), "phi-segmented-QR.png"); // Illustration "Madoka": kanji, kana, Cyrillic, full-width Latin, Greek characters var madoka = "「魔法少女まどか☆マギカ」って、 ИАИ desu κα?"; qr = QrCode.EncodeText(madoka, Ecc.Low); WritePng(qr.ToImage(9, 4), "madoka-utf8-QR.png"); segs = new List <QrSegment>() { QrSegmentAdvanced.MakeKanji(madoka) }; qr = QrCode.EncodeSegments(segs, Ecc.Low); WritePng(qr.ToImage(9, 4), "madoka-kanji-QR.png"); }
private void ShowEditorLocalIP() { string getLocalIP() { var host = Dns.GetHostEntry(Dns.GetHostName()); foreach (var ip in host.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork) { return(ip.ToString()); } } throw new Exception("no local ip found."); } // ローカルIPを取得 var localIp = getLocalIP(); // qrコードを生成する var qr = QrCode.EncodeText(localIp, QrCode.Ecc.Medium); var(bits, w, h) = qr.ToBitPattern(); // デフォサイズが441, 21x21、これが仕様上最小 // テクスチャにポイントを描く。 var drawTexture = new Texture2D(w, h, TextureFormat.RGBA32, false); drawTexture.filterMode = FilterMode.Point; for (var i = 0; i < w * h; i++) { var isOn = bits[i]; if (isOn) { var x = i / w; var y = i % h; drawTexture.SetPixels(x, y, 1, 1, new Color[] { Color.black }, 0); } } drawTexture.Apply(); var canvasResourcePrefab = Resources.Load("a-npanResource/a-npanQRCanvas") as GameObject; var canvasObj = GameObject.Instantiate(canvasResourcePrefab); var image = canvasObj.transform.GetChild(0).GetComponent <Image>(); image.sprite = Sprite.Create(drawTexture, new Rect(0, 0, drawTexture.width, drawTexture.height), Vector2.zero); }
// Creates a single QR Code, then writes it to a PNG file and an SVG file. private static void DoBasicDemo() { var text = "Hello, world!"; // User-supplied Unicode text var errCorLvl = Ecc.Low; // Error correction level var qr = QrCode.EncodeText(text, errCorLvl); // Make the QR Code symbol var img = qr.ToImage(10, 4); // Convert to bitmap image WritePng(img, "hello-world-QR.png"); // Write image to file var svg = qr.ToSvgString(4); // Convert to SVG XML code File.WriteAllText("hello-world-QR.svg", svg); // Write image to file }
public static void RegenerateQr(int resolution, string text, PictureBox QrPicture, ProgressBar progressBar1) { progressBar1.Value = 0; if (text.Equals("")) { return; } progressBar1.Value = 80; var qr = QrCode.EncodeText(text, QrCode.Ecc.Medium); var bitmap = qr.ToBitmap(scale: resolution, border: 1); progressBar1.Value = 100; //bitmap.Save("qr-code.png", ImageFormat.Png); QrPicture.Image = bitmap; }
// Creates a single QR code, then writes it to a PNG file and an SVG file. private static void DoBasicDemo() { const string text = "Hello, world!"; // User-supplied Unicode text var errCorLvl = QrCode.Ecc.Low; // Error correction level var qr = QrCode.EncodeText(text, errCorLvl); // Make the QR code symbol using (var img = qr.ToBitmap(10, 4)) // Convert to bitmap image { img.Save("hello-world-QR.png", ImageFormat.Png); // Write image to file } string svg = qr.ToSvgString(4); // Convert to SVG XML code File.WriteAllText("hello-world-QR.svg", svg, Encoding.UTF8); // Write image to file }
static void Main() { var vcard = new VCard { Version = VCardVersion.V3, FirstName = "Robin", LastName = "Hood", Organization = "Sherwood Inc.", Addresses = new List <Address> { new Address { Type = AddressType.Work, Street = "The Major Oak", Locality = "Sherwood Forest", PostalCode = "NG21 9RN", Country = "United Kingdom", } }, Telephones = new List <Telephone> { new Telephone { Type = TelephoneType.Work, Number = "+441623677321" } }, Emails = new List <Email> { new Email { Type = EmailType.Smtp, EmailAddress = "*****@*****.**" } } }; var qrCode = QrCode.EncodeText(vcard.Serialize(), QrCode.Ecc.Medium); File.WriteAllText("vcard-qrcode.svg", qrCode.ToSvgString(3)); }
private void UpdateQrCode() { var qrCode = QrCode.EncodeText(_text, ErrorCorrection); QrCodeImage.Source = QrCodeDrawing.CreateDrawing(qrCode, 192, BorderWidth); }
/// <summary> /// Draws the QR code to the specified graphics context (canvas). The QR code will /// always be 46 mm by 46 mm. /// </summary> /// <param name="graphics">The graphics context.</param> /// <param name="offsetX">The x offset.</param> /// <param name="offsetY">The y offset.</param> internal void Draw(ICanvas graphics, double offsetX, double offsetY) { var qrCode = QrCode.EncodeText(_embeddedText, QrCode.Ecc.Medium); var modules = CopyModules(qrCode); ClearSwissCrossArea(modules); var modulesPerSide = modules.GetLength(0); graphics.SetTransformation(offsetX, offsetY, 0, Size / modulesPerSide / 25.4 * 72, Size / modulesPerSide / 25.4 * 72); graphics.StartPath(); DrawModulesPath(graphics, modules); graphics.FillPath(0, false); graphics.SetTransformation(offsetX, offsetY, 0, 1, 1); // Swiss cross graphics.StartPath(); graphics.AddRectangle(20, 20, 6, 6); graphics.FillPath(0, false); const double barWidth = 7 / 6.0; const double barLength = 35 / 9.0; graphics.StartPath(); // A----B // | | // | | // K-----L C-----D // | | // | | // J-----I F-----E // | | // | | // H----G // Center is (23;23) // Start in A graphics.MoveTo(23 - barWidth / 2, 23 - barLength / 2); // Line to B graphics.LineTo(23 + barWidth / 2, 23 - barLength / 2); // Line to C graphics.LineTo(23 + barWidth / 2, 23 - barWidth / 2); // Line to D graphics.LineTo(23 + barLength / 2, 23 - barWidth / 2); // Line to E graphics.LineTo(23 + barLength / 2, 23 + barWidth / 2); // Line to F graphics.LineTo(23 + barWidth / 2, 23 + barWidth / 2); // Line to G graphics.LineTo(23 + barWidth / 2, 23 + barLength / 2); // Line to H graphics.LineTo(23 - barWidth / 2, 23 + barLength / 2); // Line to I graphics.LineTo(23 - barWidth / 2, 23 + barWidth / 2); // Line to J graphics.LineTo(23 - barLength / 2, 23 + barWidth / 2); // Line to K graphics.LineTo(23 - barLength / 2, 23 - barWidth / 2); // Line to K graphics.LineTo(23 - barWidth / 2, 23 - barWidth / 2); graphics.FillPath(0xffffff, false); }