[英]I need to read a qr-code with ZXING.net librays
Here is my code for reading a qr-code in a picturebox这是我在图片框中读取二维码的代码
no matter what code i try i always get a problem无论我尝试什么代码,我总是遇到问题
BC30311 Unable to convert a value of type 'Bitmap' to 'Bitmap'. BC30311 无法将“位图”类型的值转换为“位图”。
' Obtenir l'image QR code à partir du fichier
Dim image As Bitmap = CType(Bitmap.FromFile("file.png"), Bitmap)
' Lire le contenu du fichier en tant qu'un tableau de bytes
Dim bytes As Byte() = File.ReadAllBytes("file.png")
Try
Using image
' Créer une source de luminance à partir de l'image
Dim source As LuminanceSource = New BitmapLuminanceSource(**image**)
' Créer un objet BinaryBitmap à partir de la source de luminance
Dim bitmap As BinaryBitmap = New BinaryBitmap(New HybridBinarizer(source))
' Décoder le QR code en utilisant un lecteur multi-formats
Dim result As Result = New MultiFormatReader().decode(bitmap)
If result IsNot Nothing Then
' Code trouvé
Dim data As String() = result.Text.Split(Environment.NewLine)
Else
' Pas de code trouvé
End If
End Using
Catch ex As Exception
Throw New Exception("Impossible de décoder le QR code : " & ex.Message)
End Try
Pls help !请帮助!
I tried to read a qr-code with zxing.net in vb.net我试图在 vb.net 中使用 zxing.net 读取二维码
but i always get a proble with luminanceSource and bitmap conversion但我总是遇到 luminanceSource 和 bitmap 转换的问题
Try the following:尝试以下操作:
Create a Windows Forms App (.NET Framework)
创建一个
Windows Forms App (.NET Framework)
Download/install NuGet package: ZXing.Net
下载/安装 NuGet package:
ZXing.Net
Add the following Imports :添加以下导入:
Imports System.IO
Imports ZXing
Imports ZXing.Common
Imports ZXing.QrCode
Form1.vb Form1.vb
Private Sub SaveQrCode(data As String, filename As String, imgFormat As System.Drawing.Imaging.ImageFormat)
'create QR code and save to file
Using bmp As Bitmap = CreateQrCode(data)
bmp.Save(filename, imgFormat)
End Using
End Sub
Private Function CreateQrCode(data As String) As Bitmap
'specify desired options
Dim options As QrCodeEncodingOptions = New QrCodeEncodingOptions With
{
.DisableECI = True,
.CharacterSet = "UTF-8",
.Width = 250,
.Height = 250
}
'create new instance and set properties
Dim writer As BarcodeWriter = New BarcodeWriter() With {.Format = BarcodeFormat.QR_CODE, .Options = options}
'create QR code and return Bitmap
Return writer.Write(data)
End Function
Private Function GetTextFromQrCode(filename As String) As String
'specify desired options
Dim options As DecodingOptions = New DecodingOptions() With
{
.CharacterSet = "UTF-8"
}
'create new instance and set properties
Dim reader As BarcodeReader = New BarcodeReader() With {.Options = options}
'read image and convert to Bitmap
Using bmp As Bitmap = DirectCast(Bitmap.FromFile(filename), Bitmap)
'decode QR code
Dim r As Result = reader.Decode(bmp)
'return QR code text
Return r.Text
End Using
End Function
Usage (decode QR code) :用法(解码二维码) :
Note : The following assumes that a Button named btnDecodeQrCode
exists on the Form.注意:以下假定 Form 上存在名为
btnDecodeQrCode
的 Button。
Private Sub btnDecodeQrCode_Click(sender As Object, e As EventArgs) Handles btnDecodeQrCode.Click
Using ofd As OpenFileDialog = New OpenFileDialog()
ofd.Filter = "All Files (*.*)|*.*|Bitmap Image File (*.bmp;*.dib;*.rle)|*.bmp;*.dib;*.rle|JPEG File Interchange Format (*.jpg;*.jpeg;*.jfif;*.jpe)|*.jpg;*.jpeg;*.jfif;*.jpe|Portable Network Graphics (*.png)|*.png"
If ofd.ShowDialog = DialogResult.OK Then
'get text from QR code
TextBoxDecodedData.Text = GetTextFromQrCode(ofd.FileName)
End If
End Using
End Sub
Usage (save QR Code) :使用方法(保存二维码) :
Note : The following assumes that a Button named btnSaveQrCode
exists on the Form.注意:以下假定 Form 上存在名为
btnSaveQrCode
的 Button。
Private Sub btnSaveQrCode_Click(sender As Object, e As EventArgs) Handles btnSaveQrCode.Click
If String.IsNullOrEmpty(TextBoxInput.Text) Then
MessageBox.Show("QR Code text not specified.", "Error - QR Code Text", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
Using sfd As SaveFileDialog = New SaveFileDialog()
sfd.Filter = "All Files (*.*)|*.*|Bitmap Image File (*.bmp;*.dib;*.rle)|*.bmp;*.dib;*.rle|JPEG File Interchange Format (*.jpg;*.jpeg;*.jfif;*.jpe)|*.jpg;*.jpeg;*.jfif;*.jpe|Portable Network Graphics (*.png)|*.png"
sfd.FileName = "QRCode-Test"
sfd.FilterIndex = 4
If sfd.ShowDialog = DialogResult.OK Then
'get filename extension
Dim ext As String = Path.GetExtension(sfd.FileName)
Debug.WriteLine($"ext: '{ext}'")
If ext = ".bmp" OrElse ext = ".dib" OrElse ext = ".rle" Then
SaveQrCode(TextBoxInput.Text, sfd.FileName, System.Drawing.Imaging.ImageFormat.Bmp)
ElseIf ext = ".jpg" OrElse ext = ".jpeg" OrElse ext = ".jfif" OrElse ext = ".jpe" Then
SaveQrCode(TextBoxInput.Text, sfd.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
ElseIf ext = ".png" Then
SaveQrCode(TextBoxInput.Text, sfd.FileName, System.Drawing.Imaging.ImageFormat.Png)
End If
End If
End Using
End Sub
Resources资源
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.