2010-09-25 22 views
28

Czy istnieje .NET API, który generuje QR Codes, taki jak ten?Generowanie kodu QR w ASP.NET MVC

Meagre human needs a phone to read QR codes. Ha ha ha.

Chciałbym, aby wyświetlić je na stronach, że użytkownicy oczekują moi wydrukować.

+4

Kod QR brzmi: "Mizerny człowiek potrzebuje telefonu do czytania kodów QR. Ha ha ha. "Lovely. :) –

+2

Nie wiem, dlaczego został ustawiony jako nie na temat. Uważam, że jest dokładnie na temat ...:/ –

Odpowiedz

53

pisałem podstawową metodę HTML pomocnika do emisji poprawnego <img> znacznik, aby skorzystać z API Google. Tak więc, na swojej stronie (zakładając ASPX widok silnika) używać coś takiego:

<%: Html.QRCodeImage(Request.Url.AbsolutePath) %> 
<%: Html.QRCodeImage("Meagre human needs a phone to read QR codes. Ha ha ha.") %> 

Albo jeśli chcesz określić rozmiar w pikselach (obraz jest zawsze kwadratowa):

<%: Html.QRCodeImage(Request.Url.AbsolutePath, size: 92) %> 

Oto kod:

public static class QRCodeHtmlHelper 
{ 
    /// <summary> 
    /// Produces the markup for an image element that displays a QR Code image, as provided by Google's chart API. 
    /// </summary> 
    /// <param name="htmlHelper"></param> 
    /// <param name="data">The data to be encoded, as a string.</param> 
    /// <param name="size">The square length of the resulting image, in pixels.</param> 
    /// <param name="margin">The width of the border that surrounds the image, measured in rows (not pixels).</param> 
    /// <param name="errorCorrectionLevel">The amount of error correction to build into the image. Higher error correction comes at the expense of reduced space for data.</param> 
    /// <param name="htmlAttributes">Optional HTML attributes to include on the image element.</param> 
    /// <returns></returns> 
    public static MvcHtmlString QRCode(this HtmlHelper htmlHelper, string data, int size = 80, int margin = 4, QRCodeErrorCorrectionLevel errorCorrectionLevel = QRCodeErrorCorrectionLevel.Low, object htmlAttributes = null) 
    { 
     if (data == null) 
      throw new ArgumentNullException("data"); 
     if (size < 1) 
      throw new ArgumentOutOfRangeException("size", size, "Must be greater than zero."); 
     if (margin < 0) 
      throw new ArgumentOutOfRangeException("margin", margin, "Must be greater than or equal to zero."); 
     if (!Enum.IsDefined(typeof(QRCodeErrorCorrectionLevel), errorCorrectionLevel)) 
      throw new InvalidEnumArgumentException("errorCorrectionLevel", (int)errorCorrectionLevel, typeof (QRCodeErrorCorrectionLevel)); 

     var url = string.Format("http://chart.apis.google.com/chart?cht=qr&chld={2}|{3}&chs={0}x{0}&chl={1}", size, HttpUtility.UrlEncode(data), errorCorrectionLevel.ToString()[0], margin); 

     var tag = new TagBuilder("img"); 
     if (htmlAttributes != null) 
      tag.MergeAttributes(new RouteValueDictionary(htmlAttributes)); 
     tag.Attributes.Add("src", url); 
     tag.Attributes.Add("width", size.ToString()); 
     tag.Attributes.Add("height", size.ToString()); 

     return new MvcHtmlString(tag.ToString(TagRenderMode.SelfClosing)); 
    } 
} 

public enum QRCodeErrorCorrectionLevel 
{ 
    /// <summary>Recovers from up to 7% erroneous data.</summary> 
    Low, 
    /// <summary>Recovers from up to 15% erroneous data.</summary> 
    Medium, 
    /// <summary>Recovers from up to 25% erroneous data.</summary> 
    QuiteGood, 
    /// <summary>Recovers from up to 30% erroneous data.</summary> 
    High 
} 
+1

+1 za zabawny przykładowy ciąg – usr

+1

+1 za dokładnie to, czego szukałem. – Gallen

+1

Pamiętaj, że zgodnie z tym developers.google.com/chart/infographics kod QR Google Chart jest przestarzały – Alexandre

28

Jedną z opcji jest użycie Google Chart Server API, aby to zrobić, like this.

Na przykład, oto kod QR tej samej stronie ...

Brak kodu wymagane :)

+1

Dzięki. Znalazłem ten API wkrótce po opublikowaniu i opakowałem go za pomocą metody pomocnika ASP.NET MVC, ponieważ zamierzam ją nazwać z kilku miejsc. Kod jest umieszczany w odpowiedzi, na wypadek gdyby pomógł komuś innemu. –

+1

aktualizacja adresu URL: http://code.google.com/apis/chart/infographics/docs/qr_codes.html – benpage

+0

@benpage: Dzięki, gotowe. –

7
+0

Dzięki. Pierwszy link wygląda interesująco. Nawiasem mówiąc, ten link jest nieaktualny (będę edytować odpowiedź.) –