ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 6408|ตอบกลับ: 2

[VB.NET] การสร้างบาร์โค้ด 2 มิติ QR Code ด้วยของฟรีจาก ZXing.Net

[คัดลอกลิงก์]

309

กระทู้

500

โพสต์

6030

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6030




QR Code (คิวอาร์ โค้ด) คือ ย่อมาจาก Quick Response เป็นบาร์โค้ด 2 มิติ แสดงสัญลักษณ์แทนข้อมูลต่างๆ ซึ่งส่วนใหญ่จะนำมาประยุกต์ใช้ได้หลากหลายรูปแบบ เช่น แสดง URL ของเว็บไซต์, ข้อความ, เบอร์โทรศัพท์ และข้อมูลที่เป็นตัวอักษรได้อีกมากมาย วันนี้แอดมินขอนำเสนอการสร้าง QR Code จาก ZXing ซึ่งสามารถรองรับ UTF-8 หรือเป็นภาษาไทยได้ 100% นั่นเอง ...


ดาวน์โหลด ZXing.NET Release ได้ที่นี่ ซึ่งมี Net Framework หลายเวอร์ชั่น รวมไปถึงคู่มือการใช้งาน และตัวอย่างในขั้นสูง ...


Add Reference ZXing.NET ... อย่าลืมเรียกไฟล์ ZXing.DLL เข้ามาก่อนด้วยครับ



การคัดลอกนำไปใส่ไว้ใน MS Word



มาดูโค้ดฉบับเต็มกันเถอะ ...
  1. '// ZXing.Net release download.
  2. '// https://github.com/micjahn/ZXing.Net/releases

  3. Imports System.Drawing.Imaging
  4. Imports ZXing
  5. Imports ZXing.Common
  6. Imports ZXing.QrCode
  7. Imports System.IO

  8. Public Class frmQRCodeZXing

  9.     Dim strPathImage As String = MyPath(Application.StartupPath)

  10.     Private Sub frmQRCodeZXing_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  11.         txtData.Text = "www.g2gnet.com/webboard"
  12.     End Sub

  13.     '// Event when data is changed.
  14.     Private Sub txtData_TextChanged(sender As Object, e As EventArgs) Handles txtData.TextChanged
  15.         If String.IsNullOrWhiteSpace(txtData.Text) Then
  16.             picBarcode.Image = Nothing
  17.             Return
  18.         End If
  19.         '//
  20.         Dim options As EncodingOptions = New QrCodeEncodingOptions
  21.         With options
  22.             .Width = picBarcode.Width
  23.             .Height = picBarcode.Height
  24.             .Hints.Add(ZXing.EncodeHintType.CHARACTER_SET, "UTF-8")
  25.         End With

  26.         Dim objWriter As BarcodeWriter = New BarcodeWriter With {
  27.                 .Format = BarcodeFormat.QR_CODE,
  28.                 .Options = options
  29.                 }

  30.         picBarcode.Image = New Bitmap(objWriter.Write(txtData.Text))
  31.     End Sub

  32.     '// Save image of QR Code.
  33.     Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
  34.         Dim dlgSaveFile As New SaveFileDialog
  35.         With dlgSaveFile
  36.             .Title = "Select images"
  37.             .Filter = "Image files (*.jpg,*.png,*bmp) | *.jpg; *.png; *.bmp"
  38.             .FilterIndex = 1
  39.             .RestoreDirectory = True
  40.             .InitialDirectory = strPathImage
  41.         End With
  42.         '//
  43.         If dlgSaveFile.ShowDialog() = DialogResult.OK Then
  44.             '// Saves the Image via a FileStream created by the OpenFile method.
  45.             Dim fs = CType(dlgSaveFile.OpenFile, FileStream)
  46.             '// Saves the Image in the appropriate ImageFormat based upon the
  47.             '// file type selected in the dialog box.
  48.             Select Case dlgSaveFile.FilterIndex
  49.                 Case 1
  50.                     picBarcode.Image.Save(fs, ImageFormat.Jpeg)
  51.                 Case 3
  52.                     picBarcode.Image.Save(fs, ImageFormat.Png)
  53.                 Case 4
  54.                     picBarcode.Image.Save(fs, ImageFormat.Bmp)
  55.             End Select
  56.             fs.Close()
  57.             MessageBox.Show("QR Code image has been saved.")
  58.         End If
  59.     End Sub

  60.     '// Load QR Code and decode it.
  61.     Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
  62.         Dim dlgImage As OpenFileDialog = New OpenFileDialog()
  63.         ' / Open File Dialog
  64.         With dlgImage
  65.             '.InitialDirectory = strPath
  66.             .Title = "Select images"
  67.             .Filter = "Image files (*.jpg,*.png,*bmp) | *.jpg; *.png; *.bmp"
  68.             .FilterIndex = 1
  69.             .RestoreDirectory = True
  70.         End With
  71.         ' Select OK after Browse ...
  72.         If dlgImage.ShowDialog() = DialogResult.OK Then
  73.             Using FS As IO.FileStream = File.Open(dlgImage.FileName, FileMode.Open)
  74.                 Dim bitmap As Bitmap = New Bitmap(FS)
  75.                 Dim CurrentPicture As Image = CType(bitmap, Image)
  76.                 picBarcode.Image = CurrentPicture
  77.                 '// Decode
  78.                 Dim objReader As BarcodeReader = New BarcodeReader()
  79.                 Dim objResult As Result = objReader.Decode(bitmap)
  80.                 If objResult IsNot Nothing Then
  81.                     txtData.Text = objResult.Text
  82.                 Else
  83.                     MessageBox.Show("Cannot decode this image!")
  84.                 End If
  85.             End Using
  86.         End If
  87.     End Sub

  88.     '// Copy to clipboard.
  89.     Private Sub btnCopy_Click(sender As System.Object, e As System.EventArgs) Handles btnCopy.Click
  90.         '/ Add it as an image
  91.         Clipboard.SetImage(picBarcode.Image)
  92.         '/ Create a JPG on disk and add the location to the clipboard
  93.         Dim TempName As String = "TempName.jpg"
  94.         Dim TempPath As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, TempName)
  95.         Using FS As New System.IO.FileStream(TempPath, IO.FileMode.Create, IO.FileAccess.Write, IO.FileShare.Read)
  96.             picBarcode.Image.Save(FS, System.Drawing.Imaging.ImageFormat.Jpeg)
  97.         End Using
  98.         Dim Paths As New System.Collections.Specialized.StringCollection()
  99.         Paths.Add(TempPath)
  100.         Clipboard.SetFileDropList(Paths)
  101.     End Sub

  102.     ' / --------------------------------------------------------------------------------
  103.     ' / Get my project path
  104.     ' / AppPath = C:\My Project\bin\debug
  105.     ' / Replace "\bin\debug" with ""
  106.     ' / Return : C:\My Project\
  107.     Function MyPath(AppPath As String) As String
  108.         '/ MessageBox.Show(AppPath);
  109.         AppPath = AppPath.ToLower()
  110.         '/ Return Value
  111.         MyPath = AppPath.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "")
  112.         '// If not found folder then put the \ (BackSlash ASCII Code = 92) at the end.
  113.         If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
  114.     End Function

  115. End Class
คัดลอกไปที่คลิปบอร์ด



ดาวน์โหลดโค้ดต้นฉบับชุดเต็ม VB.NET (2010) ได้ที่นี่ ...

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

0

กระทู้

51

โพสต์

233

เครดิต

Full Member

Rank: 3Rank: 3

เครดิต
233
โพสต์ 2020-3-20 10:11:30 | ดูโพสต์ทั้งหมด

ขอบพระคุณอย่างสูงครับอาจารย์ ขอให้สุขภาพแข็งแรง เงินทองไหลมาเทมา เด้อ.. คับ.

0

กระทู้

1

โพสต์

8

เครดิต

Newbie

Rank: 1

เครดิต
8
โพสต์ 2020-11-23 14:00:48 | ดูโพสต์ทั้งหมด

ขอบคุณมากเลยค่ะ
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-19 18:36 , Processed in 0.153140 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้