31

このようなQR コードを生成する .NET API はありますか?

貧弱な人間には、QR コードを読み取るための電話が必要です。 ハハハ。

ユーザーが印刷するページにこれらを表示したいと思います。

4

7 に答える 7

55

<img>Google の API を利用するための正しいタグを発行するための基本的な HTML ヘルパー メソッドを作成しました。したがって、ページ (ASPX ビュー エンジンを想定) では、次のようなものを使用します。

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

または、サイズをピクセル単位で指定する場合 (画像は常に正方形です):

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

コードは次のとおりです。

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
}
于 2010-09-25T12:54:46.253 に答える
28

1つのオプションは、Google ChartServerAPIを使用してそれを行うことです。

たとえば、このページのQRコードは次のとおりです...

コードは必要ありません:)

リンク先のドキュメントに詳細がありますが、https://chart.googleapis.com/chart?のURLから始めます。、次に次のクエリパラメータを追加します。

  • cht=qr:QRコードが必要であることを指定します
  • chs=サイズ:サイズを指定します。例:200x200
  • chl=data:URLなどのデータを指定します

出力エンコーディングとエラー訂正のための他のクエリパラメータがあります。

于 2010-09-25T12:35:04.417 に答える
5

「コード プロジェクトのオープン ソース QRCode ライブラリ」も検討してください。

http://www.codeproject.com/KB/cs/qrcode.aspx

于 2011-02-11T17:45:58.683 に答える
4

Codeplex QRCode Helperプロジェクトに基づくQRCodeHelperというNuget パッケージも利用できます。

于 2011-07-10T14:51:23.863 に答える
3

http://qrcodenet.codeplex.com を試してください

于 2011-09-29T21:42:36.553 に答える
0

別の単純な REST Web サービスを次に示します。

http://www.esponce.com/api/v3/generate?content=Meagre+human+needs+a+phone+to+read+QR+codes.+Ha+ha+ha.&size=200x200

于 2012-09-04T08:19:06.253 に答える