C# 用 ZXing 產生 QR Code ,NuGet ZXing教學 QR Code 產生 API

原本使用Google Chart API,無法指定圖檔色彩,只好自已寫一個類似的API來使用。

 

一.新增一個空白的網站,如下:

1

 

二.新增NuGet套件→輸入ZXing查詢,安裝 ZXing.Net插件。

2

三.安裝後。自已會產生以下檔案。

3

四‧開始寫程式啦,程式碼如下:

新增CreateQRCode.ashx 

<%@ WebHandler Language="C#" Class="CreateQRCode" %>

using System;
using System.Web;

using System.Drawing;
using System.IO;
using System.Net;

using ZXing;
using ZXing.Common;
using ZXing.QrCode;
using ZXing.QrCode.Internal;

public class CreateQRCode : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {

        string strTxt = context.Request["txt"];
        string strW = context.Request["w"];
        string strH = context.Request["h"];
        string strSTxt = context.Request["stxt"];
        string strCLASS = context.Request["cls"];
        string strBG = context.Request["bg"];
        string strFG = context.Request["fg"];

        if (strTxt == null) strTxt = "";
        if (strSTxt == null) strSTxt = "";
        if (strW == null) strW = "150";
        if (strH == null) strH = "150";
        if (strBG == null) strBG = "FFFFFF";
        if (strFG == null) strFG = "000000";

        strBG = "#" + strBG;
        strFG = "#" + strFG;

        if (!strTxt.Trim().Equals(""))
        {
            var writer = new BarcodeWriter  
            {
                Format = BarcodeFormat.QR_CODE,

                Renderer = new ZXing.Rendering.BitmapRenderer { Background = System.Drawing.ColorTranslator.FromHtml(strBG), Foreground = System.Drawing.ColorTranslator.FromHtml(strFG) },

                Options = new QrCodeEncodingOptions
                {
                    Height = Int32.Parse(strH),
                    Width = Int32.Parse(strW),
                    DisableECI = true,
                    CharacterSet = "UTF-8",
                    ErrorCorrection = ErrorCorrectionLevel.H,
                    Margin = 1,
                }
            };

            Bitmap m_Bitmap = writer.Write(strTxt);
            MemoryStream ms = new MemoryStream();
            m_Bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);//JPG、GIF、PNG等均可  
            string uploadPath = HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\img\\\\CrtQRCode\\";

            byte[] buff = ms.ToArray();

            if (buff == null)
            {
                HttpContext.Current.Response.ContentType = "text/plain";
                HttpContext.Current.Response.Write("Error converting!");
            }

            try
            {
                string FileName = strTxt + ".jpeg";
                //宣告並建立WebClient物件
                WebClient wc = new WebClient();

                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ClearHeaders();
                HttpContext.Current.Response.Charset = "utf-8";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
                HttpContext.Current.Response.ContentType = "image/jpeg"; //二進位方式

                //設定標頭檔資訊
                HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;  filename=" + HttpUtility.UrlEncode(FileName));
                HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "Binary");
                HttpContext.Current.Response.AddHeader("Content-Length", buff.Length.ToString());
                HttpContext.Current.Response.BinaryWrite(buff);
                HttpContext.Current.Response.End();

            }
            catch (Exception e)
            {
                HttpContext.Current.Response.ContentType = "text/plain";
                HttpContext.Current.Response.Write(e.Message);
            }
        }

    }
    
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

 

五.驗證是否正常

5

使用方式: (圖檔)

<img src= ‘http://網址/CreateQRCode.ashx?bg=背景色&fg=文字顏色&w=寬&h=高&txt=QRCODE內容’  />

參數說明:

txt:內容(必填)
w:寬(預設:150 可不填)
h:高(預設:150 可不填)
fg:文字顏色(預設:000000 可不填)
bg:背景色(預設:FFFFFF 可不填)

 

六.附上SourceCode 

下載點: CreateQRCode

解壓縮密碼:  5dfu.com

 

 

 

 

2 則評論在 C# 用 ZXing 產生 QR Code ,NuGet ZXing教學 QR Code 產生 API.

  1. I have recently started a site, the info you provide on this website has helped me tremendously. Thanks for all of your time &amp work. The achievements of an organization are the results of the combined effort of each individual. by Vince Lombardi. eedcededbefabfad

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *