|
■No75500 (やっさん さん) に返信
> 環境:VB2008
使った事が無い上に、掲示板に直書きなので、
細かいミスとかは御容赦を:
Private Sub btnStartDecoding_Click(sender As Object, e As EventArgs ) Handles btnStartDecoding.Click
Dim fileName = txtBarcodeImageFile.Text
If Not File.Exists(fileName)
MessageBox.Show(Me, String.Format("File not found: {0}", fileName), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
Using bmp = DirectCast(Bitmap.FromFile(fileName), Bitmap)
If (TryOnlyMultipleQRCodes)
'Decode(bmp, TryMultipleBarcodes, New List(Of BarcodeFormat)() from { BarcodeFormat.QR_CODE })
Dim hoge As New List(Of BarcodeFormat)()
hoge.Add(BarcodeFormat.QR_CODE)
Decode(bmp, TryMultipleBarcodes, hoge)
Else
Decode(bmp, TryMultipleBarcodes, Nothing)
End If
End Using
End Sub
Private Sub Decode(image As Bitmap , tryMultipleBarcodes As Boolean, possibleFormats As IList(Of BarcodeFormat) )
resultPoints.Clear()
lastResults.Clear()
txtContent.Text = String.Empty
Dim timerStart = DateTime.Now.Ticks
Dim results() As Result = Nothing
barcodeReader.Options.PossibleFormats = possibleFormats
If (tryMultipleBarcodes)
results = barcodeReader.DecodeMultiple(image)
Else
Dim result = barcodeReader.Decode(image)
If result Is Not Nothing Then
'ここ少々自信なし
ReDim results(0)
results(0) = result
End If
End If
Dim timerStop = DateTime.Now.Ticks
If (results Is Nothing) txtContent.Text = "No barcode recognized"
labDuration.Text = New TimeSpan(timerStop - timerStart).Milliseconds.ToString("0 ms")
If (results IsNot Nothing)
For Each result In results
If (result.ResultPoints.Length > 0)
Dim rect = New Rectangle(CInt(result.ResultPoints(0).X), CInt(result.ResultPoints(0).Y), 1, 1)
For Each point In result.ResultPoints
If (point.X < rect.Left) rect = New Rectangle(CInt(point.X), rect.Y, rect.Width + rect.X - CInt(point.X), rect.Height)
if (point.X > rect.Right) rect = New Rectangle(rect.X, rect.Y, rect.Width + CInt(point.X) - rect.X, rect.Height)
if (point.Y < rect.Top) rect = New Rectangle(rect.X, CInt(point.Y), rect.Width, rect.Height + rect.Y - CInt(point.Y))
if (point.Y > rect.Bottom) rect = New Rectangle(rect.X, rect.Y, rect.Width, rect.Height + CInt(point.Y) - rect.Y)
Next
Using g = picBarcode.CreateGraphics()
g.DrawRectangle(Pens.Green, rect);
End Using
End If
Next
End If
End Sub
|