private async void BarcodeScannerProviderConnection_StartSoftwareTriggerRequested(BarcodeScannerProviderConnection sender, BarcodeScannerStartSoftwareTriggerRequestEventArgs args) { using (args.GetDeferral()) { bool readerStarted = false; if (scannerEnabled) { if (frameReader == null) { // Create frame reader to read video frames frameReader = await connection.CreateFrameReaderAsync(BitmapPixelFormat.Nv12, new BitmapSize() { Width = 1280, Height = 720 }); frameReader.FrameArrived += FrameReader_FrameArrived; } readerStarted = await frameReader.StartAsync(); } if (readerStarted) { await args.Request.ReportCompletedAsync(); } else { await args.Request.ReportFailedAsync(); } } }
protected virtual void Dispose(bool disposing) { if (disposed) { return; } if (disposing) { frameReader?.Dispose(); frameReader = null; connection?.Dispose(); connection = null; deferral?.Complete(); deferral = null; disposed = true; } }
private async void FrameReader_FrameArrived(BarcodeScannerFrameReader sender, BarcodeScannerFrameReaderFrameArrivedEventArgs args) { if (!scannerEnabled) { // Return immediately if decoder is not enabled return; } using (args.GetDeferral()) { SoftwareBitmap bitmap = null; using (BarcodeScannerVideoFrame frame = await sender.TryAcquireLatestFrameAsync()) { if (frame != null) { // Build software bitmap from pixel data bitmap = SoftwareBitmap.CreateCopyFromBuffer(frame.PixelData, frame.Format, (int)frame.Width, (int)frame.Height); } } if (bitmap != null) { DecodeResult decodeResult = await decodeEngine.DecodeAsync(bitmap); bitmap.Dispose(); bitmap = null; // Report only if we have a result that is different from previous one. if (decodeResult != null && !decodeResult.Equals(previousDecodeResult)) { previousDecodeResult = decodeResult; await connection.ReportScannedDataAsync(CreateBarcodeScannerReportFromString(decodeResult.Text, decodeResult.Symbology)); } } } }