/// <summary> /// Generate QR Code image as base64 string /// </summary> public string GetAsBase64Image() { generator ??= new QRCodeGenerator(); var eccLevel = EccLevel switch { EccLevel.L => QRCodeGenerator.ECCLevel.L, EccLevel.M => QRCodeGenerator.ECCLevel.M, EccLevel.Q => QRCodeGenerator.ECCLevel.Q, EccLevel.H => QRCodeGenerator.ECCLevel.H, _ => throw new ArgumentOutOfRangeException() }; var darkColor = GetColorBytes(DarkColor); var lightColor = GetColorBytes(LightColor); using var data = Payload == null ? generator.CreateQrCode(Value, eccLevel) : generator.CreateQrCode(Payload, eccLevel); var code = new PngByteQRCode(data); var bytes = code.GetGraphic(PixelsPerModule, darkColor, lightColor, DrawQuietZones); const string pngData = "data:image/png;base64, "; return(pngData + Convert.ToBase64String(bytes)); }
/// <summary> /// Generate a setup code for a Google Authenticator user to scan /// </summary> /// <param name="issuer">Issuer ID (the name of the system, i.e. 'MyApp'), can be omitted but not recommended https://github.com/google/google-authenticator/wiki/Key-Uri-Format </param> /// <param name="accountTitleNoSpaces">Account Title (no spaces)</param> /// <param name="accountSecretKey">Account Secret Key as byte[]</param> /// <param name="QRPixelsPerModule">Number of pixels per QR Module (2 = ~120x120px QRCode)</param> /// <returns>SetupCode object</returns> public SetupCode GenerateSetupCode(string issuer, string accountTitleNoSpaces, byte[] accountSecretKey, int QRPixelsPerModule, bool generateQrCode = true) { if (String.IsNullOrWhiteSpace(accountTitleNoSpaces)) { throw new NullReferenceException("Account Title is null"); } accountTitleNoSpaces = RemoveWhitespace(Uri.EscapeUriString(accountTitleNoSpaces)); string encodedSecretKey = Base32Encoding.ToString(accountSecretKey); string provisionUrl; if (String.IsNullOrWhiteSpace(issuer)) { provisionUrl = String.Format("otpauth://totp/{0}?secret={1}", accountTitleNoSpaces, encodedSecretKey.Trim('=')); } else { // https://github.com/google/google-authenticator/wiki/Conflicting-Accounts // Added additional prefix to account otpauth://totp/Company:[email protected] for backwards compatibility provisionUrl = String.Format("otpauth://totp/{2}:{0}?secret={1}&issuer={2}", accountTitleNoSpaces, encodedSecretKey.Trim('='), UrlEncode(issuer)); } string qrCodeUrl = string.Empty; if (generateQrCode) { using (QRCodeData qrCodeData = QRCodeGenerator.CreateQrCode(provisionUrl, QRCodeGenerator.ECCLevel.Q)) using (PngByteQRCode qrCode = new PngByteQRCode(qrCodeData)) { qrCodeUrl = $"data:image/png;base64,{Convert.ToBase64String(qrCode.GetGraphic(QRPixelsPerModule))}"; } } return(new SetupCode(accountTitleNoSpaces, encodedSecretKey.Trim('='), qrCodeUrl)); }
private static async Task GenerateQRCodeAsync(LinkBundle linkDocument, HttpRequest req, Binder binder) { req.Headers.TryGetValue("Origin", out StringValues origin); Url generator = new Url($"{origin.ToString()}/{linkDocument.VanityUrl}"); string payload = generator.ToString(); QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q); PngByteQRCode qrCode = new PngByteQRCode(qrCodeData); byte[] qrCodeAsPngByteArr = qrCode.GetGraphic(20); var attributes = new Attribute[] { new BlobAttribute(blobPath: $"{QRCODECONTAINER}/{linkDocument.VanityUrl}.png", FileAccess.Write), new StorageAccountAttribute("AzureWebJobsStorage") }; using (var writer = await binder.BindAsync <CloudBlobStream>(attributes).ConfigureAwait(false)) { writer.Write(qrCodeAsPngByteArr); } }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string url = req.Query["url"]; string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); url = url ?? data?.name; if (string.IsNullOrEmpty(url)) { return(new BadRequestResult()); } var isAbsoluteUrl = Uri.TryCreate(url, UriKind.Absolute, out Uri resultUrl); if (!isAbsoluteUrl) { return(new BadRequestResult()); } var generator = new Url(resultUrl.AbsoluteUri); var payload = generator.ToString(); using (var qrGenerator = new QRCodeGenerator()) { var qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q); var qrCode = new PngByteQRCode(qrCodeData); var qrCodeAsPng = qrCode.GetGraphic(20); return(new FileContentResult(qrCodeAsPng, "image/png")); } }
public string RenderQrCode(string text) { var data = _generator.CreateQrCode(text, QRCodeGenerator.ECCLevel.Q, forceUtf8: true); var code = new PngByteQRCode(data); var encoded = code.GetGraphic(20); return($"data:image/png;base64, {Convert.ToBase64String(encoded)}"); }
public byte[] GetCodeBytes(string code) { QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q); PngByteQRCode qrCode = new PngByteQRCode(qrCodeData); return(qrCode.GetGraphic(32, new byte[] { 0, 0, 0 }, new byte[] { 255, 255, 255 })); }
/// <summary> /// 绘制二维码(有边距,不支持自定义颜色) /// </summary> /// <param name="content"></param> /// <returns>PNG</returns> public byte[] RenderPngBytes(string content) { QRCodeGenerator generator = new QRCodeGenerator(); QRCodeData data = generator.CreateQrCode(content, _eccLevel); var qrCode = new PngByteQRCode(data); return(qrCode.GetGraphic(Config.Size)); }
public QRClient ToByte() { var qrGenerator = new QRCodeGenerator(); var qrCodeData = qrGenerator.CreateQrCode(Code, QRCodeGenerator.ECCLevel.Q); PngByteQRCode qrCode = new PngByteQRCode(qrCodeData); Byte = qrCode.GetGraphic(20); return(this); }
private static string CreateQRCode(string text) { var qrGenerator = new QRCodeGenerator(); var qrCodeData = qrGenerator.CreateQrCode(text, QRCodeGenerator.ECCLevel.L); var qrCode = new PngByteQRCode(qrCodeData); var qrCodeImage = qrCode.GetGraphic(4); return(Convert.ToBase64String(qrCodeImage)); }
public IViewComponentResult Invoke(string data) { QRCodeData qrCodeData = qrGenerator.CreateQrCode(data, QRCodeGenerator.ECCLevel.Q); PngByteQRCode qrCode = new PngByteQRCode(qrCodeData); var bytes = qrCode.GetGraphic(5, new byte[] { 0, 0, 0, 255 }, new byte[] { 0xf5, 0xf5, 0xf7, 255 }, true); var b64 = Convert.ToBase64String(bytes); return(new HtmlContentViewComponentResult(new HtmlString($"<img height=\"256\" style=\"image-rendering: pixelated;image-rendering: -moz-crisp-edges;\" src=\"data:image/png;base64,{b64}\" />"))); }
private byte[] GetQRImage(string content) { using (var qrCodeGenerator = new QRCodeGenerator()) using (QRCodeData data = qrCodeGenerator.CreateQrCode(content, QRCodeGenerator.ECCLevel.L)) using (var qrCode = new PngByteQRCode(data)) { return(qrCode.GetGraphic(4)); } }
public ImageSource GetImage(string content) { QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(content, QRCodeGenerator.ECCLevel.M); PngByteQRCode qRCode = new PngByteQRCode(qrCodeData); byte[] qrCodeBytes = qRCode.GetGraphic(20); return(ImageSource.FromStream(() => new System.IO.MemoryStream(qrCodeBytes))); }
public static Byte[] CreateQrArray(string content) { QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(content, QRCodeGenerator.ECCLevel.Q);; PngByteQRCode qrCode = new PngByteQRCode(qrCodeData); var bitmap = qrCode.GetGraphic(2); //var stream = new MemoryStream(bitmap); return(bitmap); }
/// <summary> /// Create a QR code byte[] data /// </summary> /// <param name="data">Your code</param> /// <returns></returns> public byte[] CreateBytePngQrCode(string data) { using (QRCodeGenerator qrGenerator = new QRCodeGenerator()) { QRCodeData qrCodeData = qrGenerator.CreateQrCode(data, QRCodeGenerator.ECCLevel.Q); PngByteQRCode qrCode = new PngByteQRCode(qrCodeData); byte[] qrCodeAsPngByteArr = qrCode.GetGraphic(pixelsPerModule_QRpara); return(qrCodeAsPngByteArr); } }
public static NSData GenerateQR(string data) { QRCodeGenerator _generator = new QRCodeGenerator(); QRCodeData _data = _generator.CreateQrCode(data, QRCodeGenerator.ECCLevel.L); PngByteQRCode _qr = new PngByteQRCode(_data); byte[] imgArr = _qr.GetGraphic(20); cacheQR = new NSImage(NSData.FromArray(imgArr)); return(NSData.FromArray(imgArr)); }
public IActionResult GetQRCode(string data) { using (var qrGenerator = new QRCodeGenerator()) using (var qrCodeData = qrGenerator.CreateQrCode(data, QRCodeGenerator.ECCLevel.L)) using (var pngByteQrCode = new PngByteQRCode(qrCodeData)) { var pngBytes = pngByteQrCode.GetGraphic(20, false); return(File(pngBytes, "image/png")); } }
public string GerarQrCode(string emv) { QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(emv, QRCodeGenerator.ECCLevel.Q); PngByteQRCode qrCode = new PngByteQRCode(qrCodeData); byte[] qrCodeAsPngByteArr = qrCode.GetGraphic(20); return(Convert.ToBase64String(qrCodeAsPngByteArr)); }
public static byte[] GenPngByteFromUrl(string msg) { var payload = new PayloadGenerator.Url(msg); QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q); PngByteQRCode qrCode = new PngByteQRCode(qrCodeData); byte[] qrCodeAsPngByteArr = qrCode.GetGraphic(20); return(qrCodeAsPngByteArr); }
public async Task <ActionResult <byte[]> > Generate(string link) { return(await Task.Run(() => { using var qrGenerator = new QRCodeGenerator(); using var qrCodeData = qrGenerator.CreateQrCode(link, QRCodeGenerator.ECCLevel.Q); using var qrCode = new PngByteQRCode(qrCodeData); byte[] qrCodeAsBitmapByteArr = qrCode.GetGraphic(20); return File(qrCodeAsBitmapByteArr, "image/png"); })); }
/// <summary> /// Generates a QR code as image. /// </summary> /// <param name="text">The text to encode in the QR code.</param> /// <param name="size">The desired size (width and height equal) for the image.</param> /// <returns>Returns the binary representation of the image.</returns> /// <seealso cref="IQrCodeProvider"/> public byte[] GetQrCodeImage(string text, int size) { using (var generator = new QRCodeGenerator()) using (var qrcodedata = generator.CreateQrCode(text, MapECCLevel(ErrorCorrectionLevel), _forceutf8, _utf8bom, MapEcimode(EciMode), _requestedversion)) using (var qrcode = new PngByteQRCode(qrcodedata)) return(qrcode.GetGraphic( (int)Math.Ceiling(size / (double)qrcodedata.ModuleMatrix.Count), new[] { ForegroundColor.R, ForegroundColor.G, ForegroundColor.B }, new[] { BackgroundColor.R, BackgroundColor.G, BackgroundColor.B } )); }
public void SavePngQRCode(string path, string text) { var qrCodeData = _qrCodeGenerator.CreateQrCode(text, QRCodeGenerator.ECCLevel.H); var qrCode = new PngByteQRCode(qrCodeData); var byteArray = qrCode.GetGraphic(20); using (var image = Image.FromStream(new MemoryStream(byteArray))) { image.Save(path, ImageFormat.Png); } }
static async Task <BitmapImage> GetQRCodeAsync(string content) { using var generator = new QRCodeGenerator(); using var data = generator.CreateQrCode(content, QRCodeGenerator.ECCLevel.Q); using var qrcode = new PngByteQRCode(data); var imgBytes = qrcode.GetGraphic(20); var img = new BitmapImage(); await img.SetSourceAsync(new MemoryStream(imgBytes).AsRandomAccessStream()); return(img); }
private void GenQrCode() { if (curQrCodeText is null) { QrCodeImage = null; } else { IsLoading = true; Color c = GetQrCodeBackgroundColor(); Task.Run(async() => { QRCodeData qRCodeData = QR_CODE_GENERATOR.CreateQrCode(curQrCodeText, QRCodeGenerator.ECCLevel.Q); PngByteQRCode qRCode = new PngByteQRCode(qRCodeData); byte[] qRCodeGraphic; if (DefaultQrCode) { qRCodeGraphic = qRCode.GetGraphic(10); } else { qRCodeGraphic = qRCode.GetGraphic(10, new byte[] { (byte)~c.R, (byte)~c.G, (byte)~c.B }, new byte[] { c.R, c.G, c.B }); } await SharedUtils.CallDispatcherAsync(async() => { using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream()) { using (DataWriter writer = new DataWriter(stream.GetOutputStreamAt(0))) { writer.WriteBytes(qRCodeGraphic); await writer.StoreAsync(); } QrCodeImage = new BitmapImage(); await QrCodeImage.SetSourceAsync(stream); } IsLoading = false; }); }); } }
string IVerificationService.CreateQRCode(string url) { QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(url, QRCodeGenerator.ECCLevel.Q); PngByteQRCode qrCode = new PngByteQRCode(qrCodeData); byte[] qrCodeAsPngByteArr = qrCode.GetGraphic(20); string base64String = Convert.ToBase64String(qrCodeAsPngByteArr); return(base64String); }
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e) { InfoText.Text += "Please Scan the QRCode\n"; using var qrGenerator = new QRCodeGenerator(); var qrCodeData = qrGenerator.CreateQrCode(StrGuid, QRCodeGenerator.ECCLevel.Q); var qrCode = new PngByteQRCode(qrCodeData); var qrCodeAsPng = qrCode.GetGraphic(20); QrCodeImage.Source = ToImage(qrCodeAsPng); Client.Timeout = 1000; }
public static byte[] GenerateQRCodeAsBytes(string content, int pixelsPerModule = 4) { var qrCodeData = GenerateQRCode(content); byte[] qrCodeImageAsByte = null; using (var qrCode = new PngByteQRCode(qrCodeData)) { qrCodeImageAsByte = qrCode.GetGraphic(pixelsPerModule); } return(qrCodeImageAsByte); }
public ActionResult QrCode() { string url = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}"; QRCodeGenerator qrGen = new QRCodeGenerator(); QRCodeData qrData = qrGen.CreateQrCode(url, QRCodeGenerator.ECCLevel.H); PngByteQRCode qrCode = new PngByteQRCode(qrData); byte[] qrCodeAsPngByteArr = qrCode.GetGraphic(20); return(File(qrCodeAsPngByteArr, "application/octet-stream")); }
/// <summary> /// Generates a QR code for BankID using the auto start token. /// </summary> /// <remarks> /// First the token is added to the BankID URL for auto start and /// then the QR code is generated from the resulting URL. /// </remarks> /// <param name="autoStartToken"></param> /// <returns>A base 64 representation of the QR code</returns> public string GenerateQrCodeAsBase64(string autoStartToken) { var queryPart = GetQueryPart(autoStartToken); var qrUrl = $"bankid:///{queryPart}"; using var qrGenerator = new QRCodeGenerator(); var qrCodeData = qrGenerator.CreateQrCode(qrUrl, QRCodeGenerator.ECCLevel.Q); using var qrCode = new PngByteQRCode(qrCodeData); var pngQrCode = qrCode.GetGraphic(PixelsPerModule); var base64QrCpde = Convert.ToBase64String(pngQrCode); return(base64QrCpde); }
public FileContentResult Get([FromQuery] string number) { if (string.IsNullOrEmpty(number)) { number = "19132706063"; } _generator = new QRCodeGenerator(); QRCodeData qrCodeData = _generator.CreateQrCode($"sms:+{number}?body=Hi", QRCodeGenerator.ECCLevel.Q); _qrCode = new PngByteQRCode(qrCodeData); return(File(_qrCode.GetGraphic(10), "image/png")); }
public void Start(string url) { LabelUrl.Text = url; Path = url; //Barcode generation thru QRCoder QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(url, QRCodeGenerator.ECCLevel.Q); PngByteQRCode qRCode = new PngByteQRCode(qrCodeData); byte[] qrCodeBytes = qRCode.GetGraphic(5); BarCode.Source = ImageSource.FromStream(() => new MemoryStream(qrCodeBytes)); }