Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows support tips #192

Open
dotMorten opened this issue Jul 16, 2024 · 0 comments
Open

Windows support tips #192

dotMorten opened this issue Jul 16, 2024 · 0 comments

Comments

@dotMorten
Copy link

dotMorten commented Jul 16, 2024

I noticed it states Windows isn't supported. I do see the camera view works on Windows, but I never actually get it to scan anything.
I figured I would share my crude little Windows scan version - you should be able to incorporate that (it doesn't even require underlying ZXing engine to scan, and works very fast. Use as you please (this uses a Window but just use the MediaElement in the Maui renderer and you should be good to go):

using Windows.Media.Capture;
using Windows.Devices.PointOfService;

namespace BarcodeScanner
{
 internal static class WinUIBarcodeScannerWindow
 {
     public static async Task<string?> ShowScannerUI()
     {
         BarcodeScanner scanner = await BarcodeScanner.GetDefaultAsync();
         if (string.IsNullOrEmpty(scanner.VideoDeviceId)) // No camera
             return null;

         ClaimedBarcodeScanner claimedScanner = await scanner.ClaimScannerAsync();
         claimedScanner.IsDecodeDataEnabled = true;
         await claimedScanner.EnableAsync();

         await claimedScanner.StartSoftwareTriggerAsync();
         var settings = new MediaCaptureInitializationSettings
         {
             VideoDeviceId = scanner.VideoDeviceId,
             StreamingCaptureMode = StreamingCaptureMode.Video,
             SharingMode = MediaCaptureSharingMode.SharedReadOnly,
         };

         var mediaCapture = new MediaCapture();
         await mediaCapture.InitializeAsync(settings);
         Microsoft.UI.Xaml.Controls.MediaPlayerElement elm = new Microsoft.UI.Xaml.Controls.MediaPlayerElement();
         var source = Windows.Media.Core.MediaSource.CreateFromMediaFrameSource(mediaCapture.FrameSources.First().Value);
         elm.Source = source;
         Microsoft.UI.Xaml.Window w = new Microsoft.UI.Xaml.Window();
         w.AppWindow.Resize(new Windows.Graphics.SizeInt32(640, 480));
        
         w.Content = elm;
         try
         {
             w.Activate();
             await source.OpenAsync();
             elm.MediaPlayer.Play();
             TaskCompletionSource<string?> result = new();
             claimedScanner.DataReceived += (s, e) =>
             {
                 var data = Windows.Security.Cryptography.CryptographicBuffer.ConvertBinaryToString(Windows.Security.Cryptography.BinaryStringEncoding.Utf8, e.Report.ScanDataLabel);
                 result.TrySetResult(data);
             };
             w.Closed += (s, e) =>
             {
                 result.TrySetResult(null);
             };
             return await result.Task;
         }
         finally
         {
             w.Close();
         }
     }
 }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant