Total Tayangan Halaman

Jumat, 07 Agustus 2009

Barcode CODE 128 dengan VB .NET

Wah, aku gak mudeng dengan PHP, tapi pingin barcode CODE 128, gimana nih?
Tidak usah khawatir, karena kebetulan juga sudah tak buat. Silakan dicermati
Imports System.Drawing.Imaging
Imports System.IO


Public Class bar_value
Public value As Integer
Public bar As String






Public Sub New(ByVal val As Integer, ByVal bar_string As String)
value = val
bar = bar_string
End Sub
End Class


Public Class barcode128
Private start_b As New bar_value(104, "211214")
Private start_c As New bar_value(105, "211232")
Private code_b2c As New bar_value(99, "113141")
Private code_c2b As New bar_value(100, "114131")
Private stops As String = "2331112"
Private quiet_zone_start As String = "0901"
Private quiet_zone_end As String = "901"
Private code() As String = {"212222", "222122", "222221", "121223", "121322", _
"131222", "122213", "122312", "132212", "221213", _
"221312", "231212", "112232", "122132", "122231", _
"113222", "123122", "123221", "223211", "221132", _
"221231", "213212", "223112", "312131", "311222", _
"321122", "321221", "312212", "322112", "322211", _
"212123", "212321", "232121", "111323", "131123", _
"131321", "112313", "132113", "132311", "211313", _
"231113", "231311", "112133", "112331", "132131", _
"113123", "113321", "133121", "313121", "211331", _
"231131", "213113", "213311", "213131", "311123", _
"311321", "331121", "312113", "312311", "332111", _
"314111", "221411", "431111", "111224", "111422", _
"121124", "121421", "141122", "141221", "112214", _
"112412", "122114", "122411", "142112", "142211", _
"241211", "221114", "413111", "241112", "134111", _
"111242", "121142", "121241", "114212", "124112", _
"124211", "411212", "421112", "421211", "212141", _
"214121", "412121", "111143", "111341", "131141", _
"114113", "114311", "411113", "411311", "113141", _
"114131", "311141", "411131"}
Public bar_height As UInt16
Public bar_width As Byte
Public total_bar_width As UInt16
Private pattern() As String
Private checksum As Integer
Private bar As String
Private input As String
Private length As Integer
Public barcode_image As Bitmap
Public barcode_byte As Byte()


Protected Overrides Sub Finalize()
start_b = Nothing
start_c = Nothing
code_b2c = Nothing
code_c2b = Nothing
code = Nothing
If Not barcode_image Is Nothing Then barcode_image.Dispose()
barcode_byte = Nothing
pattern = Nothing
MyBase.Finalize()
End Sub


Public Sub New(ByVal height As UInt16)
Me.bar_height = height
End Sub


Public Sub New(ByVal height As UInt16, ByVal width As Byte)
Me.bar_height = height
Me.bar_width = width
End Sub


Public Sub barcode(Optional ByVal inp As String = "")
Me.input = inp
Me.initialize()
Me.draw_barcode()
End Sub


Public Function debug_barcode(Optional ByVal inp As String = "") As String
Me.input = inp
Me.initialize()
Return Me.debug_draw_barcode()
End Function


Private Sub reset()
Me.pattern = Nothing
Me.checksum = 0
Me.bar = ""
Me.length = 0
Me.total_bar_width = 0
barcode_image = Nothing
barcode_byte = Nothing
End Sub


Private Sub initialize()
Me.reset()
'create a pattern to decide which codefication applied (code b or code c)
Dim prev_char As String = "x"
Dim cur_char As String = ""
Dim count_char As Integer = 0
Dim temp_count As Integer = 0
Dim idx_code As Integer = 0
Dim i As Integer
For i = 0 To input.Length - 1
If Me.is_digit(Me.input(i)) Then
cur_char = "d"
Else
cur_char = "a"
End If
If prev_char = "x" Then
count_char = 1
ReDim pattern(0)
Me.pattern(0) = cur_char
ElseIf (prev_char = cur_char) Then
count_char += 1
Else
If ((prev_char = "a") Or (prev_char = "d" And count_char <= 3)) Then
temp_count += count_char
count_char = 1
ElseIf (count_char Mod 2 = 0) Then
If (temp_count > 0) Then Me.pattern(0) = temp_count.ToString
ReDim Preserve Me.pattern(Me.pattern.GetLength(0) + 1)
Me.pattern(Me.pattern.GetLength(0)) = count_char.ToString
temp_count = 0
count_char = 1
ElseIf (prev_char = "d" And count_char Mod 2 = 1) Then
If (temp_count > 0) Then Me.pattern(0) = temp_count.ToString
ReDim Preserve Me.pattern(Me.pattern.GetLength(0) + 1)
Me.pattern(Me.pattern.GetLength(0)) = CStr(count_char - 1)
temp_count = 0
count_char = 2
End If
End If
prev_char = cur_char
Next i
If ((prev_char = "a") Or (prev_char = "d" And count_char <= 3)) Then
ReDim Preserve Me.pattern(Me.pattern.GetLength(0))
Me.pattern(UBound(Me.pattern)) = CStr(temp_count + count_char)
ElseIf (count_char Mod 2 = 0) Then
If (temp_count > 0) Then Me.pattern(0) = temp_count.ToString
ReDim Preserve Me.pattern(Me.pattern.GetLength(0))
Me.pattern(UBound(Me.pattern)) = count_char.ToString
ElseIf (prev_char = "d" And count_char Mod 2 = 1) Then
If (temp_count > 0) Then Me.pattern(0) = temp_count.ToString
ReDim Preserve Me.pattern(Me.pattern.GetLength(0) + 1)
Me.pattern(UBound(Me.pattern) - 1) = CStr(count_char - 1)
Me.pattern(UBound(Me.pattern)) = "1"
End If


'save graph info and count checksum
If (Me.pattern(0) = "a") Then
Me.checksum = Me.start_b.value
Me.bar = Me.start_b.bar
idx_code = 0
Else
Me.checksum = Me.start_c.value
Me.bar = Me.start_c.bar
idx_code = 1
End If
Dim idx_input As Integer = 0
Dim idx_checksum As Integer = 1
For idx_pattern As Integer = 1 To UBound(Me.pattern)
If (idx_code Mod 2 = 0) Then
For i = 0 To CInt(Me.pattern(idx_pattern)) - 1
Me.checksum += idx_checksum * Me.ascii2idx_code(Me.input(idx_input))
Me.bar &= Me.code(Me.ascii2idx_code(Me.input(idx_input)))
idx_checksum += 1
idx_input += 1
Next i
If (idx_pattern <> Me.pattern.GetLength(0)) Then
Me.checksum += idx_checksum * Me.code_b2c.value
Me.bar &= Me.code_b2c.bar
idx_checksum += 1
End If
Else
For i = 0 To CInt(Me.pattern(idx_pattern)) - 1 Step 2
Me.checksum += idx_checksum * CInt(Me.input(idx_input).ToString)
Me.bar &= Me.code(CInt(Me.input(idx_input).ToString))
idx_checksum += 1
idx_input += 2
Next i
If (idx_pattern < UBound(Me.pattern) - 1) Then
Me.checksum += idx_checksum * Me.code_c2b.value
Me.bar &= Me.code_c2b.bar
idx_checksum += 1
End If
End If
idx_code += 1
Next idx_pattern
Me.checksum = Me.checksum Mod 103
Me.bar &= Me.code(CInt(Me.checksum))
Me.bar &= Me.stops
Me.bar = Me.quiet_zone_start & Me.bar & Me.quiet_zone_end
For i = 0 To Len(Me.bar) - 1
Me.total_bar_width += CType(Me.bar(i).ToString, UInt16)
Next i
End Sub


Private Function is_digit(ByVal character As Char) As Boolean
If (character >= "0" And character <= "9") Then
Return True
Else
Return False
End If
End Function


Private Function ascii2idx_code(ByVal character As Char) As Integer
Return Asc(character) - 32
End Function


Private Sub draw_barcode()
Dim GambarBarcode As Graphics
Dim hitam As New Pen(Color.Black)
hitam.Width = bar_width
Dim putih As New Pen(Color.White)
putih.Width = bar_width
Me.barcode_image = New Bitmap(Me.bar_width * Me.total_bar_width, Me.bar_height + 1)
GambarBarcode = Graphics.FromImage(barcode_image)
GambarBarcode.Clear(Color.White)
For i As Integer = 0 To Me.bar.Length - 1
If i Mod 2 = 0 Then
repeat_DrawLine(GambarBarcode, hitam, CInt(Me.bar(i).ToString))
Else
repeat_DrawLine(GambarBarcode, putih, CInt(Me.bar(i).ToString))
End If
Next i
Dim ImageStream As MemoryStream
ReDim Me.barcode_byte(0)
If Me.barcode_image IsNot Nothing Then
ImageStream = New MemoryStream
Me.barcode_image.Save(ImageStream, ImageFormat.Jpeg)
ReDim Me.barcode_byte(CInt(ImageStream.Length - 1))
ImageStream.Position = 0
ImageStream.Read(Me.barcode_byte, 0, CInt(ImageStream.Length))
End If
hitam.Dispose()
putih.Dispose()
GambarBarcode.Dispose()
End Sub


Private Sub repeat_DrawLine(ByRef canvas As Graphics, ByVal pena As Pen, ByVal numBar As Integer)
For i As Integer = 1 To numBar
canvas.DrawLine(pena, Me.length, 0, Me.length, Me.bar_height)
Me.length += 1
Next i
End Sub


Private Function debug_draw_barcode() As String
Dim ret As String = ""
For i As Integer = 0 To Me.bar.Length - 1
If i Mod 2 = 0 Then
ret &= Me.bar(i)
Else
ret &= Me.bar(i)
End If
Next i
Return ret
End Function
End Class

Tidak ada komentar: