public IActionResult Get() { var req = this.Request; switch (req.Host.Host) { case "localhost": case "127.0.0.1": return(Content("地址为127.0.0.1或localhost时,二维码不可用。")); } var obj = new { Name = name, Url = $"{req.Scheme}://{req.Host}", Password = password }; ZXing.Writer formatWriter = new ZXing.MultiFormatWriter(); ZXing.IBarcodeWriterSvg barcodeWriter = new ZXing.BarcodeWriterSvg(); ZXing.BarcodeFormat barcodeFormat = ZXing.BarcodeFormat.QR_CODE; Dictionary <ZXing.EncodeHintType, object> hints = new Dictionary <ZXing.EncodeHintType, object>() { [ZXing.EncodeHintType.CHARACTER_SET] = "UTF-8" }; ZXing.Common.BitMatrix bitMatrix = formatWriter.encode(JsonConvert.SerializeObject(obj), barcodeFormat, 400, 400, hints); var svgImage = barcodeWriter.Write(bitMatrix); var svgContent = svgImage.Content; return(this.Content(svgContent, "image/svg+xml", Encoding.UTF8)); }
private void RenderCard() { var bounds = ApplicationView.GetForCurrentView().VisibleBounds; if (App.IsMobile) { bcImage.Width = bounds.Width; bcImage.Height = bounds.Width * .5625; bcGrid.Height = bcImage.Height + 20; bcGrid.Width = bounds.Width; bcGrid.Margin = new Thickness(0, 70, 0, 0); bcNumber.Width = bounds.Width; } else { bcImage.Width = bounds.Width * .40; bcImage.Height = (bounds.Width * .40) * .5625; bcGrid.Height = bcImage.Height + 20; bcGrid.Width = (bounds.Width * .40); bcGrid.Margin = new Thickness(0, 70, 0, 0); bcNumber.Width = bounds.Width * .40; } bcNumber.Text = theCard.Number; bcNumber.TextAlignment = TextAlignment.Center; ZXing.MultiFormatWriter mfw = new ZXing.MultiFormatWriter(); try { ZXing.Common.BitMatrix bm = mfw.encode(theCard.Number, theCard.Format, (int)bcImage.Width, (int)bcImage.Height); ZXing.Mobile.WriteableBitmapRenderer w = new WriteableBitmapRenderer(); ZXing.Common.EncodingOptions eo = new ZXing.Common.EncodingOptions(); eo.Height = (int)bcImage.Height; eo.Width = (int)bcImage.Width; WriteableBitmap wbm = w.Render(bm, theCard.Format, theCard.Number, eo); bcImage.Source = wbm; } catch(Exception e) { CardEncodeErr.Text = e.Message; CardEncodeErr.Visibility = Visibility.Visible; } }
private void RenderCard() { var bounds = ApplicationView.GetForCurrentView().VisibleBounds; if (App.IsMobile) { bcImage.Width = bounds.Width; if (theCard.Format == ZXing.BarcodeFormat.PDF_417) { bcImage.Height = bounds.Width * .35; bcNumber.Margin = new Thickness(0, 0, 0, 3); } else { bcImage.Height = bounds.Width * .4; bcNumber.Margin = new Thickness(0, 0, 0, 0); } bcGrid.VerticalAlignment = VerticalAlignment.Top; bcGrid.Height = bcImage.Height + 20; bcGrid.Width = bounds.Width; bcGrid.Margin = new Thickness(0, 70, 0, 0); bcNumber.Width = bounds.Width; } else { if (theCard.Format == ZXing.BarcodeFormat.PDF_417) { bcImage.Height = (bounds.Width * .40) * .3; } else { bcImage.Height = (bounds.Width * .40) * .5625; } bcImage.Width = bounds.Width * .40; bcImage.HorizontalAlignment = HorizontalAlignment.Center; bcImage.VerticalAlignment = VerticalAlignment.Center; bcGrid.Height = bcImage.Height + 20; bcGrid.Width = (bounds.Width * .40); bcGrid.Margin = new Thickness(0, 70, 0, 0); bcNumber.Width = bounds.Width * .40; if (bounds.Width > 800) bcNumber.FontSize = 20; else if (bounds.Width > 600 && Width <= 800) bcNumber.FontSize = 16; else if (bounds.Width <= 600) bcNumber.FontSize = 11; } bcNumber.Text = theCard.Number; bcNumber.TextAlignment = TextAlignment.Center; ZXing.MultiFormatWriter mfw = new ZXing.MultiFormatWriter(); try { var scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel; ZXing.Common.BitMatrix bm; if (App.IsMobile) bm = mfw.encode(theCard.Number, theCard.Format, (int)(bcImage.Width * (scaleFactor - 1)), (int)(bcImage.Height * (scaleFactor - 1))); else bm = mfw.encode(theCard.Number, theCard.Format, (int)bcImage.Width, (int)bcImage.Height); ZXing.Mobile.WriteableBitmapRenderer w = new WriteableBitmapRenderer(); ZXing.Common.EncodingOptions eo = new ZXing.Common.EncodingOptions(); eo.Height = (int)bcImage.Height; eo.Width = (int)bcImage.Width; WriteableBitmap wbm = w.Render(bm, theCard.Format, theCard.Number, eo); bcImage.Source = wbm; } catch(Exception e) { CardEncodeErr.Text = e.Message; CardEncodeErr.Visibility = Visibility.Visible; } }