2016-12-29 79 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); 
    } 
} 

源以下步驟:

我希望這種方法能幫助你前進。