Most popular

วันจันทร์ที่ 8 สิงหาคม พ.ศ. 2554

สร้าง QR Code ด้วย ASP.NET

ส่วนที่จำเป็นคือไฟล์ .dll ที่เป็นตัว Generate QR Code อยู่ในโปรแกรม Messaging Toolkit barcode
ดาวน์โหลดได้ที่  http://twit88.com/platform/attachments/download/158/setup-qrcode.exe 
เมื่อดาวน์โหลดมา ติดตั้งให้เรียบร้อยแล้วเข้าไปตรวจสอบที่ Path 
C:\Program Files\MessagingToolkit\MessagingToolkit-QRCode\QRCode\
หาไฟล์ MessagingToolkit.QRCode.dll และให้คัดลอกไปไว้ที่ Directory bin ของ Project
จากนั้น Add Referencไฟล์ MessagingToolkit.QRCode.dll ที่อยู่ใน  Directory bin  เข้ามาใน Project

Add Reference
ตอนนี้เรามีไฟล์ .dll ที่จะทำการ Generate QR Code ได้แล้ว ขั้นตอนต่อไปคือการใช้ Generic Handler สร้างรูปภาพ QR Code ขึ้นมา

เพิ่มไฟล์ Generic Handler .ashx เข้ามาใน Project ในที่นี้ผู้เขียนตั้งชื่อเป็น QRcode.ashx
ในไฟล์ QRcode.ashx มีคำสั่งการทำงานดังนี้


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace aspNetQRCODE
{
    public class QRcode : IHttpHandler
    {


        public void ProcessRequest(HttpContext context)
        {
            string myText= context.Request.QueryString.Get("code");
            context.Response.ContentType = "image/gif";
            if (myText.Length > 0)
            {
                MessagingToolkit.QRCode.Codec.QRCodeEncoder qe = new MessagingToolkit.QRCode.Codec.QRCodeEncoder();
                System.Drawing.Bitmap bm = qe.Encode(myText);
                bm.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
            }   
        }


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

ในหน้าเพจที่ต้องการให้แสดงเป็นรูปภาพ ให้ใส่ Tag img เรียกไปที่ไฟล์ QRcode.ashx พร้อมส่งข้อความที่ต้องการจะสร้าง QR Code

<img src="QRcode.ashx?code=i love you" alt="the code" />

ลองทดสอบเปิดหน้าเพจขึ้นมา

Tag img จะทำการเรียกใช้งานผ่าน QRcode.ashx ที่เป็น Generic handler คืนค่ากลับมาเป็นภาพ .gif
ทดสอบอ่าน QR Code ด้วยการบันทึกไฟล์รูปภาพ QR Code จากหน้าเพจ
และเปิดโปรแกรม Messaging Toolkit ที่เราได้ติดตั้งไปในตอนแรก
(โดยปกติจะอยู่ที่ C:\Program Files\MessagingToolkit\MessagingToolkit-QRCode\MessagingToolkit.Barcode.Demo.exe)

เมื่อเปิดไฟล์ภาพและกด Decode จะได้ผลเป็นข้อความตามที่เราได้สร้างขึ้น





ที่มา : http://www.jphellemons.nl/post/Generate-QR-Codes-with-AspNet-C.aspx

ดาวน์โหลด
เครื่องมือสำหรับใช้งาน QR Code http://www.4shared.com/file/wNgx_Pnv/setup-qrcode.html
ตัวอย่างในเนื้อหา http://www.4shared.com/file/vkhFdPpg/aspNetQRCODE.html

ไม่มีความคิดเห็น:

แสดงความคิดเห็น