Reaperino
Reaperino

Reputation: 145

Disable barcode scanner on preview window closing

When I close the preview window of my BarcodeScanner the webcam stay active and I want to prevent that because it's kind of weird to see that the camera is still on and that you can still scan some barcode even if I close the preview.

I can't figure out how to disable the BarcodeScanner when I close the preview window.

Here's my 'BarcodeScanner' code :

private async Task<bool> ClaimScanner()
{
    bool res = false;

    string selector = BarcodeScanner.GetDeviceSelector();
    DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(selector);

    if(scanner == null)
        scanner = await BarcodeScanner.FromIdAsync(deviceCollection[0].Id);

    if (scanner != null)
    {
        if(claimedBarcodeScanner == null)
            claimedBarcodeScanner = await scanner.ClaimScannerAsync();

        if (claimedBarcodeScanner != null)
        {
            claimedBarcodeScanner.DataReceived += ClaimedBarcodeScanner_DataReceivedAsync;
            claimedBarcodeScanner.ReleaseDeviceRequested += ClaimedBarcodeScanner_ReleaseDeviceRequested;
            claimedBarcodeScanner.IsDecodeDataEnabled = true;
            claimedBarcodeScanner.IsDisabledOnDataReceived = true;
            await claimedBarcodeScanner.EnableAsync();

            res = true;
            Debug.WriteLine("Barcode Scanner claimed");
        }
    }
    antispam = false;
    return res;
}

public async void ScanBarcodeAsync()
{
    if(claimedBarcodeScanner == null && !antispam)
    {
        antispam = true;
        await ClaimScanner();
    }
    if(claimedBarcodeScanner != null)
    {
        await claimedBarcodeScanner.ShowVideoPreviewAsync();
        await claimedBarcodeScanner.StartSoftwareTriggerAsync();
        claimedBarcodeScanner = null;
    }
}

private async void ClaimedBarcodeScanner_DataReceivedAsync(ClaimedBarcodeScanner sender, BarcodeScannerDataReceivedEventArgs args)
{
    await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            if (currentDataContext != null && currentDataContext is Scannable)
            {
                Debug.WriteLine(CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, args.Report.ScanDataLabel));
                Scannable obj = (Scannable)currentDataContext;
                obj.NumSerie = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, args.Report.ScanDataLabel);
            }
        }
    );
}

void ClaimedBarcodeScanner_ReleaseDeviceRequested(object sender, ClaimedBarcodeScanner e)
{
    // always retain the device
    e.RetainDevice();
}

EDIT : I used the library indicated by Microsoft provided by Digimarc : https://learn.microsoft.com/en-us/windows/uwp/devices-sensors/pos-camerabarcode

Upvotes: 0

Views: 654

Answers (1)

Nico Zhu
Nico Zhu

Reputation: 32775

Disable barcode scanner on preview window closing

ClaimedBarcodeScanner has StopSoftwareTriggerAsync method, if you want to disable barcode scanner on preview window closing, you just invoke StopSoftwareTriggerAsync method after HideVideoPreview.

private async void HidePreviewButton_Click(object sender, RoutedEventArgs e)
{
    claimedScanner?.HideVideoPreview();
    await claimedScanner?.StopSoftwareTriggerAsync();   
}

Upvotes: 1

Related Questions