Esiste un'API .NET che genera QR Codes come questo?Generazione codice QR in ASP.NET MVC
mi piacerebbe visualizzare questi su pagine che mi aspetto i miei utenti di stampare fuori.
Esiste un'API .NET che genera QR Codes come questo?Generazione codice QR in ASP.NET MVC
mi piacerebbe visualizzare questi su pagine che mi aspetto i miei utenti di stampare fuori.
Ho scritto un metodo di supporto HTML di base per emettere il corretto <img>
tag per sfruttare le API di Google. Quindi, nella pagina (supponendo vista del motore ASPX) usare qualcosa di simile:
<%: Html.QRCodeImage(Request.Url.AbsolutePath) %>
<%: Html.QRCodeImage("Meagre human needs a phone to read QR codes. Ha ha ha.") %>
Oppure, se si desidera specificare la dimensione in pixel (l'immagine è sempre quadrata):
<%: Html.QRCodeImage(Request.Url.AbsolutePath, size: 92) %>
Ecco il codice:
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
}
Un'opzione è utilizzare lo Google Chart Server API per farlo, like this.
Per esempio, ecco il codice QR per questa pagina molto ...
Nessun codice richiesto :)
Grazie. Ho trovato questa API poco dopo la pubblicazione e l'ho completata con un metodo helper ASP.NET MVC, visto che lo chiamerò da un sacco di punti. Il codice è pubblicato in una risposta, nel caso in cui aiuti qualcun altro. Aggiornamento rapido dell'URL –
: http://code.google.com/apis/chart/infographics/docs/qr_codes.html – benpage
@benpage: Grazie, fatto. –
una rapida ricerca produce molte biblioteche QRCode (tutte tranne il commerciale prima):
Grazie. Il primo link sembra interessante. A proposito, quel link non è aggiornato (modificherò la tua risposta) –
Si potrebbe anche prendere in considerazione la "QRCode libreria open source sul progetto di codice"
È disponibile anche un pacchetto Nuget - QRCodeHelper basato sul progetto Codeplex QRCode Helper.
Ecco un altro semplice servizio REST web:
Esempio
Il codice QR legge: "L'uomo magro ha bisogno di un telefono per leggere i codici QR. Ah ah ah. "Bello. :) –
Non so perché questo è stato impostato come fuori tema. Trovo che sia esattamente sul tema ...:/ –