简体   繁体   English

Zxing.Net 读取二维码

[英]Zxing.Net ReadQRCode

I am using the Zxing.Net library to generate QR codes.我正在使用 Zxing.Net 库来生成二维码。 I update each users QRcode on a daily basis, based on answers to a daily health survey.我根据每日健康调查的答案每天更新每个用户的二维码。 If it is a new user, the QRCode is created for the first time on the web server.如果是新用户,第一次在web服务器上创建二维码。 If the file already exists, it just writes over it.如果文件已经存在,它只是覆盖它。

I have begun using the ReadQRCode() sub in a different process to read the QR code and display the data.我已经开始在不同的进程中使用 ReadQRCode() 子来读取 QR 码并显示数据。 However, after doing so, I can no longer update the QR code as it is "being used by another process".但是,这样做之后,我无法再更新 QR 码,因为它“正在被另一个进程使用”。 This only happens when I use the ReadQRCode() sub to read/decode the QR to a web page.这只发生在我使用 ReadQRCode() 子读取/解码 QR 到 web 页面时。 The only way I have been able to remove this "file lock" is by restarting the web server.我能够删除此“文件锁定”的唯一方法是重新启动 web 服务器。

Generate the QRCode which is not the issue:生成不是问题的二维码:

  Public Sub GenerateCode(name As String)
        Dim intLoc As Integer
        Dim strFileName As String

        intLoc = Strings.InStr(name, "_")
        strFileName = name.Substring(0, intLoc - 1)
        name = name.Trim()
        Dim writer = New BarcodeWriter()
        writer.Format = BarcodeFormat.QR_CODE
        Dim result = writer.Write(name)
        Dim path As String = Server.MapPath("~/qrCodeImages/" & strFileName & ".jpg")
        Dim barcodeBitmap = New Bitmap(result)

        Using memory As New MemoryStream()
            Using fs As New FileStream(path, FileMode.Create, FileAccess.ReadWrite)
                barcodeBitmap.Save(memory, ImageFormat.Jpeg)
                Dim bytes As Byte() = memory.ToArray()
                fs.Write(bytes, 0, bytes.Length)
                fs.Close()
            End Using
            memory.Close()
        End Using

Reading the qrcode image :读取二维码图片

Sub ReadQRCode()
        Dim reader = New BarcodeReader()
        Dim filename As String = Path.Combine(Request.MapPath("~/qrCodeImages/"), strFileName & ".jpg")
        ' Detect and decode the barcode inside the bitmap
        Dim result = reader.Decode(New Bitmap(filename))
        If result IsNot Nothing Then
            qrData.Visible = True
            qrData.Text = "QR Code: " + result.Text
        End If

    End Sub

Error trying to update QRCode after it has been read by Sub ReadQRCode Sub ReadQRCode 读取后尝试更新 QRCode 时出错

You have to dispose the bitmap instance which is created for the decoding process.您必须处理为解码过程创建的 bitmap 实例。 It is a common problem that the Bitmap class holds the file in a locked state until it is disposed or collected by the GC.一个常见的问题是 Bitmap class 将文件保存在锁定的 state 中,直到它被 GC 处理或收集。 Try the following code snippet.试试下面的代码片段。

Sub ReadQRCode()
    Dim reader = New BarcodeReader()
    Dim filename As String = Path.Combine(Request.MapPath("~/qrCodeImages/"), strFileName & ".jpg")
    ' Detect and decode the barcode inside the bitmap
    Using bitmap As New Bitmap(filename)
        Dim result = reader.Decode(bitmap)
        If result IsNot Nothing Then
            qrData.Visible = True
            qrData.Text = "QR Code: " + result.Text
        End If
    End Using

End Sub

Perhaps you should add using blocks in the method GenerateCode, too.也许您也应该在 GenerateCode 方法中添加 using 块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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