利用ZXing.Net元件產生圖片


Google官方有提供一個API可以依據傳入的參數, 產生對應的QRCODE圖檔, 讓網站可以快速的做出屬於自己的QRCODE圖片。不過這個技術要是需要應用在公司系統裡, 就可能面臨許多未知數了。

  • 萬一Google哪天這個API不玩了或是網址修改了, 系統是否有對應的方針呢?
  • 萬一公司網路的ISP業者維修線路, 暫停網路服務, 公司是否可以忍受這期間系統無法使用的情況呢?
  • 萬一公司對外網路設備掛點了, 是否可以忍受這期間系統無法使用的情況呢?
  • 萬一...應該還有其他萬一...

所以基於我對自己程式的完美要求, 就想辦法去找找有沒有免費的QRCODE元件可以做到這個服務。

皇天不負苦心人呀, 花了2個小時就試出這個功能了, 只能說哥哥(Google)有你真好。

開發平台:Visual Studio 2010
元件:ZXing.Net (https://zxingnet.codeplex.com/)

step-01 將下載的元件(dll)放到網站bin目錄裡


step-02 加入參考


step-03 程式碼如下,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZXing;
using ZXing.Common;
using System.Drawing;
using System.Drawing.Imaging;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

        try
        {
            //QRCODE內容
            string strQRstring = Request.QueryString["d"];
            //QRCODE圖片高度
            string strHeight = Request.QueryString["h"];
            //QRCODE圖片寬度
            string strWidth = Request.QueryString["w"];

            BarcodeWriter bw = new BarcodeWriter();
            bw.Format = BarcodeFormat.QR_CODE;
            bw.Options.Width = int.Parse(strHeight);
            bw.Options.Height = int.Parse(strWidth);
            Bitmap bitmap = bw.Write(strQRstring);

            //利用steam方式儲存圖片
            bitmap.Save(Context.Response.OutputStream, ImageFormat.Jpeg);
        }
        catch { }

        //將網頁型態指定為image/jpeg
        Context.Response.ContentType = "image/jpeg";
        Context.Response.End();


    }
}


step-04 執行後帶入參數就以取得QRCODE圖片了


20150201

  • 目前這個寫法若遇到QRCODE資料包含中文字則會顯示問號






reference web:
http://www.dami.tw/2012/11/26dami.html

留言

這個網誌中的熱門文章

ORA-12514: TNS: 監聽器目前不知道連線描述區中要求的服務

Oracle 例外控制(Exception Control)

Oracle 工作排程 DBMS_JOB 筆記