通用应用程序位图中的 ZXing

ZXing in Universal App bitmap

我有一个旧的 ZXing 代码,它可以与 Bitmaps 一起正常工作,但在我的 Win10 通用应用程序中,我正在使用 BitmapImage。如果 ZXing 也接受其他 BitmapImage,你能给我一个想法吗?或者我如何达到它工作的程度?

当我在 Win10 应用程序的 Class 中使用 using System.Drawing; 时,我在编译时遇到错误:

System.MarshalByRefObject not found

这是我以前的工作代码:

private string DecodeQRCodeFromBitmap(System.Drawing.Bitmap Picture)
{

    dynamic luminanceSource = new ZXing.BitmapLuminanceSource(Picture);
    dynamic binarizer = new ZXing.Common.HybridBinarizer(luminanceSource);


    ZXing.BinaryBitmap bb = new ZXing.BinaryBitmap(binarizer);
    ZXing.Result br = readCode2.decode(bb);
    if (br != null) {
        Interaction.MsgBox(br.ToString);
        return Convert.ToString(br.Text);
    } else {
        return string.Empty;
    }
}

新的无效代码:

    private string DecodeQRCodeFromBitmap(BitmapImage image)
    {
        dynamic luminanceSource = new ZXing.BitmapLuminanceSource(image);
        dynamic binarizer = new      ZXing.Common.HybridBinarizer(luminanceSource);

        ZXing.BinaryBitmap bb = new ZXing.BinaryBitmap(binarizer);
        ZXing.Result br = readCode.decode(bb);
        if (br != null)
        {
            return Convert.ToString(br.Text);
        }
        else {
            return string.Empty;
        }
    }

这是一个在 windows 通用应用程序中解码条形码的小示例。它在 vb.net 中,但您可以将其转换为 C# ... 或者在工作时问我 ...

Imports Windows.Media.Capture
Imports Windows.Storage.Streams
Imports Windows.Media.MediaProperties

Public NotInheritable Class MainPage
    Inherits Page

    Private capture As MediaCapture

    Private Async Function TakePhoto() As Task
        Try
            capture = New MediaCapture
            Await capture.InitializeAsync
            captureElementPreview.Source = capture
            Await capture.StartPreviewAsync
            Dim res As VideoEncodingProperties = capture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview)
            Dim iep As ImageEncodingProperties = ImageEncodingProperties.CreateBmp
            iep.Height = res.Height
            iep.Width = res.Width
            Dim barcodereader As New ZXing.BarcodeReader With {.AutoRotate = True}
            Dim wb As New WriteableBitmap(res.Width, res.Height)
            Dim result As ZXing.Result = Nothing
            While result Is Nothing
                Using stream = New InMemoryRandomAccessStream()
                    Await capture.CapturePhotoToStreamAsync(iep, stream)
                    stream.Seek(0)
                    Await wb.SetSourceAsync(stream)
                    result = barcodereader.Decode(wb)
                End Using
            End While
            Scanresult.Text = result.Text
            Await capture.StopPreviewAsync
            Debug.WriteLine(Scanresult.Text)
        Catch ex As Exception
            Debug.WriteLine("Exception when taking a photo: {0}", ex.ToString())
        End Try
    End Function

    Private Async Sub buttonCapture_Click(sender As Object, e As RoutedEventArgs)
        Await TakePhoto()
    End Sub

End Class

您需要从 zxing 库的子文件夹 \winrt 中导入 zxing.winrt.dll。我从 Whosebug 中的另一个 post 中获取这段代码。许多事情可以做得更好,但它适用于第一步......

问候弗兰克