NEWS

Hướng dẫn tạo mã QR code cho sản phẩm

Hướng dẫn tạo mã QR code cho sản phẩm
Đăng bởi: Thảo Meo - Lượt xem: 19778 14:32:06, 17/10/2015VB.NET   In bài viết

Hôm nay, mình xin hướng dẫn các bạn tạo ra mã QR code cho sản phẩm (dùng để scan code hoặc đăng nhập như chương trình zalo của zing trên windows...)

- Bài viết mình sử dụng dịch vụ web services của VB.NET kết nối với API của google, để lấy về một mã QR code.

Đầu tiên mình xin giới thiệu đường dẫn api của google: 

https://chart.googleapis.com/chart 

Với các tham số truyền vào bạn có thể mã hóa ký tự mình muốn nhập vào thành một mã qr code.

Dưới đây: là cấu pháp dùng để truyền dữ liệu cho google. Các bạn có thể tham khảo chi tiết trên url: https://chart.googleapis.com/chart 

google qr

+ cht = qr (tạo mã QR)

+ chs = x (định nghĩa kích thước cho mã QR)

+ chl = Mình sẽ truyền giá trị muốn mã hóa QR vào đây.

choe=<output_encoding> (mặc định là UTF-8)

chld=<error_correction_level>|<margin>

 

  • error_correction_level - QR codes support four levels of error correction to enable recovery of missing, misread, or obscured data. Greater redundancy is achieved at the cost of being able to store less data. See the appendix for details. Here are the supported values:
    • L - [Default] Allows recovery of up to 7% data loss
    • M - Allows recovery of up to 15% data loss
    • Q - Allows recovery of up to 25% data loss
    • H - Allows recovery of up to 30% data loss
  • margin - The width of the white border around the data portion of the code. This is in rows, not in pixels. (See below to learn what rows are in a QR code.) The default value is 4.

VD: Bây giờ ta muốn mã hóa chuỗi "LAPTRINHVB.NET" thành mã QR có kích thước 256x256 ta sẽ có được url và giá trị truyền vào như sau:

http://chart.googleapis.com/chart?cht=qr&chs=256x256&chl=LAPTRINHVB.NET&chld=H|0

Dưới đây là đoạn mã tạo QR bằng VB.NET :

Private Function Tao_QR_Code(Data As String, Size As Size)
        Dim web As New System.Net.WebClient()
        Dim url As String = "http://chart.googleapis.com/chart?chs="
        url &= Size.Width.ToString() & "x" & Size.Height.ToString()
        url &= "&cht=qr&chl=" & System.Uri.EscapeDataString(Data)
        MsgBox(url)
        Dim bytes As Byte() = web.DownloadData(url)
        Dim bmp As Bitmap = Nothing
        Using mem As New IO.MemoryStream(bytes)
            bmp = Bitmap.FromStream(mem)
        End Using
        Return bmp
    End Function

+ sự kiện click vào nút tạo mã :

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        PictureBox1.Image = Tao_QR_Code(TextBox1.Text, New Size(256, 256))
    End Sub

Vậy là đã hoàn tất. Chúc các bạn thành công.

Bạn nào có thắc mắc xin để lại commnet.

Tags: qrcode

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

Hướng dẫn tạo mã QR code cho sản phẩm
Đăng bởi: Thảo Meo - Lượt xem: 19778 14:32:06, 17/10/2015VB.NET   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp