QR码扫描在UWP中不起作用

时间:2016-12-29 09:47:59

标签: c# windows-runtime uwp

我使用ZXing.Net.Mobile并使用以下代码扫描QR码。

await scanner.Scan().ContinueWith(t =>
{
   if (t.Result != null)
       HandleScanResult(t.Result);
});

scanner.UseCustomOverlay = false;
scanner.ScanContinuously(async (res) =>
{
    var msg = "Found Barcode: " + res.Text;

    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
    {
        ViewHelper.showMessage(msg, "");
    });
});

我已经尝试过ContinueWith和ScanContinuosly,但没有一个能够正常工作。 我得到一个带红线的摄像机视图,但它没有扫描QR码。

我哪里错了。

2 个答案:

答案 0 :(得分:2)

我想你是否正在使用ZXing.NET套餐?

Mike Taulty在Windows 8.1上的扫描应用程序上编写了一个完整的博客文章系列,然后将其移植到Windows 10,甚至在HoloLens上运行它。最后的帖子还有一个小型伴侣应用程序,可在UWP上运行以进行简单扫描(使用语音命令应用程序进行扫描)。

在该样本中,他使用以下方法:

ZXingQrCodeScanner.ScanFirstCameraForQrCode(
    result =>
    {
      this.txtResult.Text = result?.Text ?? "none";
    },
    TimeSpan.FromSeconds(30));
  

假设系统中的第一台摄像机应该用于QR码扫描,但支持这种情况的类将允许采用更灵活的方法,并且ScanFirstCameraForQrCode功能扩展到以下步骤

public static class ZXingQrCodeScanner
{
  public static async void ScanFirstCameraForQrCode(
    Action<Result> resultCallback, 
    TimeSpan timeout)
  {
    Result result = null;

    var mediaFrameSourceFinder = new MediaFrameSourceFinder();      

    // We want a source of media frame groups which contains a color video
    // preview (and we'll take the first one).
    var populated = await mediaFrameSourceFinder.PopulateAsync(
      MediaFrameSourceFinder.ColorVideoPreviewFilter,
      MediaFrameSourceFinder.FirstOrDefault);

    if (populated)
    {
      // We'll take the first video capture device.
      var videoCaptureDevice =
        await VideoCaptureDeviceFinder.FindFirstOrDefaultAsync();

      if (videoCaptureDevice != null)
      {
        // Make a processor which will pull frames from the camera and run
        // ZXing over them to look for QR codes.
        var frameProcessor = new QrCaptureFrameProcessor(
          mediaFrameSourceFinder,
          videoCaptureDevice,
          MediaEncodingSubtypes.Bgra8);

        // Remember to ask for auto-focus on the video capture device.
        frameProcessor.SetVideoDeviceControllerInitialiser(
          vd => vd.Focus.TrySetAuto(true));

        // Process frames for up to 30 seconds to see if we get any QR codes...
        await frameProcessor.ProcessFramesAsync(timeout);

        // See what result we got.
        result = frameProcessor.QrZxingResult;
      }
    }
    // Call back with whatever result we got.
    resultCallback(result);
  }
}

来源:

我希望这种方法可以帮助你前进。

答案 1 :(得分:0)

您可以尝试我的解决方案: Barcode_Scanner_UWP on GitHub

我试图采用VideoScanZXingWinRT repo

他们都在使用ZXing.Net 但与老迈克Taulty sample相比,可以“在飞行中”捕获QR