2016-12-29 77 views
0

我正在使用ZXing.Net.Mobile并具有以下代码来扫描QR代码。QR代码扫描在UWP中不起作用

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码。

我在哪里出错了。

+0

第一行代码困扰了我很多 – Alex

+0

好吧,我解决了这个问题,通过手动卸载nuget并添加所需的项目https://github.com/Redth/ZXing.Net.Mobile。 也更新了Microsoft.NetCore.UniversalWindowsPlatform。我猜目前的nuget包存在一些问题。希望他们解决这个问题 – AbsoluteSith

回答

2

我想你使用的是ZXing.NET包?

Mike Taulty在Windows 8.1上使用Scanning编写了一个完整的博客文章系列,然后将其移植到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); 
    } 
} 

源以下步骤:

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