2010-09-25 24 views
28

Có API .NET tạo QR Codes chẳng hạn như cái này không?Tạo mã QR trong ASP.NET MVC

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

Tôi muốn hiển thị những trên các trang mà tôi mong đợi người dùng của tôi để in ra.

+4

Đọc mã QR: "Con người ít ỏi cần điện thoại để đọc mã QR. Ha ha ha. "Đáng yêu. :) –

+2

Tôi không biết tại sao điều này được đặt là chủ đề ngoài. Tôi thấy nó chính xác về chủ đề ...:/ –

Trả lời

53

Tôi đã viết một phương pháp HTML helper cơ bản để phát ra đúng <img> thẻ để tận dụng API của Google. Vì vậy, trên trang của bạn (giả sử công cụ xem ASPX) sử dụng một cái gì đó như thế này:

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

Hoặc nếu bạn muốn chỉ định kích thước bằng pixel (ảnh phải lúc nào cũng vuông):

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

Đây là mã:

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 cho chuỗi mẫu hài hước – usr

+1

+1 cho chính xác những gì tôi đang tìm kiếm. – Gallen

+1

Lưu ý rằng theo developers.google.com/chart/infographics Mã QR của Biểu đồ Google này không được dùng nữa – Alexandre

28

Một tùy chọn là sử dụng Google Chart Server API để làm điều đó, like this.

Ví dụ, đây là mã QR cho trang này rất ...

Không yêu cầu mã :)

+1

Cảm ơn. Tôi tìm thấy API này ngay sau khi đăng bài và gói nó lên với một phương pháp trợ giúp ASP.NET MVC, như tôi sẽ gọi nó từ một loạt các nơi. Mã được đăng trong một câu trả lời, trong trường hợp nó giúp người khác. –

+1

cập nhật url nhanh: http://code.google.com/apis/chart/infographics/docs/qr_codes.html – benpage

+0

@benpage: Cảm ơn, đã hoàn tất. –

7
+0

Cảm ơn bạn. Liên kết đầu tiên trông thú vị. Nhân tiện, liên kết đó đã lỗi thời (tôi sẽ chỉnh sửa câu trả lời của bạn.) –