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

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

[VB6/VB.NET] การสร้างรหัสบาร์โค้ด EAN13 และคำนวณหาตัวเลขตรวจสอบความถูกต้องของบาร์โค้ด

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

310

กระทู้

501

โพสต์

6035

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6035



รหัสแท่งบาร์โค้ดแบบ 1 มิติ EAN-13 (European Article Numbering international retail product code) เป็นแบบบาร์โค้ดที่ได้รับการยอมรับมากที่สุดในโลก โดยบาร์โค้ดประเภทนี้จะมีลักษณะเฉพาะของชุดตัวเลขจำนวน 13 หลัก ซึ่งมีความหมายดังนี้ ...
3 หลักแรก คือ รหัสของประเทศที่กำหนดขึ้นมาเพื่อให้ผู้ผลิตได้ทำการลงทะเบียนได้ทำการผลิตจากประเทศไหน
4 หลักถัดมา คือ รหัสโรงงานที่ผลิต
5 หลักถัดมา คือ รหัสของสินค้า
และ ตัวเลขในหลักสุดท้าย (หลักที่ 13) จะเป็นตัวเลขตรวจสอบความถูกต้องของบาร์โค้ด (Check Digit) ...

วิธีการคำนวณหาหลักที่ 13 ...
(1) นำตัวเลขในตำแหน่งคู่ (หลักที่ 2, 4, 6, 8, 10, 12) มารวมกัน แล้วคูณด้วย 3
(2) นำตัวเลขในตำแหน่งคี่ (หลักที่ 1, 3, 5, 7, 9, 11) มารวมกัน
(3) นำผลลัพท์จากข้อ (1) และ (2) มารวมกัน
(4) นำผลลัพท์ที่ได้จากข้อ (3) ทำการหารเอาเศษ (Mod) ด้วย 10 จะได้เป็นตัวเลข (Check Digit)

Tips: การนำเลขจำนวนเต็มๆใดมาหารเอาเศษ จะทำให้ได้ค่าจาก 0 ไปจนถึง ค่า Mod - 1  

โค้ดตัวอย่างนี้ จะทำการสุ่มตัวเลขจาก 0 - 9 จำนวน 12 หลัก แล้วให้คำนวณหาหลักที่ 13 เพื่อทำการตรวจสอบความถูกต้อง

มาดูโค้ดของ VB6 กันเถอะ ...
  1. ' / -----------------------------------------------------------------------------------------------
  2. ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
  3. ' / eMail : thongkorn@hotmail.com
  4. ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
  5. ' / Facebook: https://www.facebook.com/g2gnet (For Thailand only)
  6. ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
  7. ' / MORE: http://www.g2gnet.com/webboard
  8. ' /
  9. ' / Purpose: Generate Barcode EAN13.
  10. ' / Microsoft Visual Basic 6.0 Service Pack 6
  11. ' /
  12. ' / This is open source code under @Copyleft by Thongkorn Tubtimkrob.
  13. ' / You can modify and/or distribute without to inform the developer.
  14. ' / -----------------------------------------------------------------------------------------------

  15. Option Explicit

  16. Private Sub cmdGenEAN13_Click()
  17.     Me.lvwEAN13.ListItems.Clear
  18.     '// สร้างรหัสบาร์โค้ด EAN13
  19.     Call GenerateEAN13
  20.     '//
  21. End Sub

  22. Private Sub Form_Load()
  23.     Call SetupListView
  24. End Sub

  25. Sub GenerateEAN13()
  26.     Randomize
  27.     '// สูตรการสุ่มตัวเลขจำนวนเต็ม
  28.     ' Int ((upperbound - lowerbound + 1) * Rnd + lowerbound)
  29.     Dim Digit As Byte, Count As Byte
  30.     Dim TwelveDigit As String
  31.     '// ตัวอย่าง 10 รายการ
  32.     For Count = 0 To 9
  33.         '// สุ่มตัวเลขจำนวน 12 หลักแรก
  34.         For Digit = 0 To 11
  35.             TwelveDigit = TwelveDigit & CStr(Int((9) * Rnd))
  36.         Next
  37.         Dim LV As ListItem
  38.         ' Index = 0
  39.         Set LV = Me.lvwEAN13.ListItems.Add(, , TwelveDigit)
  40.         '// นำค่า 12 หลักแรก ไปคำนวณหาหลักทดสอบความถูกต้อง (หลักที่ 13)
  41.         LV.SubItems(1) = CheckDigitEAN13(TwelveDigit)
  42.         TwelveDigit = ""
  43.     Next
  44. End Sub

  45. ' / -----------------------------------------------------------------------------------------------
  46. ' / >> หลักการคิดคำนวณหา  <<
  47. ' / 1) นำตัวเลขในตำแหน่งคู่ (หลักที่ 2, 4, 6, 8, 10,12) มารวมกัน แล้วคูณด้วย 3
  48. ' / 2) นำตัวเลขในตำแหน่งคี่ (หลักที่ 1, 3, 5, 7, 9, 11) มารวมกัน
  49. ' / 3) นำผลลัพท์จากข้อ 1 และ 2 มารวมกัน
  50. ' / 4) นำผลลัพท์ที่ได้จากข้อ 3 ทำการหารเอาเศษ (Mod) ด้วย 10 จะได้เป็นตัวเลข (Check digit )
  51. ' / >> Tips: การนำเลขจำนวนเต็มๆใดมาหารเอาเศษ จะทำให้ได้ค่าจาก 0 ไปจนถึง ค่า Mod - 1
  52. ' / -----------------------------------------------------------------------------------------------
  53. Private Function CheckDigitEAN13(TwelveDigit As String) As Byte
  54. ' / -----------------------------------------------------------------------------------------------
  55.     ' จำนวน 12 หลัก
  56.     Dim Digit As Byte
  57.     ' ผลรวมหลักคี่
  58.     Dim SumOdd As Integer
  59.     ' ผลรวมหลักคู่
  60.     Dim SumEven As Integer
  61.     ' (ผลรวมหลักคู่ * 3) + ผลรวมหลักคี่
  62.     Dim BarValue As Integer
  63.    
  64.     For Digit = 1 To Len(TwelveDigit)
  65.         ' หารเอาเศษด้วย 2 หากได้คำตอบ 0 แสดงว่าเป็นหลักคู่
  66.         If Digit Mod 2 = 0 Then
  67.             SumEven = SumEven + Val(Mid$(TwelveDigit, Digit, 1))
  68.         Else
  69.             SumOdd = SumOdd + Val(Mid$(TwelveDigit, Digit, 1))
  70.         End If
  71.     Next
  72.    
  73.     BarValue = (SumEven * 3) + SumOdd
  74.    
  75.     If BarValue Mod 10 = 0 Then
  76.         CheckDigitEAN13 = 0
  77.     Else
  78.         CheckDigitEAN13 = 10 - BarValue Mod 10
  79.     End If
  80. End Function

  81. ' Initial ListView
  82. Sub SetupListView()
  83.     With Me.lvwEAN13
  84.         ' Coding with Run Time
  85.         .View = lvwReport
  86.         .Arrange = lvwNone
  87.         .LabelEdit = lvwManual
  88.         .BorderStyle = ccFixedSingle
  89.         .Appearance = cc3D
  90.         
  91.         .HideColumnHeaders = False
  92.         .HideSelection = False
  93.         .LabelWrap = False
  94.         .MultiSelect = False
  95.         .Enabled = True
  96.         .AllowColumnReorder = True
  97.         .Checkboxes = False
  98.         .FlatScrollBar = False
  99.         .FullRowSelect = True
  100.         .GridLines = True
  101.         .HotTracking = False
  102.         .HoverSelection = False
  103.         
  104.         .Sorted = False 'True
  105.         .SortKey = 0
  106.         .SortOrder = lvwAscending 'lvwDescending
  107.         
  108.         ' Add header
  109.         .ColumnHeaders.Add , , "12 หลักแรก", .Width \ 2
  110.         .ColumnHeaders.Add , , "หลักทดสอบ", .Width \ 2 - 480, lvwColumnLeft
  111.     End With
  112.    
  113. End Sub
คัดลอกไปที่คลิปบอร์ด

มาดูโค้ดของ VB.NET กันเถอะ ...
  1. ' / -------------------------------------------------------------------
  2. ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
  3. ' / eMail : thongkorn@hotmail.com
  4. ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
  5. ' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
  6. ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
  7. ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
  8. ' / MORE: http://www.g2gnet.com/webboard
  9. ' /
  10. ' / Purpose: Generate Barcode EAN13.
  11. ' / Microsoft Visual Basic .NET (2010)
  12. ' /
  13. ' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
  14. ' / You can modify and/or distribute without to inform the developer.
  15. ' / -------------------------------------------------------------------

  16. Public Class frmGenerateEAN13

  17.     Private Sub frmGenerateEAN13_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  18.         Me.Dispose()
  19.         End
  20.     End Sub

  21.     Private Sub frmGenerateEAN13_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  22.         Call InitListView()
  23.     End Sub

  24.     Private Sub btnGenEAN13_Click(sender As System.Object, e As System.EventArgs) Handles btnGenEAN13.Click
  25.         lvwEAN13.Items.Clear()
  26.         '//
  27.         Call GenerateEAN13()
  28.     End Sub

  29.     Sub GenerateEAN13()
  30.         Dim LV As ListViewItem
  31.         Randomize()
  32.         '// สูตรการสุ่มตัวเลขจำนวนเต็ม
  33.         ' Int ((upperbound - lowerbound + 1) * Rnd + lowerbound)
  34.         Dim Digit As Byte, Count As Byte
  35.         Dim TwelveDigit As String = String.Empty
  36.         '// ตัวอย่าง 10 รายการ
  37.         For Count = 0 To 9
  38.             '// สุ่มตัวเลขจำนวน 12 หลักแรก
  39.             For Digit = 0 To 11
  40.                 TwelveDigit = TwelveDigit & CStr(Int((9) * Rnd()))
  41.             Next
  42.             ' Index = 0
  43.             LV = lvwEAN13.Items.Add(String.Format("{0}", TwelveDigit))  ' Primary Node
  44.             '// นำค่า 12 หลักแรก ไปคำนวณหาหลักทดสอบความถูกต้อง (หลักที่ 13)
  45.             LV.SubItems.Add(CheckDigitEAN13(TwelveDigit))
  46.             TwelveDigit = ""
  47.         Next
  48.     End Sub

  49.     ' / -----------------------------------------------------------------------------------------------
  50.     ' / >> หลักการคิดคำนวณหา  <<
  51.     ' / 1) นำตัวเลขในตำแหน่งคู่ (หลักที่ 2, 4, 6, 8, 10,12) มารวมกัน แล้วคูณด้วย 3
  52.     ' / 2) นำตัวเลขในตำแหน่งคี่ (หลักที่ 1, 3, 5, 7, 9, 11) มารวมกัน
  53.     ' / 3) นำผลลัพท์จากข้อ 1 และ 2 มารวมกัน
  54.     ' / 4) นำผลลัพท์ที่ได้จากข้อ 3 ทำการหารเอาเศษ (Mod) ด้วย 10 จะได้เป็นตัวเลข (Check digit )
  55.     ' / >> Tips: การนำเลขจำนวนเต็มๆใดมาหารเอาเศษ จะทำให้ได้ค่าจาก 0 ไปจนถึง ค่า Mod - 1
  56.     ' / -----------------------------------------------------------------------------------------------
  57.     Private Function CheckDigitEAN13(TwelveDigit As String) As Byte
  58.         ' / -----------------------------------------------------------------------------------------------
  59.         ' จำนวน 12 หลัก
  60.         Dim Digit As Byte
  61.         ' ผลรวมหลักคี่
  62.         Dim SumOdd As Integer
  63.         ' ผลรวมหลักคู่
  64.         Dim SumEven As Integer
  65.         ' (ผลรวมหลักคู่ * 3) + ผลรวมหลักคี่
  66.         Dim BarValue As Integer

  67.         For Digit = 1 To Len(TwelveDigit)
  68.             ' หารเอาเศษด้วย 2 หากได้คำตอบ 0 แสดงว่าเป็นหลักคู่
  69.             If Digit Mod 2 = 0 Then
  70.                 SumEven = SumEven + Val(Mid$(TwelveDigit, Digit, 1))
  71.             Else
  72.                 SumOdd = SumOdd + Val(Mid$(TwelveDigit, Digit, 1))
  73.             End If
  74.         Next

  75.         BarValue = (SumEven * 3) + SumOdd

  76.         If BarValue Mod 10 = 0 Then
  77.             CheckDigitEAN13 = 0
  78.         Else
  79.             CheckDigitEAN13 = 10 - BarValue Mod 10
  80.         End If
  81.     End Function

  82.     ' / Initailize ListView Control
  83.     Sub InitListView()
  84.         With lvwEAN13
  85.             .Clear()
  86.             .View = View.Details
  87.             .GridLines = True
  88.             .FullRowSelect = True
  89.             .HideSelection = False
  90.             .MultiSelect = False
  91.             ' 1st Column Index = 0
  92.             .Columns.Add("12 หลักแรก", lvwEAN13.Width \ 2)
  93.             .Columns.Add("หลักทดสอบ", lvwEAN13.Width \ 2 - 20)
  94.         End With
  95.     End Sub

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

ดาวน์โหลดโค้ดต้นฉบับทั้ง VB6 และ VB.NET (2010) ได้ที่นี่ ...

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

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

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

1

กระทู้

11

โพสต์

81

เครดิต

Member

Rank: 2

เครดิต
81
โพสต์ 2021-3-11 17:35:33 | ดูโพสต์ทั้งหมด

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

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

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

GMT+7, 2024-4-20 16:04 , Processed in 0.113679 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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