简体   繁体   中英

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'.

        ' 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

but i always get a proble with luminanceSource and bitmap conversion

Try the following:

Create a Windows Forms App (.NET Framework)

Download/install NuGet package: ZXing.Net

Add the following Imports :

  • Imports System.IO
  • Imports ZXing
  • Imports ZXing.Common
  • Imports ZXing.QrCode

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM