-
Notifications
You must be signed in to change notification settings - Fork 3
/
dialogBarCodes.vb
291 lines (291 loc) · 14.1 KB
/
dialogBarCodes.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
Imports System.Windows.Forms
Imports ZXing.BarcodeReader
Imports ZXing.BarcodeWriter
Imports ZXing.PDF417
Imports ZXing.Datamatrix
Imports ZXing.QrCode
Imports ZXing.Rendering
Imports System.Drawing
Imports System.IO
Imports iTextSharp.text.pdf
Imports iTextSharp.text.pdf.Barcode
Class dialogBarCodes
''' <summary>
''' PdForms.net - An open source pdf form editor
''' Copyright 2018 NK-INC.COM All Rights reserved.
''' PdForms.net utilizes iTextSharp technologies.
''' Website: www.pdforms.net (source code), www.pdforms.com (about)
''' </summary>
Public barCodeBitmap As Bitmap = Nothing
Public barCodeData() As Byte
Public barCodeText As String
Public bmr As ZXing.BarcodeReader = Nothing
Public bmw As ZXing.BarcodeWriter = Nothing
Public barCodeWidth As Single = 0, barCodeHeight As Single = 0
Public barCodeOnly As Boolean = False
Public frmParent As Form
Public Sub New()
InitializeComponent()
End Sub
Public Sub New(ByRef frmMain1 As frmMain)
InitializeComponent()
frmParent = frmMain1
End Sub
Public Sub readBarCode(bm As Bitmap)
Try
barCodeBitmap = bm.Clone()
bmr = New ZXing.BarcodeReader()
If bmr.Options.PossibleFormats Is Nothing Then bmr.Options.PossibleFormats = New List(Of ZXing.BarcodeFormat)
If Not bmr.Options.PossibleFormats.Contains(ZXing.BarcodeFormat.QR_CODE) Then bmr.Options.PossibleFormats.Add(ZXing.BarcodeFormat.QR_CODE)
If Not bmr.Options.PossibleFormats.Contains(ZXing.BarcodeFormat.PDF_417) Then bmr.Options.PossibleFormats.Add(ZXing.BarcodeFormat.PDF_417)
If Not bmr.Options.PossibleFormats.Contains(ZXing.BarcodeFormat.DATA_MATRIX) Then bmr.Options.PossibleFormats.Add(ZXing.BarcodeFormat.DATA_MATRIX)
Dim rslt As ZXing.Result = bmr.Decode(barCodeBitmap)
If Not rslt Is Nothing Then
ComboBox1.SelectedValue = rslt.BarcodeFormat.ToString().Replace("_"c, " "c)
ComboBox1.SelectedText = rslt.BarcodeFormat.ToString().Replace("_"c, " "c)
TextBox6.Text = rslt.Text
PictureBox2.Image = barCodeBitmap.Clone()
End If
Catch ex As Exception
Err.Clear()
End Try
End Sub
Public Shared Function IsValidEan13(eanBarcode As String) As Boolean
Return IsValidEan(eanBarcode, 13)
End Function
Public Shared Function IsValidEan12(eanBarcode As String) As Boolean
Return IsValidEan(eanBarcode, 12)
End Function
Public Shared Function IsValidEan14(eanBarcode As String) As Boolean
Return IsValidEan(eanBarcode, 14)
End Function
Public Shared Function IsValidEan8(eanBarcode As String) As Boolean
Return IsValidEan(eanBarcode, 8)
End Function
Private Shared Function IsValidEan(eanBarcode As String, length As Integer) As Boolean
If eanBarcode.Length <> length Then
Return False
End If
Dim allDigits = eanBarcode.[Select](Function(c) Integer.Parse(c.ToString(System.Globalization.CultureInfo.InvariantCulture))).ToArray()
Dim s = If(length Mod 2 = 0, 3, 1)
Dim s2 = If(s = 3, 1, 3)
Return allDigits.Last() = (10 - (allDigits.Take(length - 1).[Select](Function(c, ci) c * (If(ci Mod 2 = 0, s, s2))).Sum() Mod 10)) Mod 10
End Function
Public Function isValidBarCode(bm As Bitmap) As Boolean
Try
If bm Is Nothing Then Return False
If bm.Width <= 0 Or bm.Height <= 0 Then Return False
bmr = New ZXing.BarcodeReader()
If Not bmr.Options.PossibleFormats.Contains(ZXing.BarcodeFormat.QR_CODE) Then bmr.Options.PossibleFormats.Add(ZXing.BarcodeFormat.QR_CODE)
If Not bmr.Options.PossibleFormats.Contains(ZXing.BarcodeFormat.PDF_417) Then bmr.Options.PossibleFormats.Add(ZXing.BarcodeFormat.PDF_417)
If Not bmr.Options.PossibleFormats.Contains(ZXing.BarcodeFormat.DATA_MATRIX) Then bmr.Options.PossibleFormats.Add(ZXing.BarcodeFormat.DATA_MATRIX)
Dim rslt As ZXing.Result = bmr.Decode(bm.Clone())
If Not rslt Is Nothing Then
If rslt.BarcodeFormat = Nothing Then
Return False
End If
If rslt.Text = "" Then
Return False
End If
writeBarCode()
Return True
End If
Catch ex As Exception
Err.Clear()
End Try
Return False
End Function
Public Function isValidBarCode(bmBytes() As Byte) As Boolean
Try
Dim bm As Bitmap = Bitmap.FromStream(New MemoryStream(bmBytes))
If bm Is Nothing Then Return False
If bm.Width <= 0 Or bm.Height <= 0 Then Return False
bmr = New ZXing.BarcodeReader()
If bmr.Options.PossibleFormats Is Nothing Then bmr.Options.PossibleFormats = New List(Of ZXing.BarcodeFormat)
If Not bmr.Options.PossibleFormats.Contains(ZXing.BarcodeFormat.QR_CODE) Then bmr.Options.PossibleFormats.Add(ZXing.BarcodeFormat.QR_CODE)
If Not bmr.Options.PossibleFormats.Contains(ZXing.BarcodeFormat.PDF_417) Then bmr.Options.PossibleFormats.Add(ZXing.BarcodeFormat.PDF_417)
If Not bmr.Options.PossibleFormats.Contains(ZXing.BarcodeFormat.DATA_MATRIX) Then bmr.Options.PossibleFormats.Add(ZXing.BarcodeFormat.DATA_MATRIX)
Dim tryCOunt As Integer = -1
GOTO_RETRY:
Dim rslt As ZXing.Result = bmr.Decode(bm.Clone())
If rslt Is Nothing And tryCOunt < 0 Then
tryCOunt += 1
bmr.Options.TryHarder = True
bmr.AutoRotate = True
bmr.TryInverted = True
bmr.Options.PureBarcode = True
GoTo GOTO_RETRY
Else
If rslt.BarcodeFormat = Nothing Then
Return False
Else
Select Case rslt.BarcodeFormat
Case ZXing.BarcodeFormat.DATA_MATRIX
ComboBox1.SelectedIndex = 2
Case ZXing.BarcodeFormat.PDF_417
ComboBox1.SelectedIndex = 0
Case ZXing.BarcodeFormat.QR_CODE
ComboBox1.SelectedIndex = 1
End Select
End If
If rslt.Text = "" Then
Return False
Else
TextBox6.Text = rslt.Text & ""
End If
writeBarCode()
Return True
End If
Catch ex As Exception
Err.Clear()
End Try
Return False
End Function
Public Shared Function isValidBarCode_Shared(bm As Bitmap) As Boolean
Try
If bm Is Nothing Then Return False
If bm.Width <= 0 Or bm.Height <= 0 Then Return False
bm = bm.Clone()
Dim bmr1 = New ZXing.BarcodeReader()
If bmr1.Options.PossibleFormats Is Nothing Then bmr1.Options.PossibleFormats = New List(Of ZXing.BarcodeFormat)
bmr1.Options.PossibleFormats.Add(ZXing.BarcodeFormat.QR_CODE)
bmr1.Options.PossibleFormats.Add(ZXing.BarcodeFormat.PDF_417)
bmr1.Options.PossibleFormats.Add(ZXing.BarcodeFormat.DATA_MATRIX)
Dim rslt As ZXing.Result = bmr1.Decode(bm)
If Not rslt Is Nothing Then
If rslt.BarcodeFormat = Nothing Then
Return False
End If
If rslt.Text = "" Then
Return False
End If
Return True
End If
Catch ex As Exception
Err.Clear()
End Try
Return False
End Function
Public Shared Function isValidBarCode_Shared(bmBytes() As Byte) As Boolean
Try
Dim bm As Bitmap = Bitmap.FromStream(New MemoryStream(bmBytes))
If bm Is Nothing Then Return False
If bm.Width <= 0 Or bm.Height <= 0 Then Return False
bm = bm.Clone()
Dim bmr1 = New ZXing.BarcodeReader()
If bmr1.Options.PossibleFormats Is Nothing Then bmr1.Options.PossibleFormats = New List(Of ZXing.BarcodeFormat)
bmr1.Options.PossibleFormats.Add(ZXing.BarcodeFormat.QR_CODE)
bmr1.Options.PossibleFormats.Add(ZXing.BarcodeFormat.PDF_417)
bmr1.Options.PossibleFormats.Add(ZXing.BarcodeFormat.DATA_MATRIX)
Dim rslt As ZXing.Result = bmr1.Decode(bm)
If Not rslt Is Nothing Then
If rslt.BarcodeFormat = Nothing Then
Return False
End If
If rslt.Text = "" Then
Return False
End If
Return True
End If
Catch ex As Exception
Err.Clear()
End Try
Return False
End Function
Public Sub writeBarCode()
Try
If String.IsNullOrEmpty(TextBox6.Text & "") Then
Return
End If
bmw = New ZXing.BarcodeWriter()
Select Case ComboBox1.SelectedItem().ToString().ToString.Replace("_"c, " "c).ToLower()
Case "QR Code".ToLower
bmw.Format = ZXing.BarcodeFormat.QR_CODE
bmw.Options.Width = barCodeWidth
bmw.Options.Height = barCodeHeight
bmw.Options.PureBarcode = barCodeOnly
barCodeBitmap = bmw.Write(TextBox6.Text.ToString()).Clone()
PictureBox2.Image = barCodeBitmap.Clone()
Case "DATA MATRIX".ToLower
bmw.Format = ZXing.BarcodeFormat.DATA_MATRIX
bmw.Options.Width = barCodeWidth
bmw.Options.Height = barCodeHeight
bmw.Options.PureBarcode = barCodeOnly
barCodeBitmap = bmw.Write(TextBox6.Text.ToString()).Clone()
PictureBox2.Image = barCodeBitmap.Clone()
Return
Case "EAN 13".ToLower
bmw.Format = ZXing.BarcodeFormat.EAN_13
bmw.Options.Width = barCodeWidth
bmw.Options.Height = barCodeHeight
bmw.Options.PureBarcode = barCodeOnly
barCodeBitmap = bmw.Write(TextBox6.Text.ToString()).Clone()
PictureBox2.Image = barCodeBitmap.Clone()
Return
Case Else
Return
End Select
If Math.Abs(CInt(barCodeBitmap.Width) - barCodeWidth) > 2 Or Math.Abs(CInt(barCodeBitmap.Height) - barCodeHeight) > 2 Then
If barCodeWidth > 0 Or barCodeHeight > 0 Then
barCodeBitmap = ResizeImage(barCodeWidth, barCodeHeight, barCodeBitmap.Clone())
PictureBox2.Image = barCodeBitmap.Clone()
End If
End If
Catch ex As Exception
Err.Clear()
End Try
End Sub
Private Function ResizeImage(newWidth As Integer, newHeight As Integer, imgPhoto As Image) As Image
Dim sourceWidth As Integer = imgPhoto.Width
Dim sourceHeight As Integer = imgPhoto.Height
If sourceWidth < sourceHeight Then
Dim buff As Integer = newWidth
newWidth = newHeight
newHeight = buff
End If
Dim sourceX As Integer = 0, sourceY As Integer = 0, destX As Integer = 0, destY As Integer = 0
Dim nPercent As Single = 0, nPercentW As Single = 0, nPercentH As Single = 0
nPercentW = (CSng(newWidth) / CSng(sourceWidth))
nPercentH = (CSng(newHeight) / CSng(sourceHeight))
If nPercentH < nPercentW Then
nPercent = nPercentH
destX = System.Convert.ToInt16((newWidth - (sourceWidth * nPercent)) / 2)
Else
nPercent = nPercentW
destY = System.Convert.ToInt16((newHeight - (sourceHeight * nPercent)) / 2)
End If
Dim destWidth As Integer = CInt(sourceWidth * nPercent)
Dim destHeight As Integer = CInt(sourceHeight * nPercent)
Dim bmPhoto As New Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution)
Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
grPhoto.FillRectangle(Brushes.White, 0, 0, bmPhoto.Width, bmPhoto.Height)
grPhoto.SmoothingMode = Drawing2D.SmoothingMode.None
grPhoto.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
grPhoto.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
If destX > 0 Or destY > 0 Then
grPhoto.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBilinear
Else
grPhoto.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
End If
grPhoto.DrawImage(imgPhoto, New Rectangle(destX, destY, destWidth, destHeight), New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Point)
grPhoto.Dispose()
Return bmPhoto
End Function
Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
Me.DialogResult = System.Windows.Forms.DialogResult.OK
Me.Close()
End Sub
Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.Close()
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
writeBarCode()
End Sub
Private Sub TextBox6_TextChanged(sender As Object, e As EventArgs) Handles TextBox6.TextChanged
writeBarCode()
End Sub
Private Sub dialogBarCodes_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class