| 
 | 
 
  
 
รหัสแท่งบาร์โค้ดแบบ 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 กันเถอะ ...  
- ' / -----------------------------------------------------------------------------------------------
 
 - ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
 
 - ' / eMail : thongkorn@hotmail.com
 
 - ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
 
 - ' / Facebook: https://www.facebook.com/g2gnet (For Thailand only)
 
 - ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
 
 - ' / MORE: http://www.g2gnet.com/webboard
 
 - ' /
 
 - ' / Purpose: Generate Barcode EAN13.
 
 - ' / Microsoft Visual Basic 6.0 Service Pack 6
 
 - ' /
 
 - ' / This is open source code under @Copyleft by Thongkorn Tubtimkrob.
 
 - ' / You can modify and/or distribute without to inform the developer.
 
 - ' / -----------------------------------------------------------------------------------------------
 
  
- Option Explicit
 
  
- Private Sub cmdGenEAN13_Click()
 
 -     Me.lvwEAN13.ListItems.Clear
 
 -     '// สร้างรหัสบาร์โค้ด EAN13
 
 -     Call GenerateEAN13
 
 -     '//
 
 - End Sub
 
  
- Private Sub Form_Load()
 
 -     Call SetupListView
 
 - End Sub
 
  
- Sub GenerateEAN13()
 
 -     Randomize
 
 -     '// สูตรการสุ่มตัวเลขจำนวนเต็ม
 
 -     ' Int ((upperbound - lowerbound + 1) * Rnd + lowerbound)
 
 -     Dim Digit As Byte, Count As Byte
 
 -     Dim TwelveDigit As String
 
 -     '// ตัวอย่าง 10 รายการ
 
 -     For Count = 0 To 9
 
 -         '// สุ่มตัวเลขจำนวน 12 หลักแรก
 
 -         For Digit = 0 To 11
 
 -             TwelveDigit = TwelveDigit & CStr(Int((9) * Rnd))
 
 -         Next
 
 -         Dim LV As ListItem
 
 -         ' Index = 0
 
 -         Set LV = Me.lvwEAN13.ListItems.Add(, , TwelveDigit)
 
 -         '// นำค่า 12 หลักแรก ไปคำนวณหาหลักทดสอบความถูกต้อง (หลักที่ 13)
 
 -         LV.SubItems(1) = CheckDigitEAN13(TwelveDigit)
 
 -         TwelveDigit = ""
 
 -     Next
 
 - End Sub
 
  
- ' / -----------------------------------------------------------------------------------------------
 
 - ' / >> หลักการคิดคำนวณหา  <<
 
 - ' / 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
 
 - ' / -----------------------------------------------------------------------------------------------
 
 - Private Function CheckDigitEAN13(TwelveDigit As String) As Byte
 
 - ' / -----------------------------------------------------------------------------------------------
 
 -     ' จำนวน 12 หลัก
 
 -     Dim Digit As Byte
 
 -     ' ผลรวมหลักคี่
 
 -     Dim SumOdd As Integer
 
 -     ' ผลรวมหลักคู่
 
 -     Dim SumEven As Integer
 
 -     ' (ผลรวมหลักคู่ * 3) + ผลรวมหลักคี่
 
 -     Dim BarValue As Integer
 
 -     
 
 -     For Digit = 1 To Len(TwelveDigit)
 
 -         ' หารเอาเศษด้วย 2 หากได้คำตอบ 0 แสดงว่าเป็นหลักคู่
 
 -         If Digit Mod 2 = 0 Then
 
 -             SumEven = SumEven + Val(Mid$(TwelveDigit, Digit, 1))
 
 -         Else
 
 -             SumOdd = SumOdd + Val(Mid$(TwelveDigit, Digit, 1))
 
 -         End If
 
 -     Next
 
 -     
 
 -     BarValue = (SumEven * 3) + SumOdd
 
 -     
 
 -     If BarValue Mod 10 = 0 Then
 
 -         CheckDigitEAN13 = 0
 
 -     Else
 
 -         CheckDigitEAN13 = 10 - BarValue Mod 10
 
 -     End If
 
 - End Function
 
  
- ' Initial ListView
 
 - Sub SetupListView()
 
 -     With Me.lvwEAN13
 
 -         ' Coding with Run Time
 
 -         .View = lvwReport
 
 -         .Arrange = lvwNone
 
 -         .LabelEdit = lvwManual
 
 -         .BorderStyle = ccFixedSingle
 
 -         .Appearance = cc3D
 
 -         
 
 -         .HideColumnHeaders = False
 
 -         .HideSelection = False
 
 -         .LabelWrap = False
 
 -         .MultiSelect = False
 
 -         .Enabled = True
 
 -         .AllowColumnReorder = True
 
 -         .Checkboxes = False
 
 -         .FlatScrollBar = False
 
 -         .FullRowSelect = True
 
 -         .GridLines = True
 
 -         .HotTracking = False
 
 -         .HoverSelection = False
 
 -         
 
 -         .Sorted = False 'True
 
 -         .SortKey = 0
 
 -         .SortOrder = lvwAscending 'lvwDescending
 
 -         
 
 -         ' Add header
 
 -         .ColumnHeaders.Add , , "12 หลักแรก", .Width \ 2
 
 -         .ColumnHeaders.Add , , "หลักทดสอบ", .Width \ 2 - 480, lvwColumnLeft
 
 -     End With
 
 -     
 
 - End Sub
 
  คัดลอกไปที่คลิปบอร์ด 
มาดูโค้ดของ VB.NET กันเถอะ ... 
- ' / -------------------------------------------------------------------
 
 - ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
 
 - ' / eMail : thongkorn@hotmail.com
 
 - ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
 
 - ' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
 
 - ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
 
 - ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
 
 - ' / MORE: http://www.g2gnet.com/webboard
 
 - ' /
 
 - ' / Purpose: Generate Barcode EAN13.
 
 - ' / Microsoft Visual Basic .NET (2010)
 
 - ' /
 
 - ' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
 
 - ' / You can modify and/or distribute without to inform the developer.
 
 - ' / -------------------------------------------------------------------
 
  
- Public Class frmGenerateEAN13
 
  
-     Private Sub frmGenerateEAN13_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
 
 -         Me.Dispose()
 
 -         End
 
 -     End Sub
 
  
-     Private Sub frmGenerateEAN13_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
 
 -         Call InitListView()
 
 -     End Sub
 
  
-     Private Sub btnGenEAN13_Click(sender As System.Object, e As System.EventArgs) Handles btnGenEAN13.Click
 
 -         lvwEAN13.Items.Clear()
 
 -         '//
 
 -         Call GenerateEAN13()
 
 -     End Sub
 
  
-     Sub GenerateEAN13()
 
 -         Dim LV As ListViewItem
 
 -         Randomize()
 
 -         '// สูตรการสุ่มตัวเลขจำนวนเต็ม
 
 -         ' Int ((upperbound - lowerbound + 1) * Rnd + lowerbound)
 
 -         Dim Digit As Byte, Count As Byte
 
 -         Dim TwelveDigit As String = String.Empty
 
 -         '// ตัวอย่าง 10 รายการ
 
 -         For Count = 0 To 9
 
 -             '// สุ่มตัวเลขจำนวน 12 หลักแรก
 
 -             For Digit = 0 To 11
 
 -                 TwelveDigit = TwelveDigit & CStr(Int((9) * Rnd()))
 
 -             Next
 
 -             ' Index = 0
 
 -             LV = lvwEAN13.Items.Add(String.Format("{0}", TwelveDigit))  ' Primary Node
 
 -             '// นำค่า 12 หลักแรก ไปคำนวณหาหลักทดสอบความถูกต้อง (หลักที่ 13)
 
 -             LV.SubItems.Add(CheckDigitEAN13(TwelveDigit))
 
 -             TwelveDigit = ""
 
 -         Next
 
 -     End Sub
 
  
-     ' / -----------------------------------------------------------------------------------------------
 
 -     ' / >> หลักการคิดคำนวณหา  <<
 
 -     ' / 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
 
 -     ' / -----------------------------------------------------------------------------------------------
 
 -     Private Function CheckDigitEAN13(TwelveDigit As String) As Byte
 
 -         ' / -----------------------------------------------------------------------------------------------
 
 -         ' จำนวน 12 หลัก
 
 -         Dim Digit As Byte
 
 -         ' ผลรวมหลักคี่
 
 -         Dim SumOdd As Integer
 
 -         ' ผลรวมหลักคู่
 
 -         Dim SumEven As Integer
 
 -         ' (ผลรวมหลักคู่ * 3) + ผลรวมหลักคี่
 
 -         Dim BarValue As Integer
 
  
-         For Digit = 1 To Len(TwelveDigit)
 
 -             ' หารเอาเศษด้วย 2 หากได้คำตอบ 0 แสดงว่าเป็นหลักคู่
 
 -             If Digit Mod 2 = 0 Then
 
 -                 SumEven = SumEven + Val(Mid$(TwelveDigit, Digit, 1))
 
 -             Else
 
 -                 SumOdd = SumOdd + Val(Mid$(TwelveDigit, Digit, 1))
 
 -             End If
 
 -         Next
 
  
-         BarValue = (SumEven * 3) + SumOdd
 
  
-         If BarValue Mod 10 = 0 Then
 
 -             CheckDigitEAN13 = 0
 
 -         Else
 
 -             CheckDigitEAN13 = 10 - BarValue Mod 10
 
 -         End If
 
 -     End Function
 
  
-     ' / Initailize ListView Control
 
 -     Sub InitListView()
 
 -         With lvwEAN13
 
 -             .Clear()
 
 -             .View = View.Details
 
 -             .GridLines = True
 
 -             .FullRowSelect = True
 
 -             .HideSelection = False
 
 -             .MultiSelect = False
 
 -             ' 1st Column Index = 0
 
 -             .Columns.Add("12 หลักแรก", lvwEAN13.Width \ 2)
 
 -             .Columns.Add("หลักทดสอบ", lvwEAN13.Width \ 2 - 20)
 
 -         End With
 
 -     End Sub
 
  
- End Class
 
  คัดลอกไปที่คลิปบอร์ด 
ดาวน์โหลดโค้ดต้นฉบับทั้ง VB6 และ VB.NET (2010) ได้ที่นี่ ...  
 
 |   
ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง
คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน  
 
x
 
 
 
 
 |